a
    Sic@7                     @   s   d Z ddlZddlmZ ddlmZ ddlmZ dejfddZdejfd	d
ZdejfddZ	dejfddZ
ejfddZejfddZdejfddZdejfddZdd ZdS )a  Module to enforce different constraints on flags.

Flags validators can be registered using following functions / decorators::

    flags.register_validator
    @flags.validator
    flags.register_multi_flags_validator
    @flags.multi_flags_validator

Three convenience functions are also provided for common flag constraints::

    flags.mark_flag_as_required
    flags.mark_flags_as_required
    flags.mark_flags_as_mutual_exclusive
    flags.mark_bool_flags_as_mutual_exclusive

See their docstring in this module for a usage manual.

Do NOT import this module directly. Import the flags package and use the
aliases defined at the package level instead.
    N)_exceptions)_flagvalues)_validators_classeszFlag validation failedc                 C   s,   t | |\} }t| ||}t|| dS )a  Adds a constraint, which will be enforced during program execution.

  The constraint is validated when flags are initially parsed, and after each
  change of the corresponding flag's value.

  Args:
    flag_name: str | FlagHolder, name or holder of the flag to be checked.
        Positional-only parameter.
    checker: callable, a function to validate the flag.

        * input - A single positional argument: The value of the corresponding
          flag (string, boolean, etc.  This value will be passed to checker
          by the library).
        * output - bool, True if validator constraint is satisfied.
          If constraint is not satisfied, it should either ``return False`` or
          ``raise flags.ValidationError(desired_error_message)``.

    message: str, error text to be shown to the user if checker returns False.
        If checker raises flags.ValidationError, message from the raised
        error will be shown.
    flag_values: flags.FlagValues, optional FlagValues instance to validate
        against.

  Raises:
    AttributeError: Raised when flag_name is not registered as a valid flag
        name.
    ValueError: Raised when flag_values is non-default and does not match the
        FlagValues of the provided FlagHolder instance.
  N)r   resolve_flag_refr   SingleFlagValidator_add_validator)	flag_namecheckermessageflag_valuesv r   R/var/www/html/django/DPS/env/lib/python3.9/site-packages/absl/flags/_validators.pyregister_validator,   s    !r   c                    s    fdd}|S )aW  A function decorator for defining a flag validator.

  Registers the decorated function as a validator for flag_name, e.g.::

      @flags.validator('foo')
      def _CheckFoo(foo):
        ...

  See :func:`register_validator` for the specification of checker function.

  Args:
    flag_name: str | FlagHolder, name or holder of the flag to be checked.
        Positional-only parameter.
    message: str, error text to be shown to the user if checker returns False.
        If checker raises flags.ValidationError, message from the raised
        error will be shown.
    flag_values: flags.FlagValues, optional FlagValues instance to validate
        against.
  Returns:
    A function decorator that registers its function argument as a validator.
  Raises:
    AttributeError: Raised when flag_name is not registered as a valid flag
        name.
  c                    s   t  | d | S Nr
   r   )r   functionr   r   r
   r   r   decoratem   s
    zvalidator.<locals>.decorater   )r   r
   r   r   r   r   r   	validatorR   s    r   zFlags validation failedc                 C   s,   t | |\} }t| ||}t|| dS )aP  Adds a constraint to multiple flags.

  The constraint is validated when flags are initially parsed, and after each
  change of the corresponding flag's value.

  Args:
    flag_names: [str | FlagHolder], a list of the flag names or holders to be
        checked. Positional-only parameter.
    multi_flags_checker: callable, a function to validate the flag.

        * input - dict, with keys() being flag_names, and value for each key
            being the value of the corresponding flag (string, boolean, etc).
        * output - bool, True if validator constraint is satisfied.
            If constraint is not satisfied, it should either return False or
            raise flags.ValidationError.

    message: str, error text to be shown to the user if checker returns False.
        If checker raises flags.ValidationError, message from the raised
        error will be shown.
    flag_values: flags.FlagValues, optional FlagValues instance to validate
        against.

  Raises:
    AttributeError: Raised when a flag is not registered as a valid flag name.
    ValueError: Raised when multiple FlagValues are used in the same
        invocation. This can occur when FlagHolders have different `_flagvalues`
        or when str-type flag_names entries are present and the `flag_values`
        argument does not match that of provided FlagHolder(s).
  N)r   resolve_flag_refsr   MultiFlagsValidatorr   )
flag_namesZmulti_flags_checkerr
   r   r   r   r   r   register_multi_flags_validatoru   s    !r   c                    s    fdd}|S )a  A function decorator for defining a multi-flag validator.

  Registers the decorated function as a validator for flag_names, e.g.::

      @flags.multi_flags_validator(['foo', 'bar'])
      def _CheckFooBar(flags_dict):
        ...

  See :func:`register_multi_flags_validator` for the specification of checker
  function.

  Args:
    flag_names: [str | FlagHolder], a list of the flag names or holders to be
        checked. Positional-only parameter.
    message: str, error text to be shown to the user if checker returns False.
        If checker raises flags.ValidationError, message from the raised
        error will be shown.
    flag_values: flags.FlagValues, optional FlagValues instance to validate
        against.

  Returns:
    A function decorator that registers its function argument as a validator.

  Raises:
    AttributeError: Raised when a flag is not registered as a valid flag name.
  c                    s   t  | d | S r   )r   r   r   r   r
   r   r   r      s    z'multi_flags_validator.<locals>.decorater   )r   r
   r   r   r   r   r   multi_flags_validator   s    r   c                 C   sN   t | |\} }||  jdur0tjd|  dd t| dd d| |d dS )	a  Ensures that flag is not None during program execution.

  Registers a flag validator, which will follow usual validator rules.
  Important note: validator will pass for any non-``None`` value, such as
  ``False``, ``0`` (zero), ``''`` (empty string) and so on.

  If your module might be imported by others, and you only wish to make the flag
  required when the module is directly executed, call this method like this::

      if __name__ == '__main__':
        flags.mark_flag_as_required('your_flag_name')
        app.run()

  Args:
    flag_name: str | FlagHolder, name or holder of the flag.
        Positional-only parameter.
    flag_values: flags.FlagValues, optional :class:`~absl.flags.FlagValues`
        instance where the flag is defined.
  Raises:
    AttributeError: Raised when flag_name is not registered as a valid flag
        name.
    ValueError: Raised when flag_values is non-default and does not match the
        FlagValues of the provided FlagHolder instance.
  NzFlag --%s has a non-None default value; therefore, mark_flag_as_required will pass even if flag is not specified in the command line!   
stacklevelc                 S   s   | d uS Nr   )valuer   r   r   <lambda>       z'mark_flag_as_required.<locals>.<lambda>z,Flag --{} must have a value other than None.r   )r   r   defaultwarningswarnr   format)r   r   r   r   r   mark_flag_as_required   s    r(   c                 C   s   | D ]}t || qdS )ae  Ensures that flags are not None during program execution.

  If your module might be imported by others, and you only wish to make the flag
  required when the module is directly executed, call this method like this::

      if __name__ == '__main__':
        flags.mark_flags_as_required(['flag1', 'flag2', 'flag3'])
        app.run()

  Args:
    flag_names: Sequence[str | FlagHolder], names or holders of the flags.
    flag_values: flags.FlagValues, optional FlagValues instance where the flags
        are defined.
  Raises:
    AttributeError: If any of flag name has not already been defined as a flag.
  N)r(   )r   r   r   r   r   r   mark_flags_as_required   s    r)   Fc                    s\   t  |\ } D ]&}|| jdurtjd|dd q fdd}t ||d dS )a  Ensures that only one flag among flag_names is not None.

  Important note: This validator checks if flag values are ``None``, and it does
  not distinguish between default and explicit values. Therefore, this validator
  does not make sense when applied to flags with default values other than None,
  including other false values (e.g. ``False``, ``0``, ``''``, ``[]``). That
  includes multi flags with a default value of ``[]`` instead of None.

  Args:
    flag_names: [str | FlagHolder], names or holders of flags.
        Positional-only parameter.
    required: bool. If true, exactly one of the flags must have a value other
        than None. Otherwise, at most one of the flags can have a value other
        than None, and it is valid for all of the flags to be None.
    flag_values: flags.FlagValues, optional FlagValues instance where the flags
        are defined.

  Raises:
    ValueError: Raised when multiple FlagValues are used in the same
        invocation. This can occur when FlagHolders have different `_flagvalues`
        or when str-type flag_names entries are present and the `flag_values`
        argument does not match that of provided FlagHolder(s).
  NzFlag --{} has a non-None default value. That does not make sense with mark_flags_as_mutual_exclusive, which checks whether the listed flags have a value other than None.r   r   c                    sR   t dd |  D }|dks*s.|dkr.dS tdr>dndd	 d S )
Nc                 s   s   | ]}|d urdV  qd S )N   r   .0valr   r   r   	<genexpr>%  r#   zTmark_flags_as_mutual_exclusive.<locals>.validate_mutual_exclusion.<locals>.<genexpr>r*   r   Tz1{} one of ({}) must have a value other than None.ExactlyAt most, sumvaluesr   ValidationErrorr'   joinZ
flags_dictZ
flag_countr   requiredr   r   validate_mutual_exclusion$  s    zAmark_flags_as_mutual_exclusive.<locals>.validate_mutual_exclusionr   )r   r   r$   r%   r&   r'   r   )r   r9   r   r   r:   r   r8   r   mark_flags_as_mutual_exclusive  s    r<   c                    sT   t  |\ } D ]}|| jstd|q fdd}t ||d dS )a  Ensures that only one flag among flag_names is True.

  Args:
    flag_names: [str | FlagHolder], names or holders of flags.
        Positional-only parameter.
    required: bool. If true, exactly one flag must be True. Otherwise, at most
        one flag can be True, and it is valid for all flags to be False.
    flag_values: flags.FlagValues, optional FlagValues instance where the flags
        are defined.

  Raises:
    ValueError: Raised when multiple FlagValues are used in the same
        invocation. This can occur when FlagHolders have different `_flagvalues`
        or when str-type flag_names entries are present and the `flag_values`
        argument does not match that of provided FlagHolder(s).
  zbFlag --{} is not Boolean, which is required for flags used in mark_bool_flags_as_mutual_exclusive.c                    sR   t dd |  D }|dks*s.|dkr.dS tdr>dndd	 d S )
Nc                 s   s   | ]}t |V  qd S r    )boolr+   r   r   r   r.   K  r#   zamark_bool_flags_as_mutual_exclusive.<locals>.validate_boolean_mutual_exclusion.<locals>.<genexpr>r*   r   Tz{} one of ({}) must be True.r/   r0   r1   r2   r7   r8   r   r   !validate_boolean_mutual_exclusionJ  s    zNmark_bool_flags_as_mutual_exclusive.<locals>.validate_boolean_mutual_exclusionr;   N)r   r   booleanr   r5   r'   r   )r   r9   r   r   r>   r   r8   r   #mark_bool_flags_as_mutual_exclusive0  s    
r@   c                 C   s"   |  D ]}| | j| qdS )a  Register new flags validator to be checked.

  Args:
    fv: flags.FlagValues, the FlagValues instance to add the validator.
    validator_instance: validators.Validator, the validator to add.
  Raises:
    KeyError: Raised when validators work with a non-existing flag.
  N)get_flags_names
validatorsappend)fvZvalidator_instancer   r   r   r   r   V  s    	r   )__doc__r%   
absl.flagsr   r   r   FLAGSr   r   r   r   r(   r)   r<   r@   r   r   r   r   r   <module>   s2   
&
%
)
('
/
&