
    -                         d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZ ddlmZ d Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Z G d dej4                        ZddZedk(  r ej<                   e              yy)z%Command-line interface to gen_client.    N)
exceptions)gen_client_lib)utilc                    t        j                  t        j                  | d            5 }t	        j
                  d|       }|t        j                  d| z        |j                  |       d d d        y # 1 sw Y   y xY w)Nwapitools.base.pyzCould not find file %s)	
contextlibclosingioopenpkgutilget_datar   GeneratedClientErrorwrite)filenameoutsrc_datas      *lib/third_party/apitools/gen/gen_client.py_CopyLocalFiler   !   sk    			BGGHc2	3s##*11(835 5		( 
4	3	3s   AA55A>c                    | j                   r 	 t        j                  | j                         S t        j                  j                  | j                        xs d}t        j                  |d      5 }t        j                  t        j                  |j                                     cddd       S # t        j                  $ r t        j
                  d      w xY w# 1 sw Y   yxY w)z!Get the discovery doc from flags.zCould not fetch discovery docz
/dev/stdinutf8encodingN)discovery_urlr   FetchDiscoveryDocr   CommunicationErrorr   ospath
expanduserinfiler   r   jsonloadsReplaceHomoglyphsread)argsr    fs      r   _GetDiscoveryDocFromFlagsr'   +   s    	1))$*<*<==
 WW,<F	&	)Qzz$00:; 
*	) ,, 	111/1 1	1
 
*	)s   B1 16C1)CC&c                    t        |       }t        j                  | j                  | j                  | j
                        }| j                  r	 t        j                  | j                  d      5 }t        j                  t        j                  |j                                     }|j                  d|j                  di             }|j                  d      }|j                  d      }ddd       n| j"                  }| j$                  }st'        j(                  d	       d
}st'        j(                  d       d
}t        j*                  j-                  || j.                  ||| j0                  || j2                  | j4                        }t6        j8                  j;                  | j<                        xs |j>                  }	t6        j8                  jA                  |	      r!| jB                  st        jD                  d      t6        j8                  jA                  |	      st7        jF                  |	       tI        jJ                  |||| jL                  |	| jN                  | jP                  | jR                  dk(  | jT                  | jV                  | jX                        S # 1 sw Y   xY w# t        $ r# t        j                   d| j                  z        w xY w)z#Create a codegen object from flags.r   r   	installedweb	client_idclient_secretNz#Failed to open client json file: %szNo client ID supplied zNo client secret suppliedzHOutput directory exists, pass --overwrite to replace the existing files.	wildcards)base_packageprotorpc_packageinit_wildcards_file
use_proto2unelidable_request_methodsapitools_version)-r'   r   Namesstrip_prefixexperimental_name_conventionexperimental_capitalize_enumsclient_jsonr   r   r!   r"   r#   r$   getIOErrorr   NotFoundErrorr+   r,   loggingwarning
ClientInfoCreatescope
user_agentapi_keyversion_identifierr   r   r   outdirdefault_directoryexists	overwriteConfigurationValueErrormakedirsr   DescriptorGeneratorroot_packager/   r0   	init_fileexperimental_proto2_outputr3   r4   )
r%   discovery_docnamesr9   r&   r*   r+   r,   client_inforE   s
             r   _GetCodegenFromFlagsrR   9   s@   -d3MJJ))**,E
 	J))F;{JJt55k6F6F6HIJeeKub)9:GGK0	 # 8	 < NN	**/0	34//((tzz9md.E.EGK WW,M0M0MF	ww~~fdnn00"# 	# 77>>&!
F--{E4+<+<f&&..!^^{:22#'#B#B..0 0? <;
  	J**58H8HHJ J	Js%   !J, /A;J*J, J)$J, ,,Kc                     t        j                  | j                        5  t        d       t        d       t        d       d d d        y # 1 sw Y   y xY w)Nzbase_api.pyzcredentials_lib.pyzexceptions.py)r   ChdirrE   r   )codegens    r   _WriteBaseFilesrV   m   s7    	GNN	#}%+,' 
$	#	#s   "AAc                 |    t        j                  dd      5 }| j                  |       d d d        y # 1 sw Y   y xY wNz__init__.pyr   )r   r   WriteIntermediateInitrU   r   s     r   _WriteIntermediateInitr[   t   s,    		$%%c* 
%	$	$   2;c                    t        j                  | j                        5  t        j                  | j
                  j                  d      5 }| j                  |       d d d        t        j                  | j
                  j                  d      5 }| j                  |       d d d        d d d        y # 1 sw Y   VxY w# 1 sw Y   xY w# 1 sw Y   y xY wNr   )
r   rT   rE   r   r   rQ   messages_proto_file_nameWriteMessagesProtoFileservices_proto_file_nameWriteServicesProtoFilerZ   s     r   _WriteProtoFilesrc   y   s    	GNN	#WWW((AA3G3**3/ HWWW((AA3G3**3/ H 
$	#GGGG 
$	#s;   +C
B22C
B>!C
2B;	7C
>C	C

Cc                    |j                   rt        |       t        j                  |j                        5  t        j                  |j                  j                  d      5 }|j                  |       d d d        t        j                  |j                  j                  d      5 }|j                  |       d d d        d d d        y # 1 sw Y   VxY w# 1 sw Y   xY w# 1 sw Y   y xY wr^   )r2   rc   r   rT   rE   r   r   rQ   messages_file_nameWriteMessagesFileclient_file_nameWriteClientLibrary)r%   rU   r   s      r   _WriteGeneratedFilesri      s    !	GNN	#WWW((;;SAS%%c* BWWW((993?3&&s+ @ 
$	#AA?? 
$	#s;   +C!"C	42C!&C8C!	C	C!C	C!!C*c                     t        j                  | j                        5  t        j                  dd      5 }| j                  |       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY wrX   )r   rT   rE   r   r   	WriteInitrZ   s     r   
_WriteInitrl      sM    	GNN	#WW]C(Cc" ) 
$	#(( 
$	#s"   A&A	A&A#	A&&A/c                 |    t        j                  dd      5 }| j                  |       d d d        y # 1 sw Y   y xY w)Nzsetup.pyr   )r   r   WriteSetupPyrZ   s     r   _WriteSetupPyro      s,    	S	!SS! 
"	!	!r\   c                     t        |       }|t        j                  d       yt        | |       | j                  dk7  rt        |       yy)z"Driver for client code generation.N"Failed to create codegen, exiting.   none)rR   r=   errorri   rM   rl   r%   rU   s     r   GenerateClientrv      sF     #4(G:;w'~~7      c                    t        |       }|d   }t        j                  j                  | j                        }t        j                  j                  | j                  d|z        | _        d|z  | _        t        |       }|t        j                  d       yt        | |       t        |       t        j                  |      5  t        |       t        j                  d      5  t        |       t        j                  d      5  t        |       ddd       ddd       ddd       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY w)	z/Generate a client as a pip-installable tarball.namezapitools/clients/%szapitools.clients.%sNrq      apitoolsclients)r'   r   r   r   rE   joinrL   rR   r=   rt   ri   rl   r   rT   ro   r[   )r%   rO   packageoriginal_outdirrU   s        r   GeneratePipPackager      s     .d3MF#Ggg((5O'',,*W46DK-7D"4(G:;w'w	O	$gZZ
#"7+I&&w/ ' $ 
%	$ '& $# 
%	$s<   !E&!D8D,D8E,D51D88E	=EEc                 0    t        |       }t        |       y)z2Generate just the two proto files for a given API.N)rR   rc   ru   s     r   GenerateProtor      s     #4(GWrw   c                       e Zd ZddZy)_SplitCommaSeparatedListNc                 P    t        || j                  |j                  d             y )N,)setattrdestsplit)selfparser	namespacevaluesoption_strings        r   __call__z!_SplitCommaSeparatedList.__call__   s    	499fll3&78rw   N)__name__
__module____qualname__r    rw   r   r   r      s    9rw   r   c                    | t         j                  } t        j                  d      }|j	                         }|j                  dd       |j                  dd       |j                  dd	d
       |j                  ddd       |j                  dd       |j                  ddd       |j                  dddd       |j                  ddd       |j                  ddg d       |j                  dd        |j                  d!d"       |j                  d#d$d%       |j                  d&d'd(       |j                  d)dg d*       |j                  d+d,d-       |j                  d.d/dd01       |j                  d2d/d3d01       |j                  d4g d5d6 d7d89       |j                  d:t        g d;<       |j                  d=dd>d?@       |j                  dAdddB       |j                  dCt        j                  j                  t        j                  j                  dDE       |j                  dFdddG       |j                  dH      }|j                  dIdJ      }|j                  t        K       |j                  dLdM      }|j                  t        K       |j                  dNdO      }|j                  t         K       |j#                  | dPd        }|j%                  |      xs dQS )RNzApitools Client Code Generator)descriptionz--infilezLFilename for the discovery document. Mutually exclusive with --discovery_url)helpz--discovery_urlz[URL (or "name.version") of the discovery document to use. Mutually exclusive with --infile.z--base_packager   z;Base package path of apitools (defaults to apitools.base.py)defaultr   z--protorpc_packagezapitools.base.protorpclitezEBase package path of protorpc (defaults to apitools.base.protorpclitez--version-identifierzVersion identifier to use for the generated client (defaults to "version" value in discovery doc). This must be a valid identifier when used in a Python module name.z--outdirr-   z<Directory name for output files. (Defaults to the API name.)z--overwriteF
store_truez>Only overwrite the output directory if this flag is specified.)r   actionr   z--root_packagezCPython import path for where these modules should be imported from.z--strip_prefix*z]Prefix to strip from type names in the discovery document. (May be specified multiple times.))nargsr   r   z	--api_keyzAPI key to use for API access.z--client_jsonzTUse the given file downloaded from the dev. console for client_id and client_secret.z--client_id	CLIENT_IDz*Client ID to use for the generated client.z--client_secretCLIENT_SECRETz'Client secret for the generated client.z--scopezKScopes to request in the generated client. May be specified more than once.z--user_agentx_Tw5K8nnjoRAqULM9PFAC2bzEUser agent for the generated client. Defaults to <api>-generated/0.1.z--generate_cligenerate_clizIgnored.)r   r   r   z--nogenerate_clistore_falsez--init-file)rs   emptyr.   c                 "    | j                         S r   )lower)ss    r   <lambda>zmain.<locals>.<lambda>)  s
    qwwyrw   r.   z>Controls whether and how to generate package __init__.py file.)choicestyper   r   z--unelidable_request_methodsztFull method IDs of methods for which we should NOT try to elide the request type. (Should be a comma-separated list.)r   r   r   z--apitools_versionr4   zzApitools version used as a requirement in generated clients. Defaults to version of apitools used to generate the clients.)r   r   r   z--experimental_capitalize_enumsz:Dangerous: attempt to rewrite enum values to be uppercase.z--experimental_name_conventionz6Dangerous: use a particular style for generated names.)r   r   r   z--experimental_proto2_outputz-Dangerous: also output a proto2 message file.zType of generated codeclientz.Generate apitools client in destination folder)funcpip_packagez$Generate apitools client pip packageprotozGenerate apitools client protosrz   r   )sysargvargparseArgumentParseradd_mutually_exclusive_groupadd_argumentr   r   r5   NAME_CONVENTIONSDEFAULT_NAME_CONVENTIONadd_subparsers
add_parserset_defaultsrv   r   r   
parse_argsr   )r   r   discovery_group
subparsersclient_parserpip_package_parserproto_parserr%   s           r   mainr      s   |xx$$46F 99;O    ! "
   2 ! 4
 "J  L
 ,8  : >  @ K  M
 lM  O
 )  + 3  5 .  1 -  /
 9  ;
 6  8
 1  3 *1  3 ~l      . M  O &'K	  M +N  P )lI  K
 (

++

22E	  G &l<  >
 &&,D&EJ))G * IMN3#..B / D##);#<((7 ) 9L=1T!"X&D99T?arw   __main__r   )__doc__r   r	   r   r!   r=   r   r   r   apitools.base.pyr   apitools.genr   r   r   r'   rR   rV   r[   rc   ri   rl   ro   rv   r   r   Actionr   r   r   exitr   rw   r   <module>r      s   " ,   	   	  
 ' ' <00h(+
0,#"

009x 9Q h zCHHTV rw   