
    -                     >   S 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SK	r	SSK
Jr  SS	KJr  SSKrS
rSR#                  \S9r\	R&                  " \5      rSr " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rS rS rS rg)zResource definition generator.    )absolute_import)division)print_function)unicode_literals)OrderedDictN)resource)filesz)[a-zA-Z][a-zA-Z0-9_]+(?:\.[a-zA-Z0-9_]+)+z!(?P<collection>{collection})\.get)
collection c                       \ rS rSrSrSrg)Error&   zErrors raised by this module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       6lib/googlecloudsdk/api_lib/regen/resource_generator.pyr   r   &   s    %r   r   c                       \ rS rSrSrSrg)NoMatchingMethodError*   z,Raised when no matching method can be found.r   Nr   r   r   r   r   r   *   s    4r   r   c                       \ rS rSrSrSrg)UnsupportedDiscoveryDoc.   z1Raised when some unsupported feature is detected.r   Nr   r   r   r   r   r   .   s    9r   r   c                       \ rS rSrSrSrg)ConflictingCollection2   z>Raised when collection names conflict and need to be resolved.r   Nr   r   r   r   r    r    2   s    Fr   r    c                       \ rS rSrSrS r\S 5       r\S 5       r	\S 5       r
\S 5       r\S 5       rS	 rS
 rS r SS jrS rS rSrg)DiscoveryDoc6   z%Encapsulates access to discovery doc.c                     Xl         g N_discovery_doc_dict)selfdiscovery_doc_dicts     r   __init__DiscoveryDoc.__init__9   s    1r   c                     [         R                  " U5       nU " [        R                  " U[        S95      sS S S 5        $ ! , (       d  f       g = f)N)object_pairs_hook)r	   
FileReaderjsonloadr   )clspathfs      r   FromJsonDiscoveryDoc.FromJson<   s1    			$	11<= 
 		s   A  
Ac                      U R                   S   $ )Nnamer'   r)   s    r   api_nameDiscoveryDoc.api_nameA   s    ##F++r   c                      U R                   S   $ )Nversionr'   r9   s    r   api_versionDiscoveryDoc.api_versionE       ##I..r   c                      U R                   S   $ )NbaseUrlr'   r9   s    r   base_urlDiscoveryDoc.base_urlI   r@   r   c                      U R                   S   $ )NdocumentationLinkr'   r9   s    r   docs_urlDiscoveryDoc.docs_urlM   s    ##$788r   c                    U R                  X R                  5      nU(       a  [        R                  " US   R                  5      S   nU H  nU[        R                  " UR                  5      S   :w  d  M,  [        SR                  UR                  UUS   R                  US   R                  UR                  UR                  5      5      e   UR                  U R                  X1U5      5        U$ )a  Returns all resources collections found in this discovery doc.

Args:
  custom_resources: {str, str}, A mapping of collection name to path that
      have been registered manually in the yaml file.
  api_version: Override api_version for each found resource collection.

Returns:
  list(resource_util.CollectionInfo).

Raises:
  UnsupportedDiscoveryDoc: if collections have different base URLs.
r      zWIn client {0}/{1}, collection {2} is using url {3}, but collection {4} is using url {5})_ExtractResourcesr(   resource_utilSplitEndpointUrlrC   r   formatr:   r8   extend!_GenerateMissingParentCollections)r)   custom_resourcesr>   collectionsurl_api_versioncs         r   GetResourceCollections#DiscoveryDoc.GetResourceCollectionsQ   s     ((--/K%66
a.
!
!##$&o!m<<QZZHKK'006**a.%%a.))&&**1   ..;	89 r   c                    / nUR                  S5      (       aK  UR                  S5      nUR                  S5      nU(       a"  U R                  X5      nUR                  U5        UR                  S5      (       aJ  UR                  S5      R                  5        H'  u  pxU R	                  X5      n	UR                  U	5        M)     U$ )z0Extract resource definitions from discovery doc.methodsget	resources)rY   _GetCollectionFromMethodappenditemsrK   rO   )
r)   r>   infosrR   rX   
get_methodcollection_info_infosubresource_collectionss
             r   rK   DiscoveryDoc._ExtractResourcest   s    Kyy		)$g;;u%j	77%?+yyYY{+113'!"&"8"8"K23 4 r   c                     [        US   5      nUR                  SS5      S   nUR                  S5      nUR                  S5      nU R                  XXT5      $ )z>Created collection_info object given discovery doc get_method.id.rJ   flatPathr3   )_ExtractCollectionNamesplitrY   _MakeResourceCollection)r)   r>   r_   collection_name	flat_pathr3   s         r   r[   %DiscoveryDoc._GetCollectionFromMethod   s^    ,Z-=>O &++C3A6Oz*I>>&!D''d r   Nc                 l   XC:X  a  SnU R                   U-   n[        R                  " U5      u  pgnU(       a&  [        R                  " U R                   U-   5      u    ptUS[        U5      *  n[        R                  " UUUU R
                  UUU(       a  [        U0O0 [        R                  " U5      5      $ )z8Make resource collection object given its name and path.N)rC   rL   rM   lenCollectionInforG   DEFAULT_PATH_NAMEGetParamsFromPath)r)   r>   rl   r3   rm   urlurl_api_namera   s           r   rk   $DiscoveryDoc._MakeResourceCollection   s     i
--$
C)::3?LT%66
--)
#oa kD	z
C''*3	I&''-	 	r   c                    U Vs0 s H  oDR                   U_M     nnU Vs1 s H  oDR                  [        5      iM     nn/ n[        U5      n/ n	0 n
U(       a  U H  n[	        U5      u  pU(       d  M  X;   a  M   X;   a+  U
R                  U[        5       5      R                  U5        MP  X;   aE  [        SR                  UR                  X4R                   UX[   R                  [        5      US95      eU R                  XSU5      nU	R                  U5        XU'   UR                  U5        M     UR                  U	5        U	n/ n	U(       a  M  [        R                  " U
5       HX  u  p[!        U5      S:  a  M  UR#                  5       nUX.   S   :X  d  M3  [%        SR                  XR                  U5      5        MZ     U$ s  snf s  snf )a  Generates parent collections for any existing collection missing one.

Args:
  collections: [resource.CollectionInfo], The existing collections from the
    discovery doc.
  custom_resources: {str, str}, A mapping of collection name to path that
    have been registered manually in the yaml file.
  api_version: Override api_version for each found resource collection.

Raises:
  ConflictingCollection: If multiple parent collections have the same name
    but different paths, and a custom resource has not been declared to
    resolve the conflict.

Returns:
  [resource.CollectionInfo], Additional collections to include in the
  resource module.
zIn API [{api}/{version}], the parent of collection [{c}] is not registered, but a collection with [{parent_name}] and path [{existing_path}] already exists. Update the api config file to manually add the parent collection with a path of [{parent_path}].)apir=   rT   parent_nameexisting_pathparent_pathTrJ   r3   z:WARNING: Custom resource [{}] in API [{}/{}] is redundant.)r8   GetPathrr   list_GetParentCollection
setdefaultsetaddr    rN   r:   MakeResourceCollectionr\   rO   six	iteritemsrp   popprint)r)   rR   rQ   r>   rT   	all_names	all_paths	generatedin_progress
to_processignoredry   r{   parent_collectionr8   pathsr3   s                    r   rP   .DiscoveryDoc._GenerateMissingParentCollections   s   * %00KqKI07BC{!,-{ICI{#KJG
 !#7#: 
#
*


[#%
0
4
4[
A
# &! "(jj+)(001BC)	 "( "+	, 	, !77dK9+,!2+k"7 : z"kjE +L }}W-	Ua 	YY[d	!'/	/Kt]]K8	; . o 1Cs
   GG	c                 0   [         R                  " U R                  5      u  pVnU(       a  U R                  nOSR                  U R                  U5      n[         R                  " U R
                  XGU R                  X0 [         R                  " U5      U5	      $ )Nz{}{}/)rL   rM   rC   rN   rq   r:   rG   rs   )r)   rl   r3   enable_uri_parsingr>   ra   rS   rC   s           r   r   #DiscoveryDoc.MakeResourceCollection   sr    )::4==IAh{;h''{dmmr=#B#B4#H r   r'   r&   )r   r   r   r   r   r+   classmethodr5   propertyr:   r>   rC   rG   rU   rK   r[   rk   rP   r   r   r   r   r   r#   r#   6   s    -2 > > , , / / / / 9 9!F"
 ;?8L\
r   r#   c                     [         R                  U 5      nU(       a  UR                  S5      $ [        SR	                  U [
        5      5      e)z4Extract the name of the collection from a method ID.r
   z%Method {0} does not match regexp {1}.)_METHOD_ID_REmatchgroupr   rN   _METHOD_ID_RE_RAW)	method_idr   s     r   ri   ri     sA    


i
(%
;;|$$
/		,	-/ /r   c                    U R                  [        5      n[        U5      S:  a  gU R                  [        5      nUR	                  S5      n[        US5        [        US5        U(       d  gSR                  U5      n[        US5        U(       d  gSU R                  ;   a!  U R                  R                  SS5      u  pVXT4$ US   nXT4$ )	zGenerates the name and path for a parent collection.

Args:
  collection_info: resource.CollectionInfo, The collection to calculate the
    parent of.

Returns:
  (str, str), A tuple of parent name and path or (None, None) if there is no
  parent.
   )NN/TFrg   rJ   )		GetParamsrr   rp   r|   rj   _PopSegmentsjoinr8   rsplit)r`   paramsr3   partsr{   ry   ra   s          r   r~   r~     s     $$%67&[1_		 	 !2	3$ **S/%udue	+ ud	O    %))00a8NK
 
	!! )K		!!r   c                    U (       a~  U S   R                  S5      U:X  ae  U S   R                  S5      U:X  aL  U R                  5         U (       d  g U S   R                  S5      U:X  a  U S   R                  S5      U:X  a  MJ  g g g g g )Nr   {})
startswithendswithr   )r   	is_paramss     r   r   r   A  s    
9$	19c"i/iik	 9$	19c"i/ 2/ 2 r   ) r   
__future__r   r   r   r   rR   r   r0   regooglecloudsdk.api_lib.utilr   rL   googlecloudsdk.core.utilr	   r   _COLLECTION_SUB_RErN   r   compiler   rr   	Exceptionr   r   r   r    objectr#   ri   r~   r   r   r   r   <module>r      s    % &  % ' #  	 A * 
 B 8??! @ # 

,- &I &5E 5:e :GE GO6 Od/+"\r   