
    '                        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	 jr  SS
 jrSS jrSS jrS rS r  SS jr  SS jr " S S\5      r " S S\5      rS rS r SS jrS rS rg)z+Utilities for updating primitive dict args.    )absolute_import)division)unicode_literalsN)arg_parsers)base)yamlc           
          [         R                  " SR                  U 5      SR                  XE5      [        R                  [        R
                  " X#S9SR                  U5      S9$ )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      sT     
9%nn[8%%H299)D
     c           	      <    [        UUUUUUS9R                  U 5      $ N)r   r   )r   AddToParsergroupr   r   r   r   r   r   s          r   AddMapUpdateFlagr&   -   s/     
!
 Kr    c           	          [         R                  " SR                  U 5      U[        R                  [        R
                  " US9SR                  U5      S9$ )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                 4    [        XX4S9R                  U 5      $ )N)r   )r+   r#   )r%   r   r   r   r   s        r   AddMapRemoveFlagr-   J   s    	H
Kr    c                 j    [         R                  " SR                  U 5      SSR                  U5      S9$ )N
--clear-{}
store_truezRemove all {}.)r   r   )r   r   r   )r   r   s     r   MapClearFlagr1   P   s2    	)$""9-
 r    c                 6    [        X5      R                  U 5      $ )N)r1   r#   )r%   r   r   s      r   AddMapClearFlagr3   X   s    	i	+	7	7	>>r    c           
          [         R                  " SR                  U 5      SR                  XE5      [        R                  [        R
                  " X#S9SR                  U5      S9$ )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   \   sU     
	"nn[8%%Hy	
 	r    c           	      <    [        UUUUUUS9R                  U 5      $ r"   )r6   r#   r$   s          r   AddMapSetFlagr8   p   s/     
!
 Kr    c                   (    \ rS rSrSrSS jrS rSrg)ArgDictWithYAMLOrEnv   z(Interpret a YAML or .env file as a dict.Nc                     Xl         X l        g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__ArgDictWithYAMLOrEnv.__init__        M Or    c                    0 n[         R                  R                  U5      (       d%  [        R                  " SR                  U5      5      eUR                  S5      (       aA  [        R                  " US9nU(       d%  [        R                  " SR                  U5      5      eOV[        R                  " U5      n[        R                  " U5      (       d%  [        R                  " SR                  U5      5      eUR                  5        HO  u  pEU R                  (       a   U R                  U5      nU R                  (       a   U R                  U5      nXRU'   MQ     U$ ! [         a&    [        R                  " SR                  U5      5      ef = f! [         a&    [        R                  " SR                  U5      5      ef = f)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__ArgDictWithYAMLOrEnv.__call__   s    H77>>)$$))
%
,
,Y
7  &!!**yAm++=DDYO
 	
 
 nnY/m^^M**++ELL
 	
 $))+
		Oc"# 
	//%(%
 sm , O  	O--.A.H.H.MN
N	O
  	--#**51 	s    EF0F0Gr   NN__name__
__module____qualname____firstlineno____doc__r@   rX   __static_attributes__ r    r   r:   r:      s    0!-r    r:   c                   (    \ rS rSrSrSS jrS rSrg)ArgDictFile   z Interpret a YAML file as a dict.Nc                     Xl         X l        gr=   r   r>   s      r   r@   ArgDictFile.__init__   rB   r    c                 H   [         R                  " U5      n0 n[         R                  " U5      (       d%  [        R                  " SR                  U5      5      eUR                  5        HO  u  pEU R                  (       a   U R                  U5      nU R                  (       a   U R                  U5      nXSU'   MQ     U$ ! [         a&    [        R                  " SR                  U5      5      ef = f! [         a&    [        R                  " SR                  U5      5      ef = f)NrE   rF   rG   )
r   rO   rP   r   rK   r   rQ   r   rR   r   )r?   rS   rU   rT   rV   rW   s         r   rX   ArgDictFile.__call__   s   NN9-MH>>-(())
C
J
J 
 $))+
		Oc"# 
	//%(%
 sm , O  	O--.A.H.H.MN
N	O
  	--#**51 	s    B>#C1>0C.10D!r   rZ   r[   rb   r    r   rd   rd      s    (!r    rd   c           	      r    U R                  SR                  U5      S[        X4S9SR                  U5      S9  g 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   AddMapSetFileFlagWithYAMLOrEnvrp      s@     #IGy  r    c           	      r    U R                  SR                  U5      S[        X4S9SR                  U5      S9  g rk   )rn   r   rd   ro   s        r   AddMapSetFileFlagrr      s>    #@Gy  r    c                     U(       d  UnU R                  5       nUR                  SR                  U5      S9n[        UUUUUS9  [	        XaX#S9  [        XQU5        [        XQX#US9  [        XQX#US9  g)aD  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   rr   )parserr   r   r   r   r%   update_remove_groups          r   AddUpdateMapFlagsrx     s     
I

-
-
/%00?y	 1   &9P%I.	 	r    c           	      ^   UR                  5       nUR                  SR                  U 5      5      UR                  SR                  U 5      5      UR                  SR                  U 5      5      UR                  SR                  U 5      5      UR                  SR                  U 5      5      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(   rl   )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                    U(       a  0 $ U(       a  U$ U(       a  U$ U(       d  U(       a]  U =(       d    0 n U=(       d    / nU R                  5        VVs0 s H  u  pgXd;  d  M  Xg_M     nnnUR                  U=(       d    0 5        U$ U $ s  snnf )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.
)rQ   update)	old_maprz   r{   r|   r}   r~   kvnew_maps	            r   ApplyMapFlagsr   <  s    , I+mG)/R 'N13MtqtGNNN$*+N	. Os   B	"B	)KEYVALUE)r   )NNN)r`   
__future__r   r   r   rH   rM   googlecloudsdk.callioper   r   googlecloudsdk.corer   r   r&   r+   r-   r1   r3   r6   r8   objectr:   rd   rp   rr   rx   r   r   rb   r    r   <module>r      s    2 &  ' 	  / ( $ . &? 4 &:6 :z$& $N	 BF$N("r    