
    $3                       S 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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\R8                  " SS9 " S S5      5       rS rS rg)z+Utility wrappers around apitools generator.    )annotationsN)
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)NoDefaultApiError4   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   4   s    Cr   r   c                      \ rS rSrSrSrg)WrongDiscoveryDocError8   zUnexpected discovery doc.r   Nr   r   r   r   r   r   8   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   GenerateApitoolsApir9   <   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>a   s     ?)>A)>s   _)r"   r)   )
snake_cases    r   
_CamelCaserD   `   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.AdminDirectoryV1rB   r   messagesr;   )
class_pathclient_classpathmessages_modulepathbase_url)r"   rD   _GetClientClassFromDefBASE_URL	Exceptionr   ApitoolsClientDef)
r0   r1   r2   rJ   rK   rL   rM   client_full_classpathclient_classpath_defapitools_defs
             r   _MakeApitoolsClientDefrU   d   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.rF      )fromlist)rsplit
__import__getattr)rR   module_pathclient_class_name
module_objs       r   rN   rN      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.rF   Ngapic_wrappers)rJ   )r"   r)   r   GapicClientDef)r0   r1   r2   gapic_root_packagerJ   s        r   _MakeGapicClientDefrd      sS    xx 2 23 7 <=xx+XCE*			
 r   c           
         0 nU R                  5        Hc  u  p#UR                  5        HJ  u  pEUR                  (       d  [        U5      S:X  d  M'  X!;   a  [        SR	                  U5      5      eXAU'   ML     Me     [        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  p#UR                  5        H  u  pEUR                  =n(       aB  [        UR                  X$5      n	[         R"                  R%                  U5      n
U
R&                  nOSn	SnUR(                  (       a  [+        UR                  X$5      nOSnXAU   :H  nUR,                  nUR.                  =(       d    Sn[0        R2                  " U	UXX5      Xr   U'   M     M     U$ )a8  Combines package_map and api_config maps into ApiDef map.

Args:
  apis_config_map: {api_name->api_version->ApiConfig},
                   description of each api.
Returns:
  {api_name->api_version->ApiDef()}.

Raises:
  NoDefaultApiError: if for some api with multiple versions
      default was not specified.
rW   z0Multiple default client versions found for [{}]!z+No default client versions found for [{0}]!z, Nr;   )itemsdefaultlenr   r!   setkeys
differencer"   sortedcollectionsdefaultdictdictr.   rU   r0   r   DiscoveryDocFromJson	endpointsgcloud_gapic_libraryrd   enable_mtlsmtls_endpoint_overrider   APIDef)apis_config_mapdefault_versions_mapr1   api_version_configr2   r3   apis_without_defaultapis_mapdoc_dataapitools_clientr.   regional_endpointsgapic_clientrg   rt   ru   s                   r   _MakeApiMapr      s    &5&;&;&="h#5#;#;#=			s#56!;+!@vh! ! *5X& $> '> 
/


 !,,-A-F-F-HI 
I#VDIIf5I.J$KLN N $$T*(&5&;&;&="h#5#;#;#=--	-	-0##X
 +77@@J*44!		(	(*##X< X >>g**k)@@FB(/


 6)Lh%% $> '>. 
/r   T)frozenc                  f    \ rS rSr% SrS\S'   S\S'   S\S'   S\S	'   S\S
'   S\S'   \S 5       rSrg)	ApiConfig   aT  Configuration for an API.

root_package: dot notation of where client is generated
discovery_doc: full path to where discovery doc is located
gcloud_gapic_library: build target of gcloud gapic library
enable_mtls: whether to enable mtls
mtls_endpoint_override: mtls endpoint override
default: whether this is the default version of the API
strr0   z
str | Noner.   rs   boolrt   ru   rg   c                .   UR                  S5      =n(       a#  Ub   [        R                  R                  X45      nOSnU " UR	                  SS5      UUR                  S5      UR                  SS5      UR                  S5      UR                  S	S
5      S9$ )zCreates an ApiConfig from regen config data.

Args:
  data: yaml data from regen config
  root_dir: where the api clients are generated
  discovery_doc_dir: where the discovery docs are generated
r.   N/rF   rs   rt   Tru   rg   F)r0   r.   rs   rt   ru   rg   )r   r$   r%   r"   replace)clsdataroot_dirdiscovery_doc_dirdoc_namer.   s         r   FromDataApiConfig.FromData   s     XXo.	.	.%ggll#4?mm%%c3/#!XX&<=HH]D1#xx(@AE* r   r   N)	r   r   r   r   r   __annotations__classmethodr   r   r   r   r   r   r      s>     ""$$- r   r   c                X   [         R                  R                  [        R                  5      u  p#[
        R                  " US-   5      n[        R                  " [         R                  R                  [         R                  R                  [        5      S5      S9n[        R                  " SU 5        [        U5      n[        R                  " SU5        [
        R                  " U 5       n[        R                   " XtUS9nUR#                  U5        SSS5        g! , (       d  f       g= f)zCreate an apis_map.py file for the given packages and api_config.

Args:
    output_file: Path of the output apis map file.
    apis_config_map: {api_name->api_version->ApiConfig}, 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_filerw   api_def_filenamerB   r   tplapi_mapapis_map_filectxs	            r   GenerateApiMapr      s     (()9)9:))*:U*BC.ww||BGGOOH5~F	# 
--*K8('	--/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   )rm   rM   docs_urlNz'Removing existing resource module at %s),r   rp   rq   r2   r&   r+   r1   r   r!   GetResourceCollectionsri   namer   add
isinstancero   
flat_pathsupdatesixstring_typesDEFAULT_PATH_NAMErj   MakeResourceCollectionr    r$   r%   r"   existsmakedirsr'   r	   r   r   r   r   r   r   r   rl   rM   r   r   r*   remove)r.   r/   r1   r2   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   rm   dataclassesr&   r$   apitools.genr   googlecloudsdk.api_lib.regenr   r   googlecloudsdk.core.utilr   makor   r	   r   r-   rP   r   r   r9   rD   rU   rN   rd   r   	dataclassr   r   r   r   r   r   <module>r      s    2 "    	 # 0 ; *   
 &D	 D"Y "!=H@:05p d#' ' $'T4O"r   