
    '                         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	Z	 	 dd
ZddZddZd Zd Z	 	 ddZ	 	 ddZ G d de      Z G d de      Zd Zd Z	 ddZd Zd Zy)z+Utilities for updating primitive dict args.    )absolute_import)division)unicode_literalsN)arg_parsers)base)yamlc           	          t        j                  dj                  |       dj                  ||      t        j                  t        j
                  ||      dj                  |            S )N--update-{}{}={}key_type
value_typez%List of key-value pairs to set as {}.metavaractiontypehelpr   Argumentformatr   UpdateActionArgDict	flag_name	long_namer   r   key_metavarvalue_metavars         4lib/googlecloudsdk/command_lib/util/args/map_util.pyMapUpdateFlagr      sX     
9%nn[-8%%ZH299)D
     c                 B    t        ||||||      j                  |       S N)r   r   )r   AddToParsergroupr   r   r   r   r   r   s          r   AddMapUpdateFlagr&   -   s/     
!
 Kr    c           	          t        j                  dj                  |       |t        j                  t        j
                  |      dj                  |            S )N--remove-{})element_typezList of {} to be removed.r   )r   r   r   r   r   ArgList)r   r   r   r   s       r   MapRemoveFlagr+   @   sI    	9%%%H5&--i8
 r    c                 >    t        ||||      j                  |       S )N)r   )r+   r#   )r%   r   r   r   r   s        r   AddMapRemoveFlagr-   J   s"    	H+
Kr    c                 n    t        j                  dj                  |       ddj                  |            S )N
--clear-{}
store_truezRemove all {}.)r   r   )r   r   r   )r   r   s     r   MapClearFlagr1   P   s2    	)$""9-
 r    c                 8    t        ||      j                  |       S )N)r1   r#   )r%   r   r   s      r   AddMapClearFlagr3   X   s    	i	+	7	7	>>r    c           	          t        j                  dj                  |       dj                  ||      t        j                  t        j
                  ||      dj                  |            S )N--set-{}r   r   zNList of key-value pairs to set as {0}. All existing {0} will be removed first.r   r   r   s         r   
MapSetFlagr6   \   sY     
	"nn[-8%%ZHy	
 	r    c                 B    t        ||||||      j                  |       S r"   )r6   r#   r$   s          r   AddMapSetFlagr8   p   s/     
!
 Kr    c                       e Zd ZdZddZd Zy)ArgDictWithYAMLOrEnvz(Interpret a YAML or .env file as a dict.Nc                      || _         || _        yzInitialize an ArgDictFile.

    Args:
      key_type: (str)->str, A function to apply to each of the dict keys.
      value_type: (str)->str, A function to apply to each of the dict values.
    Nr   selfr   r   s      r   __init__zArgDictWithYAMLOrEnv.__init__        DM DOr    c                 <   i }t         j                  j                  |      s$t        j                  dj                  |            |j                  d      r<t        j                  |      }|srt        j                  dj                  |            t        j                  |      }t        j                  |      s$t        j                  dj                  |            |j                         D ]F  \  }}| j                  r	 | j                  |      }| j                  r	 | j                  |      }|||<   H |S # t        $ r% t        j                  dj                  |            w xY w# t        $ r% t        j                  dj                  |            w xY w)zInterpret a YAML or .env file as a dict.Try to parse the file as a .env file first, if it fails, try to parse it as a YAML file.

    Args:
      file_path: The path to the file to parse.

    Returns:
      A dict with the parsed values.
    zFile [{}] does not exist.z.env)dotenv_pathz/Invalid .env file [{}], expected map-like data.7Invalid YAML/JSON data in [{}], expected map-like data.Invalid key [{0}]Invalid value [{0}])ospathexistsr   ArgumentTypeErrorr   endswithdotenvdotenv_valuesr   	load_path	dict_likeitemsr   
ValueErrorr   )r>   	file_pathmap_dictmap_file_dictkeyvalues         r   __call__zArgDictWithYAMLOrEnv.__call__   s    H77>>)$))
%
,
,Y
7  &!**yAm++=DDYO
 	
 nnY/m^^M*++ELL
 	
 $))+
U		Oc"# 
	//%(%
 hsm , O  	O--.A.H.H.MN
N	O
  	--#**51 	s   D<"E-<.E*-.FNN__name__
__module____qualname____doc__r?   rV    r    r   r:   r:      s    0!-r    r:   c                       e Zd ZdZddZd Zy)ArgDictFilez Interpret a YAML file as a dict.Nc                      || _         || _        yr<   r   r=   s      r   r?   zArgDictFile.__init__   r@   r    c                    t        j                  |      }i }t        j                  |      s$t        j                  dj                  |            |j                         D ]F  \  }}| j                  r	 | j                  |      }| j                  r	 | j                  |      }|||<   H |S # t        $ r% t        j                  dj                  |            w xY w# t        $ r% t        j                  dj                  |            w xY w)NrC   rD   rE   )
r   rM   rN   r   rI   r   rO   r   rP   r   )r>   rQ   rS   rR   rT   rU   s         r   rV   zArgDictFile.__call__   s   NN9-MH>>-())
C
J
J 
 $))+
U		Oc"# 
	//%(%
 hsm , O  	O--.A.H.H.MN
N	O
  	--#**51 	s   4B,C,.C.DrW   rX   r]   r    r   r_   r_      s    (!r    r_   c                     | j                  dj                  |      dt        ||      dj                  |             y N	--{}-file	FILE_PATHr   zvPath to a local YAML file with definitions for all {0}. All existing {0} will be removed before the new {0} are added.)r   r   r   )add_argumentr   r:   r%   r   r   r   r   s        r   AddMapSetFileFlagWithYAMLOrEnvrh      sB     #jIGy  r    c                     | j                  dj                  |      dt        ||      dj                  |             y rc   )rf   r   r_   rg   s        r   AddMapSetFileFlagrj      s@    #Z@Gy  r    c                    |s|}| j                         }|j                  dj                  |            }t        |||||       t	        ||||       t        |||       t        |||||       t        |||||       y)aR  Add flags for updating values of a map-of-atomic-values property.

  Args:
    parser: The argument parser
    flag_name: The name for the property to be used in flag names
    long_name: The name for the property to be used in help text
    key_type: A function to apply to map keys.
    value_type: A function to apply to map values.
  ztOnly --update-{0} and --remove-{0} can be used together.  If both are specified, --remove-{0} will be applied first.)r   r   )r   N)add_mutually_exclusive_groupadd_argument_groupr   r&   r-   r3   r8   rj   )parserr   r   r   r   r%   update_remove_groups          r   AddUpdateMapFlagsrp     s     
I

-
-
/%00?y	 1   &	9xP%I.Y	H Y	Hr    c           	      ^   |j                         }|j                  dj                  |             |j                  dj                  |             |j                  dj                  |             |j                  dj                  |             |j                  dj                  |             dS )zGet the flags for updating this map and return their values in a dict.

  Args:
    flag_name: The base name of the flags
    args: The argparse namespace

  Returns:
    A dict of the flag values
  r5   r
   r/   r(   rd   )set_flag_valueupdate_flag_valueclear_flag_valueremove_flag_valuefile_flag_value)GetSpecifiedArgsgetr   )r   argsspecified_argss      r   GetMapFlagsFromArgsr{   (  s     ((*.&**:+<+<Y+GH)--m.B.B9.MN(,,\-@-@-KL)--m.B.B9.MN'++K,>,>y,IJ
 r    c                     |ri S |r|S |r|S |s|rK| xs i } |xs g }| j                         D ci c]  \  }}||vs|| }}}|j                  |xs i        |S | S c c}}w )a  Determine the new map property from an existing map and parsed arguments.

  Args:
    old_map: the existing map
    set_flag_value: The value from the --set-* flag
    update_flag_value: The value from the --update-* flag
    clear_flag_value: The value from the --clear-* flag
    remove_flag_value: The value from the --remove-* flag
    file_flag_value: The value from the --*-file flag

  Returns:
    A new map with the changes applied.
  )rO   update)	old_maprr   rs   rt   ru   rv   kvnew_maps	            r   ApplyMapFlagsr   <  s    , I+mG)/R 'N11<M3Mq!tGNNN$*+N	. Os
   AA)KEYVALUE)r   )NNN)r\   
__future__r   r   r   rF   rK   googlecloudsdk.callioper   r   googlecloudsdk.corer   r   r&   r+   r-   r1   r3   r6   r8   objectr:   r_   rh   rj   rp   r{   r   r]   r    r   <module>r      s    2 &  ' 	  / ( $ . &? 4 &:6 :z$& $N	 BF$N("r    