
    )>                     >   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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mZ ddlmZ dZdZdZ G d dej.                        Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d  Z&d! Z'd" Z(d# Z)d$ Z*y)%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                   "     e Zd ZdZ fdZ xZS )CollectionNotFoundErrorz8Exception for attempts to generate unsupported commands.c                 P    dj                  |      }t        t        |   |       y )Nz${collection} collection is not found)
collection)formatsuperr   __init__)selfr   message	__class__s      >lib/googlecloudsdk/command_lib/meta/generate_config_command.pyr   z CollectionNotFoundError.__init__*   s+    4;; < G	
!41':    )__name__
__module____qualname____doc__r   __classcell__)r!   s   @r"   r   r   '   s    @; ;r#   r   c                    t         j                  j                  dj                  |              t        j
                  j                  |       }t        |||||       t        |||||       t        |||||       t        ||||       y)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"   WriteConfigYamlr7   0   s     **7>>*-.&&88D/+})>;LNk=-~/1 [-#%68;$&r#   c                    d}d}t         j                  j                  |       sd}n|rd}d}|rwt        j                  j                  dj                  | |             t        j                  | d      5 }t        j                  |fi |}|j                  |       ddd       yt        j                  j                  dj                  |              y# 1 sw Y   yxY w)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contextr5   render_file	overwritefctxs           r"   _RenderFilerH   J   s    
 +)			"KKIJJBII9 			)	6!OOA))c""3' 
7	6 JJ188CD	 
7	6s   5(CCc                     t         j                  j                  |       r|r2t        j                  | d      5 }|j                  |       d d d        y y # 1 sw Y   y xY w)NTr9   )r;   r<   r=   r   r>   write)rA   file_contentsr5   rF   s       r"   
_WriteFilerL   a   sG    			"&7			)	6!ggm 
7	6 '8	6	6s   AAc                     | f|z   t        |j                  d            z   t        d |D              z   }t        j                  j                  | }|S )N.c              3       K   | ]  }|  y w)N ).0path_components     r"   	<genexpr>z!_BuildFilePath.<locals>.<genexpr>i   s      )5/3^.ts   )tuplesplitr;   r<   join)r2   sdk_pathhome_directoryargv	path_argsrA   s         r"   _BuildFilePathr[   g   s\    nx'%3+! !#( )5/3)5 $55) ggllI&)	r#   c                     t         j                  j                  t              }t         j                  j	                  |d|       }t        j                  |      }|S )Nconfig_export_templates)filename)r;   r<   dirname__file__rV   r   Template)template_file_namedir_nametemplate_pathrB   s       r"   _BuildTemplatere   o   sD    WW__X&('',,x)B13-##];-	r#   c                     t        | t        |j                  dd      }t        d      }t	        |||      }t        ||||       y )Nconfig__init__.pyzcommand_group_init_template.tpl)r[   _COMMAND_PATH_COMPONENTSrX   re   _BuildCommandGroupInitContextrH   r2   r3   r6   r4   r5   rA   rB   rC   s           r"   r/   r/   w   sL    [*B*998*,) !!BC-)/>*79'i1BCr#   c                     t        | t        |j                  dd      }t        d      }t	        |||      }t        ||||       y )Nrg   export.yamlzcommand_template.tpl)r[   ri   rX   re   _BuildCommandContextrH   rk   s           r"   r0   r0      sN    [*B*99%}6) !!78- .-P'	r#   c                     t        |||      }t        d      }t        | t        |j                  dd      }t        ||||       t        | t        |j                  dd      }t        d      }	t        ||	||       y)zBRender surface spec files (both GROUP.yaml and command spec file.)zsurface_spec_group_template.tplrg   z
GROUP.yamlrm   zsurface_spec_template.tplN)_BuildSurfaceSpecContextre   r[   _SPEC_PATH_COMPONENTSrX   rH   )
r2   r3   r6   r4   r5   rC   group_templategroup_file_path	spec_pathspec_templates
             r"   r.   r.      s     %_n%24' ""CD.";0E#0#?#?#/1/ o~w8IJ [*?*998*,) !!<=-i1BCr#   c                     t        ||      }t        | t        |j                  d      }t	        d      }t        ||||       t        | t        |j                  d      }t	        d      }t        ||||       y)z3Render python test file using template and context.rh   zpython_blank_init_template.tplzconfig_export_test.pyzunit_test_template.tplN)_BuildTestContextr[   _TEST_PATH_COMPONENTSrX   re   rH   )	r2   r3   r6   r5   rC   	init_pathinit_template	test_pathtest_templates	            r"   r1   r1      s     o}=' [*?*99=J) !AB-i1BC [*?*9946) !!9:-i1BCr#   c                    i }d|d<   t         j                   j                         j                  |d<   t        j                         j                  | j                  | j                  j                               |d<   t        j                  | j                        |d<   d}t        |      D ]>  \  }}|dj                  |j                               z  }|t        |      dz
  k7  s:|d	z  }@ ||d
<   d|v r|j                  |d<   |S )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.{}   , r4   group_category)datetimenowyearr   Brandinggetapi_name
capitalizer	   $convert_collection_name_to_delimitedname	enumerater   upperlenr   )r6   r4   r3   	init_dictrelease_track_stringxrelease_tracks          r"   rj   rj      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                    i }| j                   |d<   t        j                         j                  | j                  | j                  j                               |d<   t        j                  | j                   d      |d<   t        j                  | j                         |d<   |d   j                         |d<   d|v r|j                  |d<   nH|d   j                  d	d
      }|j                  j                  d      d   }dj                  ||      |d<   t        | j                         |d<   t        |      |d<   d}|d   d   dv rd}||d<   d}|d   d   dv rd}||d<   |S )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 _rN   r   z{}.resources:{}resource_argument_namer4   aaeiouanapi_a_or_anresource_a_or_an)r   r   r   r   r   r   r	   r   r   replacerX   rU   r   _MakeResourceArgName_GetReleaseTracks)r6   r4   r3   command_dictresource_spec_nameresource_spec_dirr   r   s           r"   rn   rn      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                     i }t        |      |d<   d|v r|j                  |d<   |S d|v r1|j                  j                  d      d   j	                         |d<   |S t        |       |d<   |S )z4Makes context dictionary for surface spec rendering.r4   surface_spec_resource_namesurface_spec_resource_argr   :)r   r   r   rU   r   _MakeSurfaceSpecResourceArg)r6   r4   r3   surface_spec_dicts       r"   rp   rp   
  s    (9.(I$% "]2'4'O'O #% 
 },'4'G'G'M'M((UW #% 
 (C( #% 
r#   c                    i }d|d<   t         j                   j                         j                  |d<   t        | |      }t	        | j
                        }dj                  ||g      |d<   d|d<   t        |d         dkD  rd	|d<   d
j                  | j                  | j
                  g      |d<   t        |j                        |d<   |S )z?Makes context dictionary for config export est files rendering.r~   r   r   r   test_command_argumentsr   pylint_disable8   z   # pylint:disable=line-too-longrN   full_collection_nametest_command_string)r   r   r   _MakeResourceArgFlagsr   r   rV   r   r   _MakeTestCommandStringrX   )r6   r3   	test_dictresource_arg_flagsresource_arg_positionals        r"   rw   rw     s    )7)N&//335::)N,_mL01E1EF(+ 23)5)$% ")+	,-2"DI&)hh!5!56'8)"#%;""&$)!"	r#   c           	          dj                  dj                  t        |       D cg c]  }|j                          c}            }|S c c}w )zReturns a string representation of release tracks.

  Args:
    release_tracks: API versions to generate release tracks for.
  z[{}]r   )r   rV   sortedr   )r4   trackrelease_tracks_normalizeds      r"   r   r   1  sH     %mmDII"("89"8u{{}"89-; <	"" :s   Ac                 `    t        j                  | j                  d      j                         S )z:Makes resource arg name for surface specification context.r   	delimiter)r	   r   r   r   )r6   s    r"   r   r   <  s'    		:	:c
++0573r#   c                 d    dj                  | j                  dd      j                  dd            S )z/Makes gcloud command string for test execution.z{} config exportr   -rN   r   )r   r   )rX   s    r"   r   r   B  s2    		"	"S#&..sC8
: :r#   c                 R    dj                  t        j                  | d            }|S )Nzmy-{}r   r   )r   r	   r   )r   resource_arg_names     r"   r   r   H  s,    nn77
S*+ 
r#   c                    g }t        | d      r6dt        | dd      v r| j                  d   j                  d      }|D cg c](  }d|v r"|j                  dd      j                  dd      * }}|D cg c]  }d|vr|
 }}g }|dd D ]K  }t	        j
                  t	        j                  |      d	
      j                         }	|j                  |	       M d|v rI|j                  j                         D ],  \  }
}t        t        |            D ]  }||   |
k(  s|||<    . |D cg c]  }dj                  |       }}nt        | dd      r| j                  D ]s  }|}|dd dk(  r|dd }t	        j                  |d	d      }|t	        j                  | j                   d	
      ddfvsQdj                  |      }|j                  |       u dj#                  |      S c c}w c c}w c c}w )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   rU   r   r	   split_name_on_capitalssingularizelowerappendr   itemsranger   r   r   r   r   rV   )r6   r3   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  s_   _l+	W_lD99"--b177<j
 &!%iI 

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   -G&5G+*G0)+r'   
__future__r   r   r   r   os.pathr;   googlecloudsdk.corer   r   core_exceptionsr   r	   r
   googlecloudsdk.core.utilr   makor   r   ri   rq   rx   Errorr   r7   rH   rL   r[   re   r/   r0   r.   r1   rj   rn   rp   rw   r   r   r   r   r   rP   r#   r"   <module>r      s    3 &  '   ( = # , ) *  M C $ ;o33 ;&4E.DD*D&64n(&#3:C&r#   