
    l,                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SS	K
Jr   " S
 S\5      rS rS rS rS r     SS jrS rS rS rS rS rS rg)a  Utilities for updating primitive repeated args.

This code:

    from googlecloudsdk.command_lib.util import repeated

    class UpdateFoo(base.UpdateCommand)

      @staticmethod
      def Args(parser):
        # add "foo" resource arg
        repeated.AddPrimitiveArgs(
            parser, 'foo', 'baz-bars', 'baz bars',
            additional_help='The baz bars allow you to do a thing.')

      def Run(self, args):
        client = foos_api.Client()
        foo_ref = args.CONCEPTS.foo.Parse()
        foo_result = repeated.CachedResult.FromFunc(client.Get, foo_ref)
        new_baz_bars = repeated.ParsePrimitiveArgs(
            args, 'baz_bars', foo_result.GetAttrThunk('bazBars'))

        if new_baz_bars is not None:
          pass  # code to update the baz_bars


Makes a command that works like so:

    $ cli-tool foos update --set-baz-bars qux,quux
    [...]
    $ cli-tool foos update --help
    [...]
    These flags modify the member baz bars of this foo. The baz bars allow you
    to do a thing. At most one of these can be specified:

      --add-baz-bars=[BAZ_BAR,...]
         Append the given values to the current baz bars.

      --clear-baz-bars
         Empty the current baz bars.

      --remove-baz-bars=[BAZ_BAR,...]
         Remove the given values from the current baz bars.

      --set-baz-bars=[BAZ_BAR,...]
         Completely replace the current access levels with the given values.
    [...]

    )absolute_import)division)print_function)unicode_literalsN)arg_parsers)base)mapc                   >    \ rS rSrSrS r\S 5       rS rS	S jr	Sr
g)
CachedResultN   zMemoizer for a function call.c                     Xl         S U l        g N)thunk_result)selfr   s     4lib/googlecloudsdk/command_lib/util/args/repeated.py__init__CachedResult.__init__Q   s    JDL    c                 B    U " [         R                  " U/UQ70 UD65      $ r   )	functoolspartial)clsfuncargskwargss       r   FromFuncCachedResult.FromFuncU   s"    y  77788r   c                 ^    U R                   c  U R                  5       U l         U R                   $ )z-Get the result of the function call (cached).r   r   )r   s    r   GetCachedResult.GetY   s#    ||ZZ\dl<<r   Nc                 4   ^ ^^ T(       a  UU U4S j$ UU 4S j$ )at  Returns a thunk that gets the given attribute of the result of Get().

Examples:

  >>> class A(object):
  ...   b = [1, 2, 3]
  >>> CachedResult([A()].pop).GetAttrThunk('b')()
  [1, 2, 3]
  >>> CachedResult([A()].pop).GetAttrThunk('b', lambda x: x+1)
  [2, 3, 4]

Args:
  attr: str, the name of the attribute. Attribute should be iterable.
  transform: func, one-arg function that, if given, will be applied to
    every member of the attribute (which must be iterable) before returning
    it.

Returns:
  zero-arg function which, when called, returns the attribute (possibly
    transformed) of the result (which is cached).
c            	      ^   > [        [        T[        TR                  5       T 5      5      5      $ r   )listr	   getattrr!   )attrr   	transforms   r   <lambda>+CachedResult.GetAttrThunk.<locals>.<lambda>v   s    T#iT)BCDr   c                  8   > [        TR                  5       T 5      $ r   )r&   r!   )r'   r   s   r   r)   r*   x   s    WTXXZ.r    )r   r'   r(   s   ```r   GetAttrThunkCachedResult.GetAttrThunk_   s    , DD..r   r    r   )__name__
__module____qualname____firstlineno____doc__r   classmethodr   r!   r-   __static_attributes__r,   r   r   r   r   N   s(    % 9 9/r   r   c                     UR                  SS5      n[        [        U SU-   5      U5      n[        [        U SU-   5      U5      n[        U SU-   5      n[        [        U SU-   S5      U5      n[        XVXxU5      $ )a"  Parse the modification to the given repeated resource name field.

To be used in combination with AddPrimitiveArgs. This variant assumes the
repeated field contains resource names and will use the given resource_parser
to convert the arguments to relative names.

Args:
  args: argparse.Namespace of parsed arguments
  arg_name: string, the (plural) suffix of the argument (snake_case).
  current_value_thunk: zero-arg function that returns the current value of the
    attribute to be updated. Will be called lazily if required.
  resource_parser: one-arg function that returns a resource reference that
    corresponds to the resource name list to be updated.

Raises:
  ValueError: if more than one arg is set.

Returns:
  List of str: the new value for the field, or None if no change is required.
-_remove_add_clear_set_N)replace_ConvertValuesToRelativeNamesr&   _ModifyCurrentValue)	r   arg_namecurrent_value_thunkresource_parserunderscored_nameremoveaddclearr<   s	            r   ParseResourceNameArgsrG   {   s    * %%c3/(dI 001?D&%dF--.	A#
$#33
4%	&dF--t4o
G$ 
V%7J	KKr   c                 h    U (       a%  U  Vs/ s H  o!" U5      R                  5       PM     n nU $ s  snf r   )RelativeName)namesrB   names      r   r>   r>      s2    
>CDed_T"//1eED	, Es   /c                     UR                  SS5      n[        U SU-   5      n[        U SU-   5      n[        U SU-   5      n[        U SU-   S5      n[        XEXgU5      $ )a  Parse the modification to the given repeated field.

To be used in combination with AddPrimitiveArgs; see module docstring.

Args:
  args: argparse.Namespace of parsed arguments
  arg_name: string, the (plural) suffix of the argument (snake_case).
  current_value_thunk: zero-arg function that returns the current value of the
    attribute to be updated. Will be called lazily if required.

Raises:
  ValueError: if more than one arg is set.

Returns:
  List of str: the new value for the field, or None if no change is required.

r7   r8   r9   r:   r;   r<   N)r=   r&   r?   )r   r@   rA   rC   rD   rE   rF   r<   s           r   ParsePrimitiveArgsrM      sq    $ %%c3/4%556&f//0#
$#33
4%	v 00$	7$	V%7J	KKr   c           	      .   [        [        [        XX#45      5      S:  a  [        S5      eU b!  U" 5       nU Vs/ s H  ofU ;  d  M
  UPM     nnO6Ub$  U" 5       nXQ Vs/ s H  ofU;  d  M
  UPM     sn-   nOU(       a  / $ Ub  U$ gXu:w  a  U$ gs  snf s  snf )aP  Performs the modification of the current value based on the args.

Args:
  remove: list[str], items to be removed from the current value.
  add: list[str], items to be added to the current value.
  clear: bool, whether or not to clear the current value.
  set_: list[str], items to replace the current value.
  current_value_thunk: zero-arg function that returns the current value of the
    attribute to be updated. Will be called lazily if required.

Raises:
  ValueError: if more than one arg is set.

Returns:
  List of str: the new value for the field, or None if no change is required.
   zAt most one arg can be set.N)sumr	   bool
ValueError)rD   rE   rF   r<   rA   current_valuex	new_values           r   r?   r?      s    " 	TF-	./!3
2
33')M)=Mqf_MI=I
')MC JCqM3IC JJIIK > !Ks   	BB	B*Bc	                 l   Un	U(       a!  SR                  X5      n
U(       a  U
SU-   -  n
OUn
U R                  U
5      nU=(       d    UR                  5       n[        X%X5      [	        X%X5      [        X)5      /nU(       a  UR                  [        X%X5      5        U H  nUR                  U5        M     g)a  Add arguments for updating a field to the given parser.

Adds `--{add,remove,set,clear-<resource>` arguments.

Args:
  parser: calliope.parser_extensions.ArgumentInterceptor, the parser to add
    arguments to.
  resource_name: str, the (singular) name of the resource being modified (in
    whatever format you'd like it to appear in help text).
  arg_name: str, the (plural) argument suffix to use (hyphen-case).
  property_name: str, the description of the property being modified (plural;
    in whatever format you'd like it to appear in help text)
  additional_help: str, additional help text describing the property.
  metavar: str, the name of the metavar to use (if different from
    arg_name.upper()).
  is_dict_args: boolean, True when the primitive args are dict args.
  auto_group_help: bool, True to generate a summary help.
  include_set: bool, True to include the (deprecated) set argument.
z,These flags modify the member {} of this {}. N)	formatadd_mutually_exclusive_groupupper_GetAppendArg_GetRemoveArg_GetClearArgappend
_GetSetArgAddToParser)parserresource_namer@   property_nameadditional_helpmetavaris_dict_argsauto_group_helpinclude_setproperties_name
group_helpgroupr   args                 r   AddPrimitiveArgsrm      s    8 "/?FF(JC/))j J

-
-j
9%'x~~''HEHE8-
$
 KK
8oLMcOOE r   c           
          SR                  U 5      nSR                  U5      nSR                  U 5      nSR                  U5      n[        R                  " U(       a  UOU[        U5      U[	        XuU5      S9$ )Nz--add-{}z*Append the given values to the current {}.z--update-{}z3Update the given key-value pairs in the current {}.typere   helprX   r   Argument_GetArgType_GetArgHelp)r@   re   	prop_namerf   	list_name	list_help	dict_name	dict_helps           r   r[   r[     sr    )):AA)L)""8,)CJJ)	iY|$y\:	
< <r   c           
          SR                  U5      nSR                  U5      n[        R                  " SR                  U 5      U[        U5      [	        XTU5      S9$ )Nz,Remove the given values from the current {}.zCRemove the key-value pairs from the current {} with the given keys.z--remove-{})re   rp   rq   rr   r@   re   rv   rf   rx   rz   s         r   r\   r\   #  sZ    <CCIN)y) 	8$|$y\:	
< <r   c           
          SR                  U5      nSR                  U5      n[        R                  " SR                  U 5      [        U5      U[	        XTU5      S9$ )Nz8Completely replace the current {} with the given values.zACompletely replace the current {} with the given key-value pairs.z--set-{}ro   rr   r|   s         r   r_   r_   .  s]    HOO)	* 	!|$y\:	
< <r   c                 j    [         R                  " SR                  U 5      SSR                  U5      S9$ )Nz
--clear-{}
store_truezEmpty the current {}.)actionrq   )r   rs   rX   )r@   rv   s     r   r]   r]   :  s2    	(#")))4
6 6r   c                 d    U (       a  [         R                  " 5       $ [         R                  " 5       $ r   )r   ArgDictArgList)rf   s    r   rt   rt   A  s!    ".			IK4G4G4IIr   c                     U(       a  U $ U$ r   r,   )rz   rx   rf   s      r   ru   ru   E  s    "1	1r   ) NFTT)r3   
__future__r   r   r   r   r   googlecloudsdk.callioper   r   	six.movesr	   objectr   rG   r>   rM   r?   rm   r[   r\   r_   r]   rt   ru   r,   r   r   <module>r      s   0d '  % '  / ( */6 */ZLDL6$V &(!"'%)!%.b
<<	<6J2r   