
    r                     x    d 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Zd
 ZddZd Zy	)zCommon utility functions for ReplicaPool template processing.

These utility functions enable easy replacement of parameters into
ReplicaPool template files.
    )absolute_import)division)unicode_literals)arg_parsers)
exceptions)yaml)filesNc                     | j                  dt        j                  d      dd       | j                  dt        j                  d      dd	       y
)z)Adds --param and --param-from-file flags.z--param   )
min_lengthzA list of key=value parameters to substitute in the template before the template is submitted to the replica pool. This does not change the actual template file.zPARAM=VALUE)typehelpmetavarz--param-from-filezA list of files each containing a key=value parameter to substitute in the template before the template is submitted to the replica pool. This does not change the actual template file.zPARAM=FILE_PATHN)add_argumentr   ArgDict)parsers    ;lib/googlecloudsdk/api_lib/compute/replica_template_util.pyAddTemplateParamArgsr   !   s`    !,3    	!,    !    c           	      
   |xs i }|xs i }t        |      }t        j                  |      D ]:  \  }}||v rt        j                  d|z         	 t        j                  |      ||<   < t        j                  |       }t        |t               rd|vrt        j                  d      t        ||      \  }}}	|r0t        j                  ddj!                  t#        |            z         t%        |j'                               |	z
  }
|
r0t        j                  ddj!                  t#        |
            z         |S # t
        j                  $ r5}t        j                  dj                  |||j                              d}~ww xY w)	a  Parse and apply params into a template file.

  Args:
    template_file: The path to the file to open and parse.
    params: a dict of param-name -> param-value
    params_from_file: a dict of param-name -> param-file

  Returns:
    The parsed template dict

  Raises:
    yaml.Error: When the template file cannot be read or parsed.
    ArgumentError: If any params are not provided.
    ValidationError: if the YAML file is invalid.
  zDuplicate param key: z3Could not load param key "{0}" from file "{1}": {2}NtemplatezMInvalid template format.  Root must be a mapping with single "template" valuezSSome parameters were present in the template but not provided on the command line: z, zWSome parameters were specified on the command line but not referenced in the template: )dictsix	iteritemsr   DuplicateErrorr	   ReadFileContentsErrorArgumentErrorformatstrerrorr   	load_path
isinstanceValidationErrorReplaceTemplateParamsjoinsortedsetkeys)template_fileparamsparams_from_filejoined_paramskey	file_pather   missing_paramsused_paramsunused_paramss              r   ParseTemplater3   4   s     <R&%+v,-&67nc9
m%%&=&CDD+ 11)<mC	 8 ^^M*(	Hd	#z'A

$
$	  -B-)8^[

"
"	#yy)?@	AB B m((*+k9-

"
"	"ii}(=>	?@ @ 
// ;; +$$
?
F
F9ajj*+ ++s   D::F0E==Fc                    t        | t        j                        rj| j                  d      rY| j	                  d      rH| dd j                         }||v r||   t               t        |g      fS | t        |g      t               fS t        | t              rht               }t               }| j                         D ]<  }t        | |   |      \  }}}|| |<   |j                  |       |j                  |       > | ||fS t        | t              ret               }t               }g }	| D ]E  }
t        |
|      \  }}}|	j                  |       |j                  |       |j                  |       G |	||fS | t               t               fS )a  Apply the params provided into the template.

  Args:
    node: A node in the parsed template
    params: a dict of params of param-name -> param-value

  Returns:
    A tuple of (new_node, missing_params, used_params) where new_node is the
    node with all params replaced, missing_params is set of param
    references found in the node that were not provided and used_params were
    the params that we actually used.
  z{{z}}   )r"   r   string_types
startswithendswithstripr'   r   r(   r$   updatelistappend)noder*   paramr0   r1   knew_subnodenew_missingnew_usednew_nodesubnodes              r   r$   r$   g   so    c&&'tt!41Rj e	&usuc5'l33c5'lCE**dUN%KYY[-B
q'6.*{Kd1gK("  .+..dUN%KH-B
6.*{Kook"K("  nk22
su	r   )NN)__doc__
__future__r   r   r   googlecloudsdk.callioper   "googlecloudsdk.command_lib.computer   googlecloudsdk.corer   googlecloudsdk.core.utilr	   r   r   r3   r$    r   r   <module>rM      s4    '  ' / 9 $ * 
!&0f,r   