
    ;                        S r SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKrSSKJr  SSKJs  Js  Jr  SSKJs  Js  Jr   " S S\5      r " S S	\5      rS
 r " S S\5      r\R<                  SS j5       rS r S r! " S S\RD                  " SS5      5      r#S r$S r% " S S\5      r&S r'S r(S r)SS jr*g)z4Assorted utilities shared between parts of apitools.    )print_function)unicode_literalsN)urllib_parsec                       \ rS rSrSrSrg)Error%   z#Base error for apitools generation. N__name__
__module____qualname____firstlineno____doc____static_attributes__r	       $lib/third_party/apitools/gen/util.pyr   r   %   s    -r   r   c                       \ rS rSrSrSrg)CommunicationError*   zError in network communication.r	   Nr
   r	   r   r   r   r   *   s    )r   r   c                     [        U 5      * U 4$ N)len)as    r   _SortLengthFirstKeyr   /   s    F7A:r   c                       \ rS rSrSrSr/ SQr  SS jr\SS j5       r	\SS j5       r
\SS	 j5       rS
 r\S 5       r\S 5       rS rSS jrSS jrS rSrg)Names3   zBUtility class for cleaning and normalizing names in a fixed style.LOWER_CAMEL)r   LOWER_WITH_UNDERNONENc                 j    [        U[        S9U l        U=(       d    U R                  U l        X0l        g )N)key)sortedr   _Names__strip_prefixesDEFAULT_NAME_CONVENTION_Names__name_convention_Names__capitalize_enums)selfstrip_prefixesname_conventioncapitalize_enumss       r   __init__Names.__init__9   s0     !'~;N O;t;; 	"2r   c                 X    [         R                  " SSU-  U 5      n U R                  5       $ )Nz([a-z0-9])([A-Z])z\1%s\2)resublowername	separators     r   __FromCamelNames.__FromCamelA   s&    vv*I	,A4Hzz|r   c                 P    SR                  S U R                  U5       5       5      $ )N c              3   R   #    U  H  oS S R                  5       USS -   v   M     g7f)r      N)upper).0ss     r   	<genexpr>"Names.__ToCamel.<locals>.<genexpr>J   s'     M7L!1v||~!"-7L   %')joinsplitr2   s     r   	__ToCamelNames.__ToCamelF   s"     wwMtzz)7LMMMr   c                 Z    [         R                  XS9n U S   R                  5       U SS  -   $ )Nr4   r   r:   )r   _Names__ToCamelr1   r2   s     r   __ToLowerCamelNames.__ToLowerCamelL   s-    t9Aw}}ab))r   c                     U(       d  U$ U R                    H)  nUR                  U5      (       d  M  U[        U5      S s  $    U$ )z%Strip strip_prefix entries from name.N)r$   
startswithr   )r(   r3   prefixs      r   __StripNameNames.__StripNameQ   sA    K++Fv&&CKL)) , r   c                 <   [         R                  " SSU 5      n U S   R                  5       (       a  SU -  n [        R                  " U 5      (       d  U S:X  a*  SU -  n [        R                  " U 5      (       a  M"  U S:X  a  M*  U R                  S5      (       a  SU -  n U $ )	zPerform generic name cleaning.z[^_A-Za-z0-9]_r   z_%sexecz%s___zf%s)r/   r0   isdigitkeyword	iskeywordrK   )r3   s    r   	CleanNameNames.CleanNameZ   s     vvosD17??4<D%%4<D %% ??4  4<Dr   c           	         U R                  S5      n/ nU H\  n[        R                  " SU5      (       a,  UR                  S[        R                  USS 5      -  5        MK  UR                  U5        M^     SR                  U5      $ )z$Normalize camelCase entries in path./z{[A-Za-z0-9_]+}$z{%s}r:   )rB   r/   matchappendr   rV   rA   )pathpath_componentsnormalized_components	components       r   NormalizeRelativePathNames.NormalizeRelativePathh   sz     **S/ "(Ixx+Y77%,,U__Yq_==? &,,Y7 ) xx-..r   c                 f    U R                   (       a  UR                  5       nU R                  U5      $ r   )r'   r;   rV   )r(   	enum_names     r   NormalizeEnumNameNames.NormalizeEnumNameu   s&    ""!)I~~i((r   c                     Uc  U$ UR                  S5      (       a  U$ U R                  U5      nU R                  XS9nU R                  U5      $ )z&Generate a valid class name from name.)z	protorpc.zmessage_types.zapitools.base.protorpclite.z)apitools.base.protorpclite.message_types.rF   )rK   _Names__StripNamerG   rV   r(   r3   r4   s      r   	ClassNameNames.ClassNamez   s_     <K ?? I J J K%~~d~8~~d##r   c                 Z    Uc  g[         R                  XS9n[         R                  U5      $ )z'Generate a valid method name from name.NrF   )r   rG   rV   ri   s      r   
MethodNameNames.MethodName   s)    <t9t$$r   c                     U R                  U5      nU R                  S:X  a  [        R                  U5      nO%U R                  S:X  a  [        R	                  U5      n[        R                  U5      $ )z&Generate a valid field name from name.r   r   )rh   r&   r   _Names__ToLowerCamel_Names__FromCamelrV   )r(   r3   s     r   	FieldNameNames.FieldName   s`    
 %!!]2''-D##'99$$T*Dt$$r   )__capitalize_enums__name_convention__strip_prefixes)NF)rP   )r   r   r   r   r   r%   NAME_CONVENTIONSr,   staticmethodrq   rG   rp   rh   rV   ra   re   rj   rm   rr   r   r	   r   r   r   r   3   s    L+B "&"'3   N N
 * *   
/ 
/)
$%
%r   r   c              #   l  #    [         R                  R                  U 5      (       d+  U(       d  [        SU -  5      e[         R                  " U 5        [         R
                  " 5       n [         R                  " U 5        S v   [         R                  " U5        g ! [         R                  " U5        f = f7f)NzCannot find directory %s)osr]   existsOSErrormkdirgetcwdchdir)dirnamecreateprevious_directorys      r   Chdirr      sr     77>>'""4w>??HHW%

#$#$s   A%B4(B B4B11B4c                 &    U R                  SS5      $ )N.rP   )replace)versions    r   NormalizeVersionr      s    ??3$$r   c                     [         R                  " X#5      nSR                  XS45      nXT;  a  US4$ UR                  U5      u  pgnXe-   U4$ )a	  Compute the base url and base path.

Attributes:
  package: name field of the discovery, i.e. 'storage' for storage service.
  version: version of the service, i.e. 'v1'.
  root_url: root url of the service, i.e. 'https://www.googleapis.com/'.
  service_path: path of the service under the rool url, i.e. 'storage/v1/'.

Returns:
  base url: string, base url of the service,
    'https://www.googleapis.com/storage/v1/' for the storage service.
  base path: string, common prefix of service endpoints after the base url.
rY   r8   )r   urljoinrA   
rpartition)	packager   root_urlservice_path	full_pathapi_path_componentrL   rP   suffixs	            r   _ComputePathsr      s\     $$X<I7R"89*"}!,,-?@Fv&..r   c                       \ rS rSrSr\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S 5       r\S	 5       r\S
 5       r\S 5       rSrg)
ClientInfo   z,Container for client-related info and names.c	                    [        UR                  S0 5      R                  S0 5      R                  S0 5      5      n	U	R                  U5        US   n
U=(       d    [        US   5      nUS   n[	        XUS   US   5      u  pUR                  SS	5      nS	nU(       a  [	        XUUS   5      u  nnU
UU[        [        U	5      5      UUUUUUUS
.nUR                  US   5      < UR                  US   5      < 3nUUS'   U " S0 UD6$ )z9Create a new ClientInfo object from a discovery document.authoauth2scopesr3   r   rootUrlservicePathmtlsRootUrlr8   )r   r   url_versionr   	client_idclient_secret
user_agentapi_keybase_url	base_pathmtls_base_urlr   client_class_namer	   )setgetupdater   r   r#   listrj   )clsdiscovery_docscope_lsr   r   r   namesr   version_identifierr   r   r   r   r   r   mtls_root_urlr   rP   client_infor   s                       r   CreateClientInfo.Create   sD   
 fb)--h;??"MOh'L"2=3K"L 	#I.+G,9),D,9-,HJ &))-<,W-:-:=-I KM1
 &T&\*"*$ "*
 OOK	23OOK	235 ,='(![!!r   c                     U R                   $ r   )r   r(   s    r   default_directoryClientInfo.default_directory   s    ||r   c                 >    U R                   < SU R                  < S3$ )NrP   _clientr   r   r   s    r   client_rule_nameClientInfo.client_rule_name   s    !%t||<<r   c                      SU R                   -  $ Nz%s.py)r   r   s    r   client_file_nameClientInfo.client_file_name   s    ....r   c                 >    U R                   < SU R                  < S3$ )NrP   	_messagesr   r   s    r   messages_rule_nameClientInfo.messages_rule_name      #'<<>>r   c                 >    U R                   < SU R                  < S3$ )NrP   	_servicesr   r   s    r   services_rule_nameClientInfo.services_rule_name  r   r   c                      SU R                   -  $ r   r   r   s    r   messages_file_nameClientInfo.messages_file_name  s    0000r   c                      SU R                   -  $ Nz%s.protor   r   s    r   messages_proto_file_name#ClientInfo.messages_proto_file_name      D3333r   c                      SU R                   -  $ r   )r   r   s    r   services_proto_file_name#ClientInfo.services_proto_file_name  r   r   r	   N)r   r   r   r   r   classmethodr   propertyr   r   r   r   r   r   r   r   r   r	   r   r   r   r      s    
 7'" '"R   = = / / ? ? ? ? 1 1 4 4 4 4r   r   )r   r   r   r   r   r   r   r   r   r   r   r   c                    ^ SSSSSSSSSSSSS	.mU4S
 jnSR                  U  Vs/ s H
  o!" U5      PM     sn5      $ s  snf )z@Returns s with unicode homoglyphs replaced by ascii equivalents. r8   z(C)z(R)-'"z...)       ãr      ©   ®u   —u   ‘u   ’u   “u   ”u   …u   ⸺c                    > TR                  U 5      nUb  U$  U R                  S5        U $ ! [         a     Of = f U R                  S5      R                  S5      $ ! [         a     gf = f)z3Returns the homoglyph or escaped replacement for c.asciizunicode-escape?)r   encodeUnicodeErrordecode)cequiv
homoglyphss     r   _ReplaceOne&ReplaceHomoglyphs.<locals>._ReplaceOne*  sv    q!L	HHWH 			88,-44W== 		s   , 
99A 
A*)A*)rA   )r=   r   r   r   s      @r   ReplaceHomoglyphsr     s\     J 77A.AqKNA.//.s   >c                    [        U [        R                  5      (       d  U $ [        R                  (       a6  U R	                  SS5      n U R	                  SS5      n U R	                  SS5      n [        U 5      n U R	                  SS5      $ )	zAReturn a version of description safe for printing in a docstring.z\Nz\\Nz\uz\\uz\Uz\\Uz"""z" " ")
isinstancesixstring_typesPY3r   r   )descriptions    r   CleanDescriptionr   <  su    k3#3#344
ww!))%9!))%9!))%9#K0Kug..r   c                       \ rS rSrSrS r\S 5       rSS jr\	R                  SS j5       r\	R                  S 5       rS rS	rg
)SimplePrettyPrinteriI  z=Simple pretty-printer that supports an indent contextmanager.c                 :    Xl         SU l        SU l        SU l        g )Nr8   F)_SimplePrettyPrinter__out_SimplePrettyPrinter__indent_SimplePrettyPrinter__skip%_SimplePrettyPrinter__comment_context)r(   outs     r   r,   SimplePrettyPrinter.__init__M  s    
!&r   c                     U R                   $ r   r   r   s    r   indentSimplePrettyPrinter.indentS  s    }}r   c                 2    U[        U R                  5      -
  $ r   )r   r   )r(   	max_widths     r   CalculateWidth"SimplePrettyPrinter.CalculateWidthW  s    3t{{+++r   c              #   R   #    U R                   nU< U< 3U l         S v   X l         g 7fr   r   )r(   r   previous_indents      r   IndentSimplePrettyPrinter.IndentZ  s"     --"16:'r@   c              #   F   #    U R                   nSU l         Sv   Xl         g7f)z&Print without any argument formatting.TN)r   )r(   old_contexts     r   CommentContext"SimplePrettyPrinter.CommentContexta  s#      ,,!%!,s   !c                 "   U R                   (       a  USS  (       a  [        S5      eU(       ay  US   (       ao  U R                   (       d  US   USS  -  R                  5       nOUS   R                  5       n[        U5      n [	        U R
                  < U< 3U R                  S9  g [	        SU R                  S9  g ! [         aH    UR                  SS5      R                  S5      n[	        U R
                  < U< 3U R                  S9   g f = f)Nr:   z1Cannot do string interpolation in comment contextr   )filer   backslashreplacer8   )
r   r   rstripr   printr   r   UnicodeEncodeErrorr   r   )r(   argslines      r   __call__SimplePrettyPrinter.__call__i  s    !!d12hKLLDG))Q$qr(*224Aw~~'$T*DGt44::F
 "4::&	 & G{{7,>?FFwOt44::FGs   $B< <ADD)__comment_context__indent__out__skipN)N   )z  )r   r   r   r   r   r,   r   r   r  
contextlibcontextmanagerr  r
  r  r   r	   r   r   r   r   I  s\    G'  , ( ( - -'r   r   c                     U R                  S5      (       a  U /$ SU ;  a  [        S5      eU R                  S5      u  pnSU< SU< S3SU< SU< 3/$ )	z5Expands a few abbreviations into full discovery urls.httpr   z)Unrecognized value "%s" for discovery urlz-https://www.googleapis.com/discovery/v1/apis/rY   z/restzhttps://z(.googleapis.com/$discovery/rest?version=)rK   
ValueError	partition)discovery_urlapi_namerP   api_versions       r   _NormalizeDiscoveryUrlsr$  {  sf    ''	M	!DEE,66s;H k	# k	# r   c                    [         R                  " SSSS9n UR                  U 5        UR                  5         [        R
                  " UR                  S5       nUR                  5       nSSS5        W[        R                  " UR                  5        $ ! , (       d  f       N0= f! [        R                  " UR                  5        f = f)z0Returns gunzipped content from gzipped contents.gzzw+bF)r   modedeleterbN)
tempfileNamedTemporaryFilewriteclosegzipopenr3   readrz   unlink)gzipped_contentfhdecompressed_contents       r   _Gunzipr6    s    ##4eEJA	 		YYqvvt$#$668  %#
		!&&	 %$ 			!&&s$   AB& B+	B& 
B#B& &"Cc                     [         R                  " U 5      nUR                  5       R                  S5      nUS:X  a  [	        UR                  5       5      nU$ UR                  5       nU$ )z'Download and return the content of URL.zContent-Encodingr.  )urllib_requesturlopeninfor   r6  r0  )urlresponseencodingcontents       r   _GetURLContentr?    sZ    %%c*H}}""#56H6(--/* N --/Nr   c                    [        U 5      nSnSnU Hj  n[        U5       HX  n [        U5      n[        U[        5      (       a  UR                  S5      n[        R                  " U5      nU(       a  Us  s  $ MZ     Ml     Uc  [        SU< SU< 35      eg! [        R                  [        R                  4 a$  n[        R                  " SU5        Un SnAM  SnAff = f)z.Fetch the discovery document at the given url.Nutf8z2Attempting to fetch discovery doc again after "%s"z'Could not find discovery doc at any of z: )r$  ranger?  r   bytesr   jsonloadsurllib_error	HTTPErrorURLErrorloggingr:  r   )	r!  retriesdiscovery_urlsr   last_exceptionr;  rP   r>  es	            r   FetchDiscoveryDocrN    s    ,];NMNwA
#(-gu--%nnV4G $

7 3 (( !     01 	1 	 !**L,A,AB #H!M!"#s   AB$C;CC)T)   )+r   
__future__r   r   collectionsr  r.  rD  rT   rI  rz   r/   r*  r   	six.movesr   six.moves.urllib.errormovesurlliberrorrF  six.moves.urllib.requestrequestr8  	Exceptionr   r   r   objectr   r  r   r   r   
namedtupler   r   r   r   r$  r6  r?  rN  r	   r   r   <module>r\     s   " ; % '       	 	  
 " - - 1 1.I .
* *
g%F g%T % %%
/,O4'' 72 3 O4d 0F
//'& /'d
1r   