a
    }=ic                     @   sj   d Z ddlZddlZddlmZ ejZdd ZefddZefdd	Zd
d Z	G dd de
Zdd ZdS )a`  Decorator and context manager for saving and restoring flag values.

There are many ways to save and restore.  Always use the most convenient method
for a given use case.

Here are examples of each method.  They all call ``do_stuff()`` while
``FLAGS.someflag`` is temporarily set to ``'foo'``::

    from absl.testing import flagsaver

    # Use a decorator which can optionally override flags via arguments.
    @flagsaver.flagsaver(someflag='foo')
    def some_func():
      do_stuff()

    # Use a decorator which can optionally override flags with flagholders.
    @flagsaver.flagsaver((module.FOO_FLAG, 'foo'), (other_mod.BAR_FLAG, 23))
    def some_func():
      do_stuff()

    # Use a decorator which does not override flags itself.
    @flagsaver.flagsaver
    def some_func():
      FLAGS.someflag = 'foo'
      do_stuff()

    # Use a context manager which can optionally override flags via arguments.
    with flagsaver.flagsaver(someflag='foo'):
      do_stuff()

    # Save and restore the flag values yourself.
    saved_flag_values = flagsaver.save_flag_values()
    try:
      FLAGS.someflag = 'foo'
      do_stuff()
    finally:
      flagsaver.restore_flag_values(saved_flag_values)

We save and restore a shallow copy of each Flag object's ``__dict__`` attribute.
This preserves all attributes of the flag, such as whether or not it was
overridden from its default value.

WARNING: Currently a flag that is saved and then deleted cannot be restored.  An
exception will be raised.  However if you *add* a flag after saving flag values,
and then restore flag values, the added flag will be deleted with no errors.
    N)flagsc                  O   s   | st f i |S t| dkrZt| d rZ|r6td| d }t|rPtdt|i S | D ]l}t|t	rxt|dkrtd|f |\}}t|t
jstd|f |j|v rtd|j |||j< q^t f i |S )z7The main flagsaver interface. See module doc for usage.   r   z?It's invalid to specify both positional and keyword parameters.z2@flagsaver.flagsaver cannot be applied to a class.   z+Expected (FlagHolder, value) pair, found %rzCannot set --%s multiple times)_FlagOverriderlencallable
ValueErrorinspectisclass	TypeError_wrap
isinstancetupler   Z
FlagHoldername)argskwargsfuncargZholdervalue r   g/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/absl/testing/flagsaver.py	flagsaverF   s*    


r   c                    s    fdd D S )a_  Returns copy of flag values as a dict.

  Args:
    flag_values: FlagValues, the FlagValues instance with which the flag will
        be saved. This should almost never need to be overridden.
  Returns:
    Dictionary mapping keys to values. Keys are flag names, values are
    corresponding ``__dict__`` members. E.g. ``{'key': value_dict, ...}``.
  c                    s   i | ]}|t  | qS r   )_copy_flag_dict).0r   flag_valuesr   r   
<dictcomp>k       z$save_flag_values.<locals>.<dictcomp>r   r   r   r   r   save_flag_valuesa   s    
r   c                 C   s^   t |}|D ]L}| |}|du r.t|| q|| j|d krN|d || _||| _qdS )a  Restores flag values based on the dictionary of flag values.

  Args:
    saved_flag_values: {'flag_name': value_dict, ...}
    flag_values: FlagValues, the FlagValues instance from which the flag will
        be restored. This should almost never need to be overridden.
  N_value)listgetdelattrr   __dict__)Zsaved_flag_valuesr   Znew_flag_namesr   Zsavedr   r   r   restore_flag_valuesn   s    
r$   c                    s   t   fdd}|S )a[  Creates a wrapper function that saves/restores flag values.

  Args:
    func: function object - This will be called between saving flags and
        restoring flags.
    overrides: {str: object} - Flag names mapped to their values.  These flags
        will be set after saving the original flag state.

  Returns:
    return value from func()
  c                     s@   t f i   | i |W  d   S 1 s20    Y  dS )z/Wrapper function that saves and restores flags.N)r   )r   r   r   	overridesr   r   _flagsaver_wrapper   s    z!_wrap.<locals>._flagsaver_wrapper)	functoolswraps)r   r&   r'   r   r%   r   r      s    r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )r   zOverrides flags for the duration of the decorated function call.

  It also restores all original values of flags after decorated method
  completes.
  c                 K   s   || _ d | _d S N)
_overrides_saved_flag_values)selfr&   r   r   r   __init__   s    z_FlagOverrider.__init__c                 C   s   t |rtdt|| jS )Nz'flagsaver cannot be applied to a class.)r	   r
   r   r   r+   )r-   r   r   r   r   __call__   s    
z_FlagOverrider.__call__c                 C   s@   t t| _ztjf i | j W n   t| jt  Y n0 d S r*   )r   FLAGSr,   _set_attributesr+   r$   )r-   r   r   r   	__enter__   s    
z_FlagOverrider.__enter__c                 C   s   t | jt d S r*   )r$   r,   r0   )r-   exc_type	exc_value	tracebackr   r   r   __exit__   s    z_FlagOverrider.__exit__N)__name__
__module____qualname____doc__r.   r/   r2   r6   r   r   r   r   r      s
   	r   c                 C   s&   | j  }| j|d< t| j|d< |S )a  Returns a copy of the flag object's ``__dict__``.

  It's mostly a shallow copy of the ``__dict__``, except it also does a shallow
  copy of the validator list.

  Args:
    flag: flags.Flag, the flag to copy.

  Returns:
    A copy of the flag object's ``__dict__``.
  r   
validators)r#   copyr   r    r;   )flagr<   r   r   r   r      s    

r   )r:   r(   r	   Zabslr   r0   r   r   r$   r   objectr   r   r   r   r   r   <module>   s   /