
    &                        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\" 1 Sk5      r " S S\R2                  5      rS rS rS rSS jrS rS r S r!S r"S r#g)zbUtilities for meta generate-command.

Contains utilities for file writing and template selection.
    )absolute_import)division)unicode_literalsN)apis)
exceptions)log)	resources)
console_io)files)runtime)templatez_template.tpl>   list_template.tplcreate_template.tpldelete_template.tpldescribe_template.tplget_iam_policy_template.tplset_iam_policy_template.tplc                   ,   ^  \ 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      7lib/googlecloudsdk/command_lib/meta/generate_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                    [        U 5      n[        R                  " US   US   5      n[        X25      nUR	                  U5        [
        R                  " [
        R                  R                  [
        R                  R                  [        5      S5      5       HC  nUR                  S5      S   [        ;  a  M!  [        XRUU5      nU(       d  M7  [        XRU5        ME     g)zWrites declarative YAML file for all supported command types.

Args:
  collection_name: name of collection to generate commands for.
  output_dir: path to the directory where generated YAML files will be
    written.
api_nameapi_versioncommand_templates/N)_MakeCollectionDictr   GetMessagesModule_MakeApiDictupdateoslistdirpathjoindirname__file__splitCRUD_TEMPLATES	WriteYamlWriteScenarioTest)collection_name
output_dircollection_dictapi_message_moduleapi_dictcommand_templateshould_write_tests          r   WriteAllYamlrE   5   s     (8/--oj.I.=m.LN,>("**ggll277??8,.ABDc"2&n<!"2Z"46(:FDr!   c                    U S[        [        5      *  nSR                  UR                  S5       Vs/ s H  oUR	                  5       PM     sn5      nUS:X  a  SnSR                  US   R                  S5       Vs/ s H  n[        U5      PM     sn5      nXv-   S-   nUS	-   n	S
n
[        U5       H  nX:X  d  X:X  d  M  Sn
M     [        U 5      nUS-   n[        R                  R                  X-5      n[        R                  R                  U5      nS
nU(       a#  [        R                  " S
SSR                  US9S9nU(       a  U(       aq  U
(       aj  [        R                  " U5       n[         R"                  " U40 UD6nUR%                  U5        SSS5        [&        R(                  R+                  SU-   5        g[&        R(                  R+                  SU-   5        g
s  snf s  snf ! , (       d  f       N^= f)a  Writes command's YAML file; returns True if file written, else False.

Args:
  command_tpl_name: name of command template file
  collection_dict: a mapping of collection info to feed template
  output_dir: path to directory in which to write YAML file. If command YAML
  file already exists in this location, the user will be prompted to
  choose to override it or not.
  api_message_module: the API's message module, used to check if command
  type is supported by API
Returns:
  True if declarative file is written, False if user chooses not to
  override an existing file OR API does not support command type, and no
  new file is written.
N _describeGetr>   .RequestInsertRequestFTz.yamlz{command_filename} already exists, and continuing will overwrite the old file. The scenario test skeleton file for this command will only be generated if you continue)command_filename)defaultthrow_if_unattendedr   zNew file written at zNo new file written at )lenTEMPLATE_SUFFIXr7   r:   
capitalize_GetResourceMessageClassNamedir_TemplateFileForCommandPathr4   r6   existsr
   PromptContinuer   r   
FileWriterr   Contextrender_contextr   statusPrint)command_tpl_namer@   r?   rA   command_namewordcommand_name_capitalizedcollection_prefixexpected_message_namealt_create_message_namecommand_supportedmessage_namecommand_yaml_tplrN   full_command_pathfile_already_exists	overwritefctxs                      r   r<   r<   L   s   " ""8C$8#89,WW%1%7%7%<=%<T%<=?Z$gg!"34::3??$ #4(?  ,FR-?,-l,0W . 11AB!G+ggll:@'89))) 99?- :@ :/0I 0A			+	,OOA11c%%c* 
- JJ+.??@JJ.1BBCG >2 
-	,s   G!;G&*)G++
G9c                 v   [        SSS9nU S[        [        5      *  S-   n[        R                  R                  X$5      n[        R                  " U5       n[        R                  " U40 UD6nUR                  U5        SSS5        [        R                  R                  SU-   5        g! , (       d  f       N1= f)zWrites declarative YAML file for command.

Args:
  command_tpl_name: name of command template file
  collection_dict: a mapping of collection info to feed template
  test_output_dir: path to directory in which to write YAML test file
zscenario_unit_test_template.tplT)testNz.scenario.yamlzNew test written at )rV   rQ   rR   r4   r6   r7   r   rY   r   rZ   r[   r   r\   r]   )r^   r@   test_output_dirtest_tpltest_filenamefull_test_pathrk   rl   s           r   r=   r=      s     )'d4("#9S%9$9:=MM-77<<?.'1
//!
/
/CC  ( **)N:; ('s   )B**
B8c                     U(       a  SnOSn[         R                  R                  [         R                  R                  [        5      UU 5      n[
        R                  " US9$ )zReturns Mako template corresping to command_template_filename.

Args:
  command_template_filename: name of file containing template (no path).
  test: if the template file should be a test file, defaults to False.
test_templatesr-   )filename)r4   r6   r7   r8   r9   r   Template)command_template_filenamern   template_dirtemplate_paths       r   rV   rV      sK     
#L&L'',,ggooh!- 
		M	22r!   c                     U SS $ )zLReturns singular of plural noun.

Args:
  plural_noun: noun, str, to make .
Nr/   r"   )plural_nouns    r   _MakeSingularr|      s     
Sb	r!   c                      SU ;   a  gSU ;   a  gg)zuReturns a string representation of release tracks.

Args:
  api_version: API version to generate release tracks for.
alphaz[ALPHA]betaz[ALPHA, BETA]z[ALPHA, BETA, GA]r"   )r,   s    r   _GetReleaseTracksr      s     r!   c                 B   [         R                  R                  U 5      n0 nXS'   UR                  US'   UR                  R	                  5       US'   UR
                  nU(       d  SOSUS'   UR                  US'   [        UR                  5      US'   UR                  R                  S	5      S
   US'   [        US   5      US'   SR                  UR                   Vs/ s H  nXBS   S4;  d  M  SU-   S-   U-   PM     sn5      US'   XS'   SU ;   a  SOSUS'   U$ s  snf )zReturns a dictionary of collection attributes from Registry.

Args:
  collection_name: Name of collection to create dictionary about.
r>   r+   uppercase_api_namefalsetrueuse_relative_namer,   release_tracksrK   r/   plural_resource_namesingular_name projectz--z=my-flagslocationparent)r	   REGISTRYGetCollectionInfor+   rS   
flat_pathsr,   r   namer:   r|   r7   params)r>   collection_infor@   r   params        r   r0   r0      sS    &&88I//'6#$ / 8 8/**9*B*B*M*M*O/&'))*8B/%&#2#>#>/- &7!!'#/"#,;,@,@,F,Fs,KB,O/()%2,-&///" XX"))')%
8)D
D $dUlVe#)' /'
 (7#$ !+o =*9 	's   D0Dc                 $   0 n [        U [        US   5      5      nUR                  5        Vs/ s H'  nUR                  S   S:w  d  M  UR                  S   PM)     nnU VVs0 s HH  nUSR	                  [
        R                  " SU5       Vs/ s H  owR                  5       PM     sn5      _MJ     snnUS'   U$ s  snf s  snf s  snnf ! [         a:    0 US'   [        R                  R                  S[        US   5      -   S-   5         U$ f = f)zReturns a dictionary of API attributes from its messages module.

Args:
  message_module: the messages module for the API (default version)
  collection_dict: a dictionary containing collection info from registry
r   r   -z^[a-z]*|[A-Z][a-z]*create_argszCannot find z in message module.)getattrrT   
all_fields__dict__r7   refindalllowerAttributeErrorr   r\   r]   )message_moduler@   rB   resource_messagefieldargsargws           r   r2   r2      s9    (,~;#2?#C EF
 &0022E>>&!V+ 	v2 	   C 	RZZ0Es%KL%K'')%KLM	NH] 
/! M
 
 , H]JJ^1(9;; ++ , 
/,sM   *C B;	B;C #+CC 'C2C ;C  CC A DDc                     U R                  5       n[        U5      S:  a  US   R                  5       USS -   $ UR                  5       $ )z5Returns the properly capitalized resource class name.   r   N)striprQ   upperrS   )r   resource_names     r   rT   rT      sL    %%'-!!!#mAB&777		!	!	##r!   )F)$r'   
__future__r   r   r   os.pathr4   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   core_exceptionsr   r	   googlecloudsdk.core.consoler
   googlecloudsdk.core.utilr   makor   r   rR   	frozensetr;   Errorr   rE   r<   r=   rV   r|   r   r0   r2   rT   r"   r!   r   <module>r      s   
 '  '  	 , = # ) 2 *  !  ;o33 ;G.6r<$3"@>$r!   