
    )>                     >   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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.    )absolute_import)division)unicode_literalsN)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#   WriteConfigYamlr<   0   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/   r0   r   r   
FileWriterr   Contextrender_context)	file_pathfile_templatecontextr:   render_file	overwritefctxs           r#   _RenderFilerM   J   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@   rA   rB   r   rC   write)rF   file_contentsr:   rK   s       r#   
_WriteFilerQ   a   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>i   s      )5/3^.ts   )tuplesplitr@   rA   join)r7   sdk_pathhome_directoryargv	path_argsrF   s         r#   _BuildFilePathr`   g   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@   rA   dirname__file__r[   r   Template)template_file_namedir_nametemplate_pathrG   s       r#   _BuildTemplaterj   o   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_COMPONENTSr]   rj   _BuildCommandGroupInitContextrM   r7   r8   r;   r9   r:   rF   rG   rH   s           r#   r4   r4   w   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 )Nrl   export.yamlzcommand_template.tpl)r`   rn   r]   rj   _BuildCommandContextrM   rp   s           r#   r5   r5      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.tplrl   z
GROUP.yamlrr   zsurface_spec_template.tplN)_BuildSurfaceSpecContextrj   r`   _SPEC_PATH_COMPONENTSr]   rM   )
r7   r8   r;   r9   r:   rH   group_templategroup_file_path	spec_pathspec_templates
             r#   r3   r3      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.rm   zpython_blank_init_template.tplzconfig_export_test.pyzunit_test_template.tplN)_BuildTestContextr`   _TEST_PATH_COMPONENTSr]   rj   rM   )	r7   r8   r;   r:   rH   	init_pathinit_template	test_pathtest_templates	            r#   r6   r6      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.{}   , r9   group_category)datetimenowyearr   Brandinggetapi_name
capitalizer	   $convert_collection_name_to_delimitedname	enumerater   upperlenr   )r;   r9   r8   	init_dictrelease_track_stringxrelease_tracks          r#   ro   ro      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 _rS   r   z{}.resources:{}resource_argument_namer9   aaeiouanapi_a_or_anresource_a_or_an)r   r   r   r   r   r   r	   r   r   replacer]   rZ   r   _MakeResourceArgName_GetReleaseTracks)r;   r9   r8   command_dictresource_spec_nameresource_spec_dirr   r   s           r#   rs   rs      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.r9   surface_spec_resource_namesurface_spec_resource_argr   :)r   r   r   rZ   r   _MakeSurfaceSpecResourceArg)r;   r9   r8   surface_spec_dicts       r#   ru   ru   
  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-longrS   full_collection_nametest_command_string)r   r   r   _MakeResourceArgFlagsr   r   r[   r   r   _MakeTestCommandStringr]   )r;   r8   	test_dictresource_arg_flagsresource_arg_positionals        r#   r|   r|     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   r[   sortedr   )r9   trackrelease_tracks_normalizeds      r#   r   r   1  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   )r;   s    r#   r   r   <  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   -rS   r   )r   r   )r]   s    r#   r   r   B  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   H  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   rZ   r   r	   split_name_on_capitalssingularizelowerappendr   itemsranger   r   r   r   r   r[   )r;   r8   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   O  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+   
__future__r   r   r   r   os.pathr@   googlecloudsdk.corer   r   core_exceptionsr   r	   r
   googlecloudsdk.core.utilr   makor   r   rn   rv   r}   Errorr   r<   rM   rQ   r`   rj   r4   r5   r3   r6   ro   rs   ru   r|   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%   