
    .                         d Z ddlmZ ddlmZ ddlm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d	lmZ dd
lmZ ddlmZ ddlZdZ G d de      Z G d de      Zd Zd Zd Zd Zd Zd Zd Zd Zy)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                       e Zd ZdZy)NoDefaultApiErrorz;Multiple apis versions are specified but no default is set.N__name__
__module____qualname____doc__     ,lib/googlecloudsdk/api_lib/regen/generate.pyr   r   5   s    Cr   r   c                       e Zd ZdZy)WrongDiscoveryDocErrorzUnexpected discovery doc.Nr   r   r   r   r   r   9   s    !r   r   c                 p   t         j                  g}|j                  d      }|r2|j                  dj	                  dj                  |d                      |j                  dddj	                  |       dj	                  t        j                  j                  |||            dd	d
dj	                  |      ddj	                  |||      dg       t        j                  d|       t        j                  |       t        j                  j                  |      \  }}	|	||fD ]  }
t        j                  j                  ||
      }t        j                  j                  |d      }t        j                  j                  |      rct        j                  d|       t        j                   |t"                y)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   GenerateApitoolsApir7   =   sf    

	$)~~.JKKK29989:< = ++M*BGGLLX{KL#% ''4<;?  
--!4(//$''--
3+x8[1f'',,{F3K[-8I77>>)$ooI!#i);< 2r   c                 P    dj                  d | j                  d      D              S )N c              3   <   K   | ]  }|j                           y w)N)
capitalize).0xs     r   	<genexpr>z_CamelCase.<locals>.<genexpr>b   s     ?)>A)>s   _)r    r'   )
snake_cases    r   
_CamelCaserA   a   s"    	?)9)9#)>?	??r   c                 x   dj                  | ||g      }|dk(  r|dk(  rd}n:dj                  dj                  ||dg      t        |      t        |      z   g      }dj                  ||dg      }d}|dz   |z   }	 t        |      }|j                  }t        j                  ||||	      }	|	S # t        $ r Y &w xY w)
zMakes an ApitoolsClientDef..adminv1z admin_v1_client.AdminDirectoryV1r?   r   messagesr9   )
class_pathclient_classpathmessages_modulepathbase_url)r    rA   _GetClientClassFromDefBASE_URL	Exceptionr   ApitoolsClientDef)
r.   r/   r0   rG   rH   rI   rJ   client_full_classpathclient_classpath_defapitools_defs
             r   _MakeApitoolsClientDefrR   e   s    xxx=>*[D09xx(K238z+66!8 9 (K!DE($s*-==	12GH#,,H
 **'-	,
 
 
 		s   ;B- -	B98B9c                 `    | j                  dd      \  }}t        ||g      }t        ||      S )zFReturns the client class for the API definition specified in the args.rC      )fromlist)rsplit
__import__getattr)rO   module_pathclient_class_name
module_objs       r   rK   rK      s8    #8#?#?Q#G + +1B0CD*	.	//r   c                     dj                  | j                  d      dd       }dj                  |d||g      }t        j                  |      S )zMakes a GapicClientDef.rC   Ngapic_wrappers)rG   )r    r'   r   GapicClientDef)r.   r/   r0   gapic_root_packagerG   s        r   _MakeGapicClientDefra      sU    xx 2 23 7 <=xx+X{CE*			
 r   c           	         i }|j                         D ]`  \  }}|j                         D ]H  \  }}|j                  d      st        |      dk(  s&||v rt        dj	                  |            |||<   J b t        |j                               j                  |j                               }|r2t        dj	                  dj                  t        |                        t        j                  t              }|j                         D ]  \  }}|j                         D ]  \  }}| j                  |i       j                  |d      )| |   |   }	|j                  d      rt        |	||      }
nd}
|j                  d      rt        |	||      }nd}|||   k(  }|j                  d	d
      }|j                  dd      }t        j                   |
||||      ||   |<     |S )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.
  defaultrT   z0Multiple default client versions found for [{}]!z+No default client versions found for [{0}]!z, Nr,   gcloud_gapic_libraryenable_mtlsTmtls_endpoint_overrider9   )itemsr   lenr   r   setkeys
differencer    sortedcollectionsdefaultdictdictrR   ra   r   APIDef)package_mapapis_configdefault_versions_mapr/   api_version_configr0   r1   apis_without_defaultapis_mapr.   apitools_clientgapic_clientrc   re   rf   s                  r   _MakeApiMapry      s     &1&7&7&9"h"#5#;#;#=Z			"c*<&=&B++!@vh! ! *5X& $> ': 
+


(()=)B)B)DE 
I#VDIIf5I.J$KLN N $$T*(&1&7&7&9"h"#5#;#;#=Z	2	&	*	*;	=	E *;7l		(0(K
 	.	/*<;O 4X >>gNN=$7k)~~.FK(/


; 6)8hx%# $> ':, 
/r   c                 T   t         j                  j                  t        j                        \  }}t        j                  |dz         }t        j                  t         j                  j                  t         j                  j                  t              d            }t        j                  d|        t        ||      }t        j                  d|       t        j                  |       5 }t        j                   |||      }	|j#                  |	       ddd       y# 1 sw Y   yxY w)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_sourcerv   N)r"   r#   splitextr   r   r	   ReadFileContentsr   Templater    dirnamer$   r%   ry   
FileWriterr
   Contextrender_context)
output_filerq   r1   api_def_filenamer?   r}   tplapi_mapapis_map_filectxs
             r   GenerateApiMapr      s     (()9)9:A))*:U*BC.ww||BGGOOH5~F	# 
--*K8Z0'	--/9$
//nwC s	 %$$s   +*DD'c                    t         j                  j                  |       } | j                  |k7  r!t	        j
                  d| j                  |       | j                  |k7  r%t        dj                  | j                  |            | j                  ||      }|rKt        g       }|D ]  }|j                  |v s||j                     j                  dd      }|s4|j                  |j                         ||j                     d   }	t        |	t              r|j                   j#                  |	       t        |	t$        j&                        s|	|j                   t         j(                  <    t        |j+                               |z
  D ]X  }
||
   }|d   }|j                  dd      }|s"|j                  dd      }| j-                  |
|||      }|j/                  |       Z t0        j2                  j5                  |||      }t0        j2                  j7                  |      st1        j8                  |       t0        j2                  j5                  |d      }|rt	        j:                  d|       t=        j>                  t0        j2                  j5                  t0        j2                  jA                  tB              d	      
      }tE        jF                  |      5 }tI        jJ                  |tM        |      |d   jN                  | jP                        }|jS                  |       ddd       yt0        j2                  jU                  |      r,t	        j:                  d|       t1        jV                  |       yy# 1 sw Y   yxY w)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   rJ   docs_urlNz'Removing existing resource module at %s),r   DiscoveryDocFromJsonr0   r$   r)   r/   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   rJ   r   r   r(   remove)r,   r-   r/   r0   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*
	,	,.z?CC!4
 #
joo.&z7?k4(



&
&{
3S%5%566A 

 224 + /4467:KK'8n&v.o*..
 !)--.BDI%<<
?,>Mo!!/2 L GGLLX{;'		 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M;;N)r   
__future__r   r   r   rm   r$   r"   apitools.genr   googlecloudsdk.api_lib.regenr   r   googlecloudsdk.core.utilr	   makor
   r   r   r+   rM   r   r   r7   rA   rR   rK   ra   ry   r   r   r   r   r   <module>r      s    2 &  '   	 # 0 ; *   
 &D	 D"Y "!=H@:06r6O"r   