
    q&fA                     H   d Z ddlZddlZddlmZ ddlmZmZ ddlm	Z	m
Z
mZmZ ddlmZ 	 ej                  Zg d	Zd
 Zd Zed        Z eddd       G d d             Zd Z eddd       G d d             Zd5dZ eddd       G d d             Zd Z eddd       G d d             Zd Z eddd       G d d             Zd Z  eddd       G d  d!             Z!d" Z" eddd       G d# d$             Z#d6d%Z$ eddd       G d& d'             Z%d6d(Z& eddd       G d) d*             Z'd+ Z(d, Z)d- Z*d. Z+ eddd       G d/ d0             Z,d1 Z- eddd       G d2 d3             Z.d4 Z/y# e$ r  e ej$                  d            ZY hw xY w)7z
Commonly useful validators.
    N)contextmanager   )get_run_validatorsset_run_validators)_AndValidatorand_attribattrs)NotCallableError )r   deep_iterabledeep_mappingdisabledgeget_disabledgtin_instance_ofis_callablelelt
matches_remax_lenmin_lenoptionalprovidesset_disabledc                     t        |         y)a  
    Globally disable or enable running validators.

    By default, they are run.

    :param disabled: If ``True``, disable running all validators.
    :type disabled: bool

    .. warning::

        This function is not thread-safe!

    .. versionadded:: 21.3.0
    Nr   )r   s    F/usr/lib/python3/dist-packages/mercurial/thirdparty/attr/validators.pyr   r   .   s     8|$    c                      t                S )z
    Return a bool indicating whether validators are currently disabled or not.

    :return: ``True`` if validators are currently disabled.
    :rtype: bool

    .. versionadded:: 21.3.0
    )r    r!   r    r   r   @   s     "###r!   c               #   b   K   t        d       	 d t        d       y# t        d       w xY ww)z
    Context manager that disables running validators within its context.

    .. warning::

        This context manager is not thread-safe!

    .. versionadded:: 21.3.0
    FNTr   r#   r!   r    r   r   L   s)      u!4 4 s   / /,/FT)reprslotshashc                   &    e Zd Z e       Zd Zd Zy)_InstanceOfValidatorc                     t        || j                        sIt        dj                  |j                  | j                  |j
                  |      || j                  |      y)P
        We use a callable class to be able to change the ``__repr__``.
        z?'{name}' must be {type!r} (got {value!r} that is a {actual!r}).)nametypeactualvalueN)
isinstancer-   	TypeErrorformatr,   	__class__selfinstattrr/   s       r    __call__z_InstanceOfValidator.__call__b   s_     %+%v ??	  &   		  ,r!   c                 :    dj                  | j                        S )Nz)<instance_of validator for type {type!r}>r-   )r2   r-   r5   s    r    __repr__z_InstanceOfValidator.__repr__t   s"    :AA B 
 	
r!   N)__name__
__module____qualname__r	   r-   r8   r<   r#   r!   r    r)   r)   ^   s    8D$
r!   r)   c                     t        |       S )a  
    A validator that raises a `TypeError` if the initializer is called
    with a wrong type for this particular attribute (checks are performed using
    `isinstance` therefore it's also valid to pass a tuple of types).

    :param type: The type to check for.
    :type type: type or tuple of types

    :raises TypeError: With a human readable error message, the attribute
        (of type `attrs.Attribute`), the expected type, and the value it
        got.
    )r)   r:   s    r    r   r   z   s      %%r!   )r%   frozenr&   c                   4    e Zd Z e       Z e       Zd Zd Zy)_MatchesReValidatorc                     | j                  |      sHt        dj                  |j                  | j                  j                  |      || j                  |      y)r+   z9'{name}' must match regex {pattern!r} ({value!r} doesn't))r,   patternr/   N)
match_func
ValueErrorr2   r,   rE   r4   s       r    r8   z_MatchesReValidator.__call__   sZ     u%''-vDLL,@,@ (. (   &r!   c                 :    dj                  | j                        S )Nz.<matches_re validator for pattern {pattern!r}>)rE   )r2   rE   r;   s    r    r<   z_MatchesReValidator.__repr__   s"    ?FFLL G 
 	
r!   N)r=   r>   r?   r	   rE   rF   r8   r<   r#   r!   r    rC   rC      s    hGJ
r!   rC   c                    t         j                  dt         j                  t         j                  f}||vrBt	        dj                  dj                  t        d t        |      D                                t        | t              r|rt        d      | }nt        j                  | |      }|t         j                  u r|j                  }n+|t         j                  u r|j                  }n|j                  }t        ||      S )a  
    A validator that raises `ValueError` if the initializer is called
    with a string that doesn't match *regex*.

    :param regex: a regex string or precompiled pattern to match against
    :param int flags: flags that will be passed to the underlying re function
        (default 0)
    :param callable func: which underlying `re` function to call. Valid options
        are `re.fullmatch`, `re.search`, and `re.match`; the default ``None``
        means `re.fullmatch`. For performance reasons, the pattern is always
        precompiled using `re.compile`.

    .. versionadded:: 19.2.0
    .. versionchanged:: 21.3.0 *regex* can be a pre-compiled pattern.
    Nz'func' must be one of {}.z, c              3   D   K   | ]  }|xr |j                   xs d   yw)NoneN)r=   ).0es     r    	<genexpr>zmatches_re.<locals>.<genexpr>   s&      78(ajj2F2s    zR'flags' can only be used with a string pattern; pass flags to re.compile() instead)re	fullmatchsearchmatchrG   r2   joinsortedsetr0   Patternr1   compilerC   )regexflagsfuncvalid_funcsrE   rF   s         r    r   r      s      <<ryy"((;K;'..		 <?<L 
 	
 %!5  **UE*rxx]]
		^^
&&
w
33r!   c                   &    e Zd Z e       Zd Zd Zy)_ProvidesValidatorc                     | j                   j                  |      s>t        dj                  |j                  | j                   |      || j                   |      y)r+   z<'{name}' must provide {interface!r} which {value!r} doesn't.)r,   	interfacer/   N)r_   
providedByr1   r2   r,   r4   s       r    r8   z_ProvidesValidator.__call__   sZ     ~~((/!6dnnE "    0r!   c                 :    dj                  | j                        S )Nz0<provides validator for interface {interface!r}>r_   )r2   r_   r;   s    r    r<   z_ProvidesValidator.__repr__   s"    AHHnn I 
 	
r!   N)r=   r>   r?   r	   r_   r8   r<   r#   r!   r    r]   r]      s    I
r!   r]   c                     t        |       S )a)  
    A validator that raises a `TypeError` if the initializer is called
    with an object that does not provide the requested *interface* (checks are
    performed using ``interface.providedBy(value)`` (see `zope.interface
    <https://zopeinterface.readthedocs.io/en/latest/>`_).

    :param interface: The interface to check for.
    :type interface: ``zope.interface.Interface``

    :raises TypeError: With a human readable error message, the attribute
        (of type `attrs.Attribute`), the expected interface, and the
        value it got.
    )r]   rb   s    r    r   r      s     i((r!   c                   &    e Zd Z e       Zd Zd Zy)_OptionalValidatorc                 0    |y | j                  |||       y N	validatorr4   s       r    r8   z_OptionalValidator.__call__  s    =tT5)r!   c                 L    dj                  t        | j                              S )Nz'<optional validator for {what} or None>)what)r2   r%   ri   r;   s    r    r<   z_OptionalValidator.__repr__  s'    8??dnn% @ 
 	
r!   N)r=   r>   r?   r	   ri   r8   r<   r#   r!   r    re   re      s    I*
r!   re   c                 `    t        | t              rt        t        |             S t        |       S )a  
    A validator that makes an attribute optional.  An optional attribute is one
    which can be set to ``None`` in addition to satisfying the requirements of
    the sub-validator.

    :param validator: A validator (or a list of validators) that is used for
        non-``None`` values.
    :type validator: callable or `list` of callables.

    .. versionadded:: 15.1.0
    .. versionchanged:: 17.1.0 *validator* can be a list of validators.
    )r0   listre   r   rh   s    r    r   r     s)     )T"!-	":;;i((r!   c                   &    e Zd Z e       Zd Zd Zy)_InValidatorc                     	 || j                   v }|s>t        dj                  |j                  | j                   |      || j                   |      y # t        $ r d}Y Nw xY w)NFz/'{name}' must be in {options!r} (got {value!r}))r,   optionsr/   )rq   r1   rG   r2   r,   )r5   r6   r7   r/   
in_optionss        r    r8   z_InValidator.__call__$  st    	$,,.J AHHDLL I      	J	s   A AAc                 :    dj                  | j                        S )Nz(<in_ validator with options {options!r}>rq   )r2   rq   r;   s    r    r<   z_InValidator.__repr__4  s"    9@@LL A 
 	
r!   N)r=   r>   r?   r	   rq   r8   r<   r#   r!   r    ro   ro      s    hG 
r!   ro   c                     t        |       S )a  
    A validator that raises a `ValueError` if the initializer is called
    with a value that does not belong in the options provided.  The check is
    performed using ``value in options``.

    :param options: Allowed options.
    :type options: list, tuple, `enum.Enum`, ...

    :raises ValueError: With a human readable error message, the attribute (of
       type `attrs.Attribute`), the expected options, and the value it
       got.

    .. versionadded:: 17.1.0
    .. versionchanged:: 22.1.0
       The ValueError was incomplete until now and only contained the human
       readable error message. Now it contains all the information that has
       been promised since 17.1.0.
    )ro   rt   s    r    r   r   :  s    &   r!   c                       e Zd Zd Zd Zy)_IsCallableValidatorc                     t        |      s5d}t        |j                  |j                  ||j                        |      y)r+   z?'{name}' must be callable (got {value!r} that is a {actual!r}).)r,   r/   r.   )msgr/   N)callabler   r2   r,   r3   )r5   r6   r7   r/   messages        r    r8   z_IsCallableValidator.__call__R  sN     8  #NN% #  	  r!   c                      y)Nz<is_callable validator>r#   r;   s    r    r<   z_IsCallableValidator.__repr__b  s    (r!   N)r=   r>   r?   r8   r<   r#   r!   r    rw   rw   P  s     )r!   rw   c                      t               S )ax  
    A validator that raises a `attr.exceptions.NotCallableError` if the
    initializer is called with a value for this particular attribute
    that is not callable.

    .. versionadded:: 19.1.0

    :raises `attr.exceptions.NotCallableError`: With a human readable error
        message containing the attribute (`attrs.Attribute`) name,
        and the value it got.
    )rw   r#   r!   r    r   r   f  s      !!r!   c                   ^    e Zd Z e e             Z ed e e                   Zd Zd Z	y)_DeepIterablerh   Ndefaultri   c                 v    | j                   | j                  |||       |D ]  }| j                  |||        yr+   N)iterable_validatormember_validator)r5   r6   r7   r/   members        r    r8   z_DeepIterable.__call__|  sC     "".##D$6 	6F!!$f5	6r!   c                     | j                   dndj                  | j                         }dj                  || j                        S )Nr   z {iterable!r})iterablezJ<deep_iterable validator for{iterable_identifier} iterables of {member!r}>)iterable_identifierr   )r   r2   r   )r5   r   s     r    r<   z_DeepIterable.__repr__  sV     &&.  ''1H1H'I 	(
& 3((  
	
r!   )
r=   r>   r?   r	   r   r   r   r   r8   r<   r#   r!   r    r   r   u  s0    6 76
r!   r   c                 V    t        | t        t        f      rt        |  } t	        | |      S )a4  
    A validator that performs deep validation of an iterable.

    :param member_validator: Validator(s) to apply to iterable members
    :param iterable_validator: Validator to apply to iterable itself
        (optional)

    .. versionadded:: 19.1.0

    :raises TypeError: if any sub-validators fail
    )r0   rm   tupler   r   )r   r   s     r    r   r     s.     "T5M2!12)+=>>r!   c                   z    e Zd Z e e             Z e e             Z ed e e                   Zd Z	d Z
y)_DeepMappingrh   Nr   c                     | j                   | j                  |||       |D ]+  }| j                  |||       | j                  ||||          - yr   )mapping_validatorkey_validatorvalue_validator)r5   r6   r7   r/   keys        r    r8   z_DeepMapping.__call__  sY     !!-""4u5 	9CtT3/  tU3Z8	9r!   c                 P    dj                  | j                  | j                        S )NzA<deep_mapping validator for objects mapping {key!r} to {value!r}>)r   r/   )r2   r   r   r;   s    r    r<   z_DeepMapping.__repr__  s%    O
&T''t/C/C&
D	Er!   )r=   r>   r?   r	   r   r   r   r   r   r8   r<   r#   r!   r    r   r     s9    []3M{}5Otx7NO	9Er!   r   c                     t        | ||      S )a}  
    A validator that performs deep validation of a dictionary.

    :param key_validator: Validator to apply to dictionary keys
    :param value_validator: Validator to apply to dictionary values
    :param mapping_validator: Validator to apply to top-level mapping
        attribute (optional)

    .. versionadded:: 19.1.0

    :raises TypeError: if any sub-validators fail
    )r   )r   r   r   s      r    r   r     s     8IJJr!   c                   B    e Zd Z e       Z e       Z e       Zd Zd Zy)_NumberValidatorc                     | j                  || j                        s<t        dj                  |j                  | j
                  | j                  |            y)r+   z&'{name}' must be {op} {bound}: {value})r,   opboundr/   N)compare_funcr   rG   r2   r,   
compare_opr4   s       r    r8   z_NumberValidator.__call__  sV       

38??**	 @   4r!   c                 P    dj                  | j                  | j                        S )Nz<Validator for x {op} {bound}>)r   r   )r2   r   r   r;   s    r    r<   z_NumberValidator.__repr__  s'    /66djj 7 
 	
r!   N)	r=   r>   r?   r	   r   r   r   r8   r<   r#   r!   r    r   r     s!    HEJ8L
r!   r   c                 8    t        | dt        j                        S )z
    A validator that raises `ValueError` if the initializer is called
    with a number larger or equal to *val*.

    :param val: Exclusive upper bound for values

    .. versionadded:: 21.3.0
    <)r   operatorr   vals    r    r   r          Chkk22r!   c                 8    t        | dt        j                        S )z
    A validator that raises `ValueError` if the initializer is called
    with a number greater than *val*.

    :param val: Inclusive upper bound for values

    .. versionadded:: 21.3.0
    z<=)r   r   r   r   s    r    r   r          Cx{{33r!   c                 8    t        | dt        j                        S )z
    A validator that raises `ValueError` if the initializer is called
    with a number smaller than *val*.

    :param val: Inclusive lower bound for values

    .. versionadded:: 21.3.0
    z>=)r   r   r   r   s    r    r   r     r   r!   c                 8    t        | dt        j                        S )z
    A validator that raises `ValueError` if the initializer is called
    with a number smaller or equal to *val*.

    :param val: Exclusive lower bound for values

    .. versionadded:: 21.3.0
    >)r   r   r   r   s    r    r   r     r   r!   c                   &    e Zd Z e       Zd Zd Zy)_MaxLengthValidatorc           	          t        |      | j                  kD  r:t        dj                  |j                  | j                  t        |                  y)r+   z*Length of '{name}' must be <= {max}: {len})r,   maxlenN)r   
max_lengthrG   r2   r,   r4   s       r    r8   z_MaxLengthValidator.__call__  M     u:'<CCSZ D   (r!   c                 :    dj                  | j                        S )Nz<max_len validator for {max}>)r   )r2   r   r;   s    r    r<   z_MaxLengthValidator.__repr__&      .55$//5JJr!   N)r=   r>   r?   r	   r   r8   r<   r#   r!   r    r   r         J	Kr!   r   c                     t        |       S )z
    A validator that raises `ValueError` if the initializer is called
    with a string or iterable that is longer than *length*.

    :param int length: Maximum length of the string or iterable

    .. versionadded:: 21.3.0
    )r   lengths    r    r   r   *       v&&r!   c                   &    e Zd Z e       Zd Zd Zy)_MinLengthValidatorc           	          t        |      | j                  k  r:t        dj                  |j                  | j                  t        |                  y)r+   z*Length of '{name}' must be => {min}: {len})r,   minr   N)r   
min_lengthrG   r2   r,   r4   s       r    r8   z_MinLengthValidator.__call__:  r   r!   c                 :    dj                  | j                        S )Nz<min_len validator for {min}>)r   )r2   r   r;   s    r    r<   z_MinLengthValidator.__repr__E  r   r!   N)r=   r>   r?   r	   r   r8   r<   r#   r!   r    r   r   6  r   r!   r   c                     t        |       S )z
    A validator that raises `ValueError` if the initializer is called
    with a string or iterable that is shorter than *length*.

    :param int length: Minimum length of the string or iterable

    .. versionadded:: 22.1.0
    )r   r   s    r    r   r   I  r   r!   )r   Nrg   )0__doc__r   rO   
contextlibr   _configr   r   _maker   r   r	   r
   
exceptionsr   rV   AttributeErrorr-   rW   __all__r   r   r   r)   r   rC   r   r]   r   re   r   ro   r   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r#   r!   r    <module>r      sA  
  	 % ; 5 5 (#jjG
,%$	$ ! !" ED)
 
 *
6&  E$d+
 
 ,
2-4` ED)
 
 *
0)" ED)
 
 *
)$ ED)
 
 *
2!, ET*) ) +)*" ED)
 
 *
>?" ED)E E *E,K  E$d+
 
 ,
2	3	4	4	3 E$d+K K ,K$	' E$d+K K ,K$	'k  #:2::b>"G#s   F   F! F!