
    =                        S 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Jr  SS
KJr  SrSrSr " S S\R&                  5      rS rS rS rS rS rS rS rS rS rS rS rS r S r!S r"S r#S r$S  r%S! r&g)"z,Utilities for meta generate-config-commands.    N)branding)
exceptions)log)name_parsing)	resources)files)runtime)template)third_partypygooglecloudsdksurface)cloudsdksurface_specsgcloud)r   r   r   testsunitr   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )CollectionNotFoundError$   z8Exception for attempts to generate unsupported commands.c                 J   > SR                  US9n[        [        U ]  U5        g )Nz${collection} collection is not found)
collection)formatsuperr   __init__)selfr   message	__class__s      >lib/googlecloudsdk/command_lib/meta/generate_config_command.pyr    CollectionNotFoundError.__init__'   s+    4;; < G	
!41':     )__name__
__module____qualname____firstlineno____doc__r   __static_attributes____classcell__)r   s   @r    r   r   $   s    @; ;r"   r   c                    [         R                  R                  SR                  U 5      5        [        R
                  R                  U 5      n[        XXSU5        [        XXSU5        [        XUX45        [        XUU5        g)a  Writes <comand|spec|test> declarative command files for collection.

Args:
  collection: Name of collection to generate commands for.
  output_root: Path to the root of the directory. Should just be $PWD when
    executing the `meta generate-config-commands` command.
  resource_data: Resource map data for the given resource.
  release_tracks: Release tracks to generate files for.
  enable_overwrites: True to enable overwriting of existing config export
    files.
z[{}]:N)r   statusPrintr   r   REGISTRYGetCollectionInfo_RenderSurfaceSpecFiles_RenderCommandGroupInitFile_RenderCommandFile_RenderTestFiles)r   output_rootresource_datarelease_tracksenable_overwritescollection_infos         r    WriteConfigYamlr9   -   sv     **7>>*-.&&88D/+);LNk-/1 [#8;$&r"   c                    SnSn[         R                  R                  U 5      (       d  SnOU(       a  SnSnU(       au  [        R                  R                  SR                  X5      5        [        R                  " U SS9 n[        R                  " U40 UD6nUR                  U5        SSS5        g[        R                  R                  SR                  U 5      5        g! , (       d  f       g= f)zERenders a file to given path using the provided template and context.FTz+ -- Generating: File: [{}], Overwrite: [{}]create_pathNz >> Skipped: File: [{}] --)ospathexistsr   r,   r-   r   r   
FileWriterr	   Contextrender_context)	file_pathfile_templatecontextr7   render_file	overwritefctxs           r    _RenderFilerJ   G   s    
 +)			"	"KKIJJBII 			)	6!OOA))c""3' 
7	6 JJ188CD	 
7	6s   )C""
C0c                     [         R                  R                  U 5      (       a  U(       a0  [        R                  " U SS9 nUR                  U5        S S S 5        g g ! , (       d  f       g = f)NTr;   )r=   r>   r?   r   r@   write)rC   file_contentsr7   rH   s       r    
_WriteFilerN   ^   sJ    			"	"&7			)	6!ggm 
7	6 '8	6	6s   A
A+c                     U 4U-   [        UR                  S5      5      -   [        S U 5       5      -   n[        R                  R                  " U6 nU$ )N.c              3   $   #    U  H  ov   M     g 7f)Nr#   ).0path_components     r    	<genexpr>!_BuildFilePath.<locals>.<genexpr>f   s      )5/3^.ts   )tuplesplitr=   r>   join)r4   sdk_pathhome_directoryargv	path_argsrC   s         r    _BuildFilePathr]   d   s\    nx'%3+! !#( )5/3)5 $55) ggllI&)	r"   c                     [         R                  R                  [        5      n[         R                  R	                  USU 5      n[
        R                  " US9nU$ )Nconfig_export_templates)filename)r=   r>   dirname__file__rX   r
   Template)template_file_namedir_nametemplate_pathrD   s       r    _BuildTemplaterg   l   sD    WW__X&('',,x)B13-##];-	r"   c                     [        U [        UR                  SS5      n[        S5      n[	        X#U5      n[        XVXt5        g )Nconfig__init__.pyzcommand_group_init_template.tpl)r]   _COMMAND_PATH_COMPONENTSrZ   rg   _BuildCommandGroupInitContextrJ   r4   r5   r8   r6   r7   rC   rD   rE   s           r    r1   r1   t   sE    [*B*998*,) !!BC-)/*79'iCr"   c                     [        U [        UR                  SS5      n[        S5      n[	        X#U5      n[        UUUU5        g )Nri   export.yamlzcommand_template.tpl)r]   rk   rZ   rg   _BuildCommandContextrJ   rm   s           r    r2   r2      sL    [*B*99%}6) !!78- -P'	r"   c                     [        X#U5      n[        S5      n[        U [        UR                  SS5      n[        XvXT5        [        U [        UR                  SS5      n[        S5      n	[        XXT5        g)zBRender surface spec files (both GROUP.yaml and command spec file.)zsurface_spec_group_template.tplri   z
GROUP.yamlro   zsurface_spec_template.tplN)_BuildSurfaceSpecContextrg   r]   _SPEC_PATH_COMPONENTSrZ   rJ   )
r4   r5   r8   r6   r7   rE   group_templategroup_file_path	spec_pathspec_templates
             r    r0   r0      s     %_%24' ""CD.";0E#0#?#?#/1/ owJ [*?*998*,) !!<=-iCr"   c                     [        X!5      n[        U [        UR                  S5      n[	        S5      n[        XVXC5        [        U [        UR                  S5      n[	        S5      n[        XxXC5        g)z3Render python test file using template and context.rj   zpython_blank_init_template.tplzconfig_export_test.pyzunit_test_template.tplN)_BuildTestContextr]   _TEST_PATH_COMPONENTSrZ   rg   rJ   )	r4   r5   r8   r7   rE   	init_pathinit_template	test_pathtest_templates	            r    r3   r3      st     o=' [*?*99=J) !AB-iC [*?*9946) !!9:-iCr"   c                    0 nSUS'   [         R                   R                  5       R                  US'   [        R                  " 5       R                  U R                  U R                  R                  5       5      US'   [        R                  " U R                  5      US'   Sn[        U5       H@  u  pVUSR                  UR                  5       5      -  nU[        U5      S-
  :w  d  M;  US	-  nMB     XCS
'   SU;   a  UR                  US'   U$ )zAMakes context dictionary for config init file template rendering.-*- coding: utf-8 -*- #utf_encodingcurrent_yearbranded_api_name"singular_resource_name_with_spaces zbase.ReleaseTrack.{}   , r6   group_category)datetimenowyearr   Brandinggetapi_name
capitalizer   $convert_collection_name_to_delimitedname	enumerater   upperlenr   )r8   r6   r5   	init_dictrelease_track_stringxrelease_tracks          r    rl   rl      s    )7)N&//335::)N"*"3"3"5"9"9 8 8 C C E#G) /;._._


/  *, #N3a299-:M:M:OPPC!##d" 4
 !5&"/">">I	r"   c                    0 nU R                   US'   [        R                  " 5       R                  U R                  U R                  R                  5       5      US'   [        R                  " U R                   SS9US'   [        R                  " U R                   5      US'   US   R                  5       US'   SU;   a  UR                  US'   OGUS   R                  S	S
5      nUR                  R                  S5      S   nSR                  XT5      US'   [        U R                   5      US'   [        U5      US'   SnUS   S   S;   a  SnXcS'   SnUS   S   S;   a  SnXsS'   U$ )zFMakes context dictionary for config export command template rendering.collection_namer   F)make_singular plural_resource_name_with_spacessingular_name_with_spacessingular_capitalized_nameresource_spec_path _rP   r   z{}.resources:{}resource_argument_namer6   aaeiouanapi_a_or_anresource_a_or_an)r   r   r   r   r   r   r   r   r   replacerZ   rW   r   _MakeResourceArgName_GetReleaseTracks)r8   r6   r5   command_dictresource_spec_nameresource_spec_dirr   r   s           r    rp   rp      s   , %4$8$8, !%-%6%6%8%<%< 8 8 C C E&G,!"
 -9,],]


e-5 (* &2%V%V


&  !#
 /;!/##-:< *+ ]* - @ @  &&ABJJS%44::3?B):)A)A*/L%& ,@,,'( $5^#D,  +$%a(G3K +}-.q1W<%5!"	r"   c                     0 n[        U5      US'   SU;   a  UR                  US'   U$ SU;   a1  UR                  R                  S5      S   R	                  5       US'   U$ [        U 5      US'   U$ )z4Makes context dictionary for surface spec rendering.r6   surface_spec_resource_namesurface_spec_resource_argr   :)r   r   r   rW   r   _MakeSurfaceSpecResourceArg)r8   r6   r5   surface_spec_dicts       r    rr   rr     s    (9.(I$% "]2'4'O'O #% 
 },'4'G'G'M'M((UW #% 
 (C( #% 
r"   c                    0 nSUS'   [         R                   R                  5       R                  US'   [        X5      n[	        U R
                  5      nSR                  XC/5      US'   SUS'   [        US   5      S:  a  S	US'   S
R                  U R                  U R
                  /5      US'   [        UR                  5      US'   U$ )z?Makes context dictionary for config export est files rendering.r   r   r   r   test_command_argumentsr   pylint_disable8   z   # pylint:disable=line-too-longrP   full_collection_nametest_command_string)r   r   r   _MakeResourceArgFlagsr   r   rX   r   r   _MakeTestCommandStringrZ   )r8   r5   	test_dictresource_arg_flagsresource_arg_positionals        r    ry   ry     s    )7)N&//335::)N,_L01E1EF(+3)5)$% ")+	,-2"DI&)hh!5!56'8)"#%;""&$)!"	r"   c           	          SR                  SR                  [        U 5       Vs/ s H  oR                  5       PM     sn5      5      nU$ s  snf )zyReturns a string representation of release tracks.

Args:
  release_tracks: API versions to generate release tracks for.
z[{}]r   )r   rX   sortedr   )r6   trackrelease_tracks_normalizeds      r    r   r   .  sF     %mmDII"("89"8{{}"89-; <	"" :s   A
c                 \    [         R                  " U R                  SS9R                  5       $ )z:Makes resource arg name for surface specification context.r   	delimiter)r   r   r   r   )r8   s    r    r   r   9  s'    		:	:c
++0573r"   c                 d    SR                  U R                  SS5      R                  SS5      5      $ )z/Makes gcloud command string for test execution.z{} config exportr   -rP   r   )r   r   )rZ   s    r    r   r   ?  s2    		"	"S#&..sC8
: :r"   c                 N    SR                  [        R                  " U SS95      nU$ )Nzmy-{}r   r   )r   r   r   )r   resource_arg_names     r    r   r   E  s,    nn77
S*+ 
r"   c                 (   / n[        U S5      (       GaL  S[        U SS5      ;   Ga9  U R                  S   R                  S5      nU Vs/ s H-  nSU;   d  M  UR                  SS5      R                  SS5      PM/     nnU Vs/ s H  nSU;  d  M  UPM     nn/ nUSS  HK  n[        R
                  " [        R                  " U5      S	S
9R                  5       n	UR                  U	5        MM     SU;   aL  UR                  R                  5        H.  u  p[        [        U5      5       H  nX   U
:X  d  M  XU'   M     M0     U Vs/ s H  nSR                  US9PM     nnO[        U SS5      (       a  U R                   Hq  nUnUSS S:X  a  USS n[        R                  " US	SS9nU[        R                  " U R                   S	S
9SS4;  d  MQ  SR                  US9nUR                  U5        Ms     SR#                  U5      $ s  snf s  snf s  snf )z;Makes input resource arg flags for config export test file.
flat_pathsr   N/{zId}projectr   r   r   resource_attribute_renamesz--{param}=my-{param})paramparamsIdF)r   r   r   r   )getattrr   rW   r   r   split_name_on_capitalssingularizelowerappendr   itemsranger   r   r   r   r   rX   )r8   r5   r   
components	componentresource_arg_flag_namesresource_arg filtered_resource_arg_flag_names!formatted_resource_arg_flag_namesformatted_nameoriginal_attr_namenew_attr_namer   r   modified_param_names                  r    r   r   L  sa   _l++	W_lD99"--b177<j
 &!%iI 8)

C
$
,
,UB
7%  ! ,C*+B<l* +B ' * +-':3B?,%<<$$\2 57 	 	*00@	 @ 
&	61>1Y1Y1_1_ 2
->?@a037II5B2 A2
 @?l !
'
'l
'
;?  
 $// ''! 
RS	!T	)1#26 )MM
EC
 CC""c34=vGG .44;N4O!!,/% (( 
$	%%s!*0s   	
H&H
H
H
H)'r(   r   os.pathr=   googlecloudsdk.corer   r   core_exceptionsr   r   r   googlecloudsdk.core.utilr   makor	   r
   rk   rs   rz   Errorr   r9   rJ   rN   r]   rg   r1   r2   r0   r3   rl   rp   rr   ry   r   r   r   r   r   r#   r"   r    <module>r      s    3   ( = # , ) *  M C $ ;o33 ;&4E.DD*D&64n(&#3:C&r"   