
    B/                     j   S 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	\R                  4S
 jrS	\R                  4S jrS\R                  4S jrS	\R                  4S jr\R                  4S jr\R                  4S jrS\R                  4S jrS\R                  4S jrS rg)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.
    )absolute_import)division)print_functionN)_exceptions)_flagvalues)_validators_classeszFlag validation failedc                 H    [         R                  " XU5      n[        X45        g)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, name of the flag to be checked.
  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.
N)r   SingleFlagValidator_add_validator)	flag_namecheckermessageflag_valuesvs        1platform/bq/third_party/absl/flags/_validators.pyregister_validatorr   .   s    4 --i'J!     c                    ^ ^^ U UU4S jnU$ )a  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 register_validator() for the specification of checker function.

Args:
  flag_name: str, name of the flag to be checked.
  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                     > [        TU TTS9  U $ Nr   r   )r   )functionr   r   r   s    r   decoratevalidator.<locals>.decoratef   s    y(&#.0 Or    )r   r   r   r   s   ``` r   	validatorr   L   s    4
 
/r   zFlags validation failedc                 H    [         R                  " XU5      n[        X45        g)a  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], a list of the flag names to be checked.
  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.
N)r   MultiFlagsValidatorr   )
flag_namesmulti_flags_checkerr   r   r   s        r   register_multi_flags_validatorr!   n   s!    4 --w0! r   c                    ^ ^^ U UU4S jnU$ )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 register_multi_flags_validator() for the specification of checker
function.

Args:
  flag_names: [str], a list of the flag names to be checked.
  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                     > [        TU TTS9  U $ r   )r!   )r   r   r   r   s    r   r   'multi_flags_validator.<locals>.decorate   s    ":#++2/:< Or   r   )r   r   r   r   s   ``` r   multi_flags_validatorr%      s    : 
/r   c                     X   R                   b  [        R                  " SU -  SS9  [        U S SR	                  U 5      US9  g)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, name of the flag
  flag_values: flags.FlagValues, optional FlagValues instance where the flag
      is defined.
Raises:
  AttributeError: Raised when flag_name is not registered as a valid flag
      name.
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                 
    U S L$ Nr   )values    r   <lambda>'mark_flag_as_required.<locals>.<lambda>   s	    E%r   z,Flag --{} must have a value other than None.r   )defaultwarningswarnr   format)r   r   s     r   mark_flag_as_requiredr3      sR    , ##/MM	#	$ 	
 %<CCIN	r   c                 ,    U  H  n[        X!5        M     g)a.  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], names 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)r3   )r   r   r   s      r   mark_flags_as_requiredr5      s    " i)1 r   Fc                    ^ ^ T  H8  nX#   R                   c  M  [        R                  " SR                  U5      SS9  M:     U U4S jn[	        T XBS9  g)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], names of the flags.
  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.
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                    > [        S U R                  5        5       5      nUS:X  d  T(       d  US:X  a  g[        R                  " SR	                  T(       a  SOSSR                  T5      5      5      e)	Nc              3   .   #    U  H  oc  M  Sv   M     g 7f)N   r   .0vals     r   	<genexpr>Tmark_flags_as_mutual_exclusive.<locals>.validate_mutual_exclusion.<locals>.<genexpr>  s     I"53QQ"5s   	r9   r   Tz1{} one of ({}) must have a value other than None.ExactlyAt most, sumvaluesr   ValidationErrorr2   join
flags_dict
flag_countr   requireds     r   validate_mutual_exclusionAmark_flags_as_mutual_exclusive.<locals>.validate_mutual_exclusion  sd    I*"3"3"5IIJQxJ!O

%
%;BB!Iy$))J2G	IJ Jr   r   )r/   r0   r1   r2   r!   )r   rJ   r   r   rK   s   ``   r   mark_flags_as_mutual_exclusiverN      sS    $ i%%1mm77=vi7H	 J !+Fr   c                    ^ ^ T  H;  nX#   R                   (       a  M  [        R                  " SR                  U5      5      e   U U4S jn[	        T XBS9  g)aZ  Ensures that only one flag among flag_names is True.

Args:
  flag_names: [str], names of the flags.
  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.
zbFlag --{} is not Boolean, which is required for flags used in mark_bool_flags_as_mutual_exclusive.c                    > [        S U R                  5        5       5      nUS:X  d  T(       d  US:X  a  g[        R                  " SR	                  T(       a  SOSSR                  T5      5      5      e)	Nc              3   8   #    U  H  n[        U5      v   M     g 7fr+   )boolr:   s     r   r=   amark_bool_flags_as_mutual_exclusive.<locals>.validate_boolean_mutual_exclusion.<locals>.<genexpr>$  s     >*=3T#YY*=s   r9   r   Tz{} one of ({}) must be True.r?   r@   rA   rB   rG   s     r   !validate_boolean_mutual_exclusionNmark_bool_flags_as_mutual_exclusive.<locals>.validate_boolean_mutual_exclusion#  sd    >**;*;*=>>JQxJ!O

%
%&--!Iy$))J2G	IJ Jr   rM   N)booleanr   rE   r2   r!   )r   rJ   r   r   rT   s   ``   r   #mark_bool_flags_as_mutual_exclusiverW     sY     i!)))''117	1BD D J !3Nr   c                 l    UR                  5        H   nX   R                  R                  U5        M"     g)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)fvvalidator_instancer   s      r   r   r   /  s.     &557iM##$67 8r   )__doc__
__future__r   r   r   r0   
absl.flagsr   r   r   FLAGSr   r   r!   r%   r3   r5   rN   rW   r   r   r   r   <module>rb      s   ( '  %  " " *
  8#.#4#4!< ":%++H ,E/:/@/@!@ #;&1&7&7$N 2=1B1B  F 4?3D3D 2* 9>/:/@/@#FL >C4?4E4EN:
8r   