
    .                         S r SSKJ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J	r	  SSK
Jr  SSK
Jr  SS	KJr  SS
KJr  SSKJr  SSKrSr " S S\5      r " S S\5      rS rS rS rS rS rS rS rS rg)z+Utility wrappers around apitools generator.    )absolute_import)division)unicode_literalsN)
gen_client)api_def)resource_generator)files)runtime)templatean  # -*- coding: utf-8 -*- #
# Copyright 2016 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

c                       \ rS rSrSrSrg)NoDefaultApiError5   z;Multiple apis versions are specified but no default is set. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       ,lib/googlecloudsdk/api_lib/regen/generate.pyr   r   5   s    Cr   r   c                       \ rS rSrSrSrg)WrongDiscoveryDocError9   zUnexpected discovery doc.r   Nr   r   r   r   r   r   9   s    !r   r   c                    [         R                  /nUR                  S5      nU(       a2  UR                  SR	                  SR                  US   5      5      5        UR                  SSSR	                  U 5      SR	                  [        R                  R                  XU5      5      SS	S
SR	                  U5      SSR	                  X#U5      S/5        [        R                  " SU5        [         R                  " U5        [        R                  R                  U5      u  pXU4 H  n
[        R                  R                  X5      n[        R                  R                  US5      n[        R                  R                  U5      (       a  Mh  [        R                  " SU5        [        R                   " U["        5        M     g)z)Invokes apitools generator for given api.unelidable_request_methodsz --unelidable_request_methods={0},z--init-file=emptyz--nogenerate_cliz--infile={0}z--outdir={0}z--overwritez--apitools_version=CloudSDKz--user_agent=google-cloud-sdkz--version-identifier={0}z--root_packagez{0}.{1}.{2}clientzApitools gen %sz__init__.pyz1%s does not have __init__.py file, generating ...N)r   __file__getappendformatjoinextendospathloggingdebugmainsplitisfilewarningr	   WriteFileContents_INIT_FILE_CONTENT)discovery_doc
output_dirroot_packageapi_nameapi_version
api_configargsr   package_dirdir_namesubdir	init_files               r   GenerateApitoolsApir;   =   s_    

	$)~~.JKKK29989:< = ++M*BGGLL{KL#% ''4<;?  
--!4(//$''--
3+[1f'',,{3K[-8I77>>)$$ooI!#i);< 2r   c                 P    SR                  S U R                  S5       5       5      $ )N c              3   @   #    U  H  oR                  5       v   M     g 7f)N)
capitalize).0xs     r   	<genexpr>_CamelCase.<locals>.<genexpr>b   s     ?)>A)>s   _)r$   r+   )
snake_cases    r   
_CamelCaserF   a   s"    	?)9)9#)>?	??r   c                 t   SR                  XU/5      nUS:X  a	  US:X  a  SnO9SR                  SR                  XS/5      [        U5      [        U5      -   /5      nSR                  XS/5      nSnUS-   U-   n [        U5      nUR                  n[
        R                  " UUUUS	9n	U	$ ! [         a     N%f = f)
zMakes an ApitoolsClientDef..adminv1z admin_v1_client.AdminDirectoryV1rD   r   messagesr=   )
class_pathclient_classpathmessages_modulepathbase_url)r$   rF   _GetClientClassFromDefBASE_URL	Exceptionr   ApitoolsClientDef)
r2   r3   r4   rL   rM   rN   rO   client_full_classpathclient_classpath_defapitools_defs
             r   _MakeApitoolsClientDefrW   e   s    xx=>*[D09xx(238z+66!8 9 (!DE($s*-==	12GH#,,H
 **'-	,
 
 
 		s   :B* *
B76B7c                 T    U R                  SS5      u  p[        X/S9n[        X25      $ )zFReturns the client class for the API definition specified in the args.rH      )fromlist)rsplit
__import__getattr)rT   module_pathclient_class_name
module_objs       r   rP   rP      s/    #8#?#?Q#G ++0CD*		//r   c                     SR                  U R                  S5      SS 5      nSR                  USX/5      n[        R                  " US9$ )zMakes a GapicClientDef.rH   Ngapic_wrappers)rL   )r$   r+   r   GapicClientDef)r2   r3   r4   gapic_root_packagerL   s        r   _MakeGapicClientDefrf      sS    xx 2 23 7 <=xx+XCE*			
 r   c           	         0 nUR                  5        Hh  u  p4UR                  5        HO  u  pVUR                  S5      (       d  [        U5      S:X  d  M,  X2;   a  [        SR	                  U5      5      eXRU'   MQ     Mj     [        UR                  5       5      R                  UR                  5       5      nU(       a2  [        SR	                  SR                  [        U5      5      5      5      e[        R                  " [        5      nUR                  5        H  u  p4UR                  5        H  u  pVU R                  U0 5      R                  US5      c  M*  X   U   n	UR                  S5      (       a  [        XU5      n
OSn
UR                  S5      (       a  [        XU5      nOSnXRU   :H  nUR                  S	S
5      nUR                  SS5      n[        R                   " U
UXU5      X   U'   M     M     U$ )a  Combines package_map and api_config maps into ApiDef map.

Args:
  package_map: {api_name->api_version->root_package},
               package where each generated api resides.
  apis_config: {api_name->api_version->{discovery,default,version,...}},
               description of each api.
Returns:
  {api_name->api_version->ApiDef()}.

Raises:
  NoDefaultApiError: if for some api with multiple versions
      default was not specified.
defaultrY   z0Multiple default client versions found for [{}]!z+No default client versions found for [{0}]!z, Nr0   gcloud_gapic_libraryenable_mtlsTmtls_endpoint_overrider=   )itemsr!   lenr   r#   setkeys
differencer$   sortedcollectionsdefaultdictdictrW   rf   r   APIDef)package_mapapis_configdefault_versions_mapr3   api_version_configr4   r5   apis_without_defaultapis_mapr2   apitools_clientgapic_clientrh   rj   rk   s                  r   _MakeApiMapr~      s     &1&7&7&9"h#5#;#;#=			"	"c*<&=&B+!@vh! ! *5X& $> ': 
+


(()=)B)B)DE 
I#VDIIf5I.J$KLN N $$T*(&1&7&7&9"h#5#;#;#=	2	&	*	*;	=	E *;7l		(	(0K
 	.	/	/*<;OX >>gNN=$7k)~~.FK(/


 6)8h%# $> ':, 
/r   c                 X   [         R                  R                  [        R                  5      u  p4[
        R                  " US-   5      n[        R                  " [         R                  R                  [         R                  R                  [        5      S5      S9n[        R                  " SU 5        [        X5      n[        R                  " SU5        [
        R                  " U 5       n[        R                   " XUS9n	UR#                  U	5        SSS5        g! , (       d  f       g= f)a  Create an apis_map.py file for the given packages and api_config.

Args:
    output_file: Path of the output apis map file.
    package_map: {api_name->api_version->root_package}, packages where the
      generated APIs reside.
    api_config: regeneration config for all apis.
z.pyztemplate.tplfilenamezGenerating api map at %szCreating following api map %s)api_def_sourcer{   N)r&   r'   splitextr   r    r	   ReadFileContentsr   Templater$   dirnamer(   r)   r~   
FileWriterr
   Contextrender_context)
output_filerv   r5   api_def_filenamerD   r   tplapi_mapapis_map_filectxs
             r   GenerateApiMapr      s     (()9)9:))*:U*BC.ww||BGGOOH5~F	# 
--*K80'	--/9$
//wC s	 %$$s   +'D
D)c                 x   [         R                  R                  U 5      n U R                  U:w  a"  [        R
                  " SU R                  U5        U R                  U:w  a%  [        SR                  U R                  U5      5      eU R                  XC5      nU(       Gae  [        / 5      nU H  nUR                  U;   d  M  XGR                     R                  SS5      nU(       d  M<  UR                  UR                  5        XGR                     S   n	[        U	[        5      (       a  UR                   R#                  U	5        M  [        U	[$        R&                  5      (       d  M  U	UR                   [         R(                  '   M     [        UR+                  5       5      U-
   H\  n
XJ   nUS   nUR                  SS5      nU(       d  M'  UR                  SS5      nU R-                  XX5      nUR/                  U5        M^     [0        R2                  R5                  XU5      n[0        R2                  R7                  U5      (       d  [0        R8                  " U5        [0        R2                  R5                  US5      nU(       a  [        R:                  " SU5        [<        R>                  " [0        R2                  R5                  [0        R2                  RA                  [B        5      S	5      S
9n[D        RF                  " U5       n[H        RJ                  " U[M        U5      US   RN                  U RP                  S9nURS                  U5        SSS5        g[0        R2                  RU                  U5      (       a.  [        R:                  " SU5        [0        RV                  " U5        gg! , (       d  f       g= f)a  Create resource.py file for given api and its discovery doc.

Args:
    discovery_doc: str, Path to the discovery doc.
    output_dir: str, Path to the base output directory (module will be
      generated underneath here in api_name/api_version subdir).
    api_name: str, name of the api.
    api_version: str, the version for the api.
    custom_resources: dict, dictionary of custom resource collections.
Raises:
  WrongDiscoveryDocError: if discovery doc api name/version does not match.
zYDiscovery api version %s does not match %s, this client will be accessible via new alias.zapi name {0}, expected {1}apitools_compatibleTr'   enable_uri_parsingzresources.pyz Generating resource module at %szresources.tplr   r   )rr   rO   docs_urlNz'Removing existing resource module at %s),r   DiscoveryDocFromJsonr4   r(   r-   r3   r   r#   GetResourceCollectionsrn   namer!   add
isinstancert   
flat_pathsupdatesixstring_typesDEFAULT_PATH_NAMEro   MakeResourceCollectionr"   r&   r'   r$   existsmakedirsr)   r   r   r   r    r	   r   r
   r   rq   rO   r   r   r,   remove)r0   r1   r3   r4   custom_resourcesresource_collectionsmatched_resources
collectionr   custom_pathcollection_namecollection_defcollection_pathr   collection_infoapi_dirresource_file_namer   r   r   s                       r   GenerateApitoolsResourceModuler      s   ( %11::=I-+-OO	8!!;0 x'
 !=!D!D"* + +&==%B*
	,	,.?CC!4
 #
joo.&7?k4((



&
&{
3S%5%5666A 

 224 + /4467:KK'8n&v.o*..
 !)--.BDI%<<
,>Mo!!/2 L GGLL{;'		 	 KKww||G^<MM46HI


RWW\\"''//(2K2A&C DC			,	-OOK(./C(D%9!%<%E%E%2%;%;=c 
 
.	- 
ww~~())MM;=OPII ! * 
.	-s   A	N++
N9)r   
__future__r   r   r   rr   r(   r&   apitools.genr   googlecloudsdk.api_lib.regenr   r   googlecloudsdk.core.utilr	   makor
   r   r   r/   rR   r   r   r;   rF   rW   rP   rf   r~   r   r   r   r   r   <module>r      s    2 &  '   	 # 0 ; *   
 &D	 D"Y "!=H@:06r6O"r   