U
    ?hNN                     @   s  d Z ddlZddlZddlm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 dd	lmZ d
ddddddddddddddddddgZdd Zdd Zedd Zed d!d!d"G d#d$ d$Zd%d Zed d!d!d&G d'd( d(ZdPd)dZed d!d!d"G d*d+ d+Zd,d Zed d!d!d"G d-d. d.Zd/d Zed d d!d"G d0d1 d1Zd2d Zed d!d!d"G d3d4 d4Z dQd5dZ!ed d!d!d"G d6d7 d7Z"dRd8dZ#ed d!d!d&G d9d: d:Z$d;d Z%d<d Z&d=d Z'd>d Z(ed d!d!d&G d?d@ d@Z)dAd Z*ed d!d!d&G dBdC dCZ+dDd Z,ed d!d!d"G dEdF dFZ-dGdH Z.ed d!d!d"G dIdJ dJZ/de0e1fdKdLdZ2ed d!d!d"G dMdN dNZ3dOd Z4dS )Sz
Commonly useful validators.
    N)contextmanager)Pattern   )get_run_validatorsset_run_validators)_AndValidatorand_attribattrs)default_if_none)NotCallableErrorr   deep_iterabledeep_mappingdisabledgeget_disabledgtin_instance_ofis_callablelelt
matches_remax_lenmin_lennot_optionalor_set_disabledc                 C   s   t |   dS )a  
    Globally disable or enable running validators.

    By default, they are run.

    Args:
        disabled (bool): If `True`, disable running all validators.

    .. warning::

        This function is not thread-safe!

    .. versionadded:: 21.3.0
    Nr   )r    r    A/var/www/html/venv/lib/python3.8/site-packages/attr/validators.pyr   *   s    c                   C   s   t   S )z
    Return a bool indicating whether validators are currently disabled or not.

    Returns:
        bool:`True` if validators are currently disabled.

    .. versionadded:: 21.3.0
    )r   r    r    r    r!   r   <   s    	c                   c   s"   t d z
dV  W 5 t d X dS )z
    Context manager that disables running validators within its context.

    .. warning::

        This context manager is not thread-safe!

    .. versionadded:: 21.3.0
    FTNr   r    r    r    r!   r   H   s    
FT)reprslotsunsafe_hashc                   @   s"   e Zd Ze Zdd Zdd ZdS )_InstanceOfValidatorc              	   C   sD   t || js@d|j d| jd|d|jd	}t||| j|dS )P
        We use a callable class to be able to change the ``__repr__``.
        '
' must be  (got z that is a ).N)
isinstancetypename	__class__	TypeErrorselfinstattrvaluemsgr    r    r!   __call__^   s    $z_InstanceOfValidator.__call__c                 C   s   d| j dS )Nz <instance_of validator for type >r,   r1   r    r    r!   __repr__k   s    z_InstanceOfValidator.__repr__N__name__
__module____qualname__r	   r,   r6   r:   r    r    r    r!   r%   Z   s   r%   c                 C   s   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).

    Args:
        type (type | tuple[type]): The type to check for.

    Raises:
        TypeError:
            With a human readable error message, the attribute (of type
            `attrs.Attribute`), the expected type, and the value it got.
    )r%   r8   r    r    r!   r   o   s    )r"   frozenr#   c                   @   s(   e Zd Ze Ze Zdd Zdd ZdS )_MatchesReValidatorc                 C   s<   |  |s8d|j d| jjd|d}t||| j|dS )r&   r'   z' must match regex z (z	 doesn't)N)
match_funcr-   pattern
ValueErrorr0   r    r    r!   r6      s    
z_MatchesReValidator.__call__c                 C   s   d| j dS )Nz"<matches_re validator for pattern r7   )rB   r9   r    r    r!   r:      s    z_MatchesReValidator.__repr__N)r<   r=   r>   r	   rB   rA   r6   r:   r    r    r    r!   r@      s   r@   c                 C   s   t jdt jt jf}||krDddtdd t|D }t|t	| t
rd|r^d}t|| }nt | |}|t jkr|j}n|t jk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*.

    Args:
        regex (str, re.Pattern):
            A regex string or precompiled pattern to match against

        flags (int):
            Flags that will be passed to the underlying re function (default 0)

        func (typing.Callable):
            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                 s   s   | ]}|r|j pd V  qdS )NoneN)r<   ).0er    r    r!   	<genexpr>   s     zmatches_re.<locals>.<genexpr>zR'flags' can only be used with a string pattern; pass flags to re.compile() instead)re	fullmatchsearchmatchformatjoinsortedsetrC   r+   r   r/   compiler@   )regexflagsfuncZvalid_funcsr5   rB   rA   r    r    r!   r      s(    


c                   @   s"   e Zd Ze Zdd Zdd ZdS )_OptionalValidatorc                 C   s   |d krd S |  ||| d S )N	validatorr1   r2   r3   r4   r    r    r!   r6      s    z_OptionalValidator.__call__c                 C   s   d| j dS )Nz<optional validator for z	 or None>rU   r9   r    r    r!   r:      s    z_OptionalValidator.__repr__N)r<   r=   r>   r	   rV   r6   r:   r    r    r    r!   rT      s   rT   c                 C   s"   t | ttf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.

    Args:
        validator
            (typing.Callable | tuple[typing.Callable] | list[typing.Callable]):
            A validator (or validators) that is used for non-`None` values.

    .. versionadded:: 15.1.0
    .. versionchanged:: 17.1.0 *validator* can be a list of validators.
    .. versionchanged:: 23.1.0 *validator* can also be a tuple of validators.
    )r+   listtuplerT   r   rU   r    r    r!   r      s    c                   @   s,   e Zd Ze ZeddZdd Zdd ZdS )_InValidatorF)hashc                 C   s\   z|| j k}W n tk
r&   d}Y nX |sXd|j d| jd|d}t||| j|d S )NFr'   z' must be in r)   ))optionsr/   r-   _original_optionsrC   )r1   r2   r3   r4   Z
in_optionsr5   r    r    r!   r6      s    
z_InValidator.__call__c                 C   s   d| j dS )Nz<in_ validator with options r7   )r^   r9   r    r    r!   r:      s    z_InValidator.__repr__N)r<   r=   r>   r	   r]   r^   r6   r:   r    r    r    r!   rZ      s   
rZ   c                 C   s&   | }t | tttfrt| } 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``, so *options* has to
    support that operation.

    To keep the validator hashable, dicts, lists, and sets are transparently
    transformed into a `tuple`.

    Args:
        options: Allowed options.

    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.
    .. versionchanged:: 24.1.0
       *options* that are a list, dict, or a set are now transformed into a
       tuple to keep the validator hashable.
    )r+   rX   dictrO   rY   rZ   )r]   Zrepr_optionsr    r    r!   r     s    c                   @   s   e Zd Zdd Zdd ZdS )_IsCallableValidatorc                 C   s,   t |s(d}t|j|j||jd|ddS )r&   z?'{name}' must be callable (got {value!r} that is a {actual!r}).)r-   r4   actual)r5   r4   N)callabler   rL   r-   r.   )r1   r2   r3   r4   messager    r    r!   r6   &  s      z_IsCallableValidator.__call__c                 C   s   dS )Nz<is_callable validator>r    r9   r    r    r!   r:   6  s    z_IsCallableValidator.__repr__N)r<   r=   r>   r6   r:   r    r    r    r!   r`   $  s   r`   c                   C   s   t  S )a  
    A validator that raises a `attrs.exceptions.NotCallableError` if the
    initializer is called with a value for this particular attribute that is
    not callable.

    .. versionadded:: 19.1.0

    Raises:
        attrs.exceptions.NotCallableError:
            With a human readable error message containing the attribute
            (`attrs.Attribute`) name, and the value it got.
    )r`   r    r    r    r!   r   :  s    c                   @   s:   e Zd Zee dZedee dZdd Zdd Z	dS )_DeepIterablerU   NdefaultrV   c                 C   s4   | j dk	r|  ||| |D ]}| ||| qdS r&   Niterable_validatormember_validator)r1   r2   r3   r4   memberr    r    r!   r6   Q  s    
z_DeepIterable.__call__c                 C   s.   | j d krdn
d| j }d| d| jdS )N  z<deep_iterable validator forz iterables of r7   rh   )r1   Ziterable_identifierr    r    r!   r:   [  s    
z_DeepIterable.__repr__)
r<   r=   r>   r	   r   rj   r   ri   r6   r:   r    r    r    r!   rd   J  s    
rd   c                 C   s    t | ttfrt|  } t| |S )aC  
    A validator that performs deep validation of an iterable.

    Args:
        member_validator: Validator to apply to iterable members.

        iterable_validator:
            Validator to apply to iterable itself (optional).

    Raises
        TypeError: if any sub-validators fail

    .. versionadded:: 19.1.0
    )r+   rX   rY   r   rd   rj   ri   r    r    r!   r   g  s    c                   @   sF   e Zd Zee dZee dZedee dZdd Z	dd Z
dS )_DeepMappingrU   Nre   c                 C   sF   | j dk	r|  ||| |D ]$}| ||| | ||||  qdS rg   )mapping_validatorkey_validatorvalue_validator)r1   r2   r3   r4   keyr    r    r!   r6     s
    
z_DeepMapping.__call__c                 C   s   d| j d| jdS )Nz,<deep_mapping validator for objects mapping z to r7   )rq   rr   r9   r    r    r!   r:     s    z_DeepMapping.__repr__)r<   r=   r>   r	   r   rq   rr   r   rp   r6   r:   r    r    r    r!   ro   {  s
   ro   c                 C   s   t | ||S )a  
    A validator that performs deep validation of a dictionary.

    Args:
        key_validator: Validator to apply to dictionary keys.

        value_validator: Validator to apply to dictionary values.

        mapping_validator:
            Validator to apply to top-level mapping attribute (optional).

    .. versionadded:: 19.1.0

    Raises:
        TypeError: if any sub-validators fail
    )ro   )rq   rr   rp   r    r    r!   r     s    c                   @   s.   e Zd Ze Ze Ze Zdd Zdd ZdS )_NumberValidatorc                 C   s<   |  || js8d|j d| j d| j d| }t|dS )r&   r'   r(   rm   : N)compare_funcboundr-   
compare_oprC   r0   r    r    r!   r6     s    "z_NumberValidator.__call__c                 C   s   d| j  d| j dS )Nz<Validator for x rm   r7   )rx   rw   r9   r    r    r!   r:     s    z_NumberValidator.__repr__N)	r<   r=   r>   r	   rw   rx   rv   r6   r:   r    r    r    r!   rt     s
   rt   c                 C   s   t | dtjS )a  
    A validator that raises `ValueError` if the initializer is called with a
    number larger or equal to *val*.

    The validator uses `operator.lt` to compare the values.

    Args:
        val: Exclusive upper bound for values.

    .. versionadded:: 21.3.0
    <)rt   operatorr   valr    r    r!   r     s    c                 C   s   t | dtjS )a  
    A validator that raises `ValueError` if the initializer is called with a
    number greater than *val*.

    The validator uses `operator.le` to compare the values.

    Args:
        val: Inclusive upper bound for values.

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

    The validator uses `operator.ge` to compare the values.

    Args:
        val: Inclusive lower bound for values

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

    The validator uses `operator.ge` to compare the values.

    Args:
       val: Exclusive lower bound for values

    .. versionadded:: 21.3.0
    r7   )rt   rz   r   r{   r    r    r!   r     s    c                   @   s"   e Zd Ze Zdd Zdd ZdS )_MaxLengthValidatorc                 C   s8   t || jkr4d|j d| j dt | }t|dS )r&   Length of 'z' must be <= ru   N)len
max_lengthr-   rC   r0   r    r    r!   r6     s    z_MaxLengthValidator.__call__c                 C   s   d| j  dS )Nz<max_len validator for r7   )r   r9   r    r    r!   r:     s    z_MaxLengthValidator.__repr__N)r<   r=   r>   r	   r   r6   r:   r    r    r    r!   r}     s   r}   c                 C   s   t | S )z
    A validator that raises `ValueError` if the initializer is called
    with a string or iterable that is longer than *length*.

    Args:
        length (int): Maximum length of the string or iterable

    .. versionadded:: 21.3.0
    )r}   lengthr    r    r!   r     s    
c                   @   s"   e Zd Ze Zdd Zdd ZdS )_MinLengthValidatorc                 C   s8   t || jk r4d|j d| j dt | }t|dS )r&   r~   z' must be >= ru   N)r   
min_lengthr-   rC   r0   r    r    r!   r6     s    z_MinLengthValidator.__call__c                 C   s   d| j  dS )Nz<min_len validator for r7   )r   r9   r    r    r!   r:     s    z_MinLengthValidator.__repr__N)r<   r=   r>   r	   r   r6   r:   r    r    r    r!   r     s   r   c                 C   s   t | S )z
    A validator that raises `ValueError` if the initializer is called
    with a string or iterable that is shorter than *length*.

    Args:
        length (int): Minimum length of the string or iterable

    .. versionadded:: 22.1.0
    )r   r   r    r    r!   r     s    
c                   @   s"   e Zd Ze Zdd Zdd ZdS )_SubclassOfValidatorc                 C   s<   t || js8d|j d| jd|d}t||| j|dS )r&   r'   z' must be a subclass of r)   r*   N)
issubclassr,   r-   r/   r0   r    r    r!   r6   0  s    z_SubclassOfValidator.__call__c                 C   s   d| j dS )Nz <subclass_of validator for type r7   r8   r9   r    r    r!   r:   =  s    z_SubclassOfValidator.__repr__Nr;   r    r    r    r!   r   ,  s   r   c                 C   s   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
    `issubclass` therefore it's also valid to pass a tuple of types).

    Args:
        type (type | tuple[type, ...]): The type(s) to check for.

    Raises:
        TypeError:
            With a human readable error message, the attribute (of type
            `attrs.Attribute`), the expected type, and the value it got.
    )r   r8   r    r    r!   _subclass_ofA  s    r   c                   @   sJ   e Zd Ze ZeeddZeeee	e
eddZdd Zdd Zd	S )
_NotValidatorzCnot_ validator child '{validator!r}' did not raise a captured error)	converterrn   rU   c                 C   sR   z|  ||| W n | jk
r(   Y n&X t| jj| j | jd|| j || jd S )NrV   	exc_types)rV   r   rC   r5   rL   rW   r    r    r!   r6   b  s    z_NotValidator.__call__c                 C   s   d| j d| jdS )Nz<not_ validator wrapping z, capturing r7   r   r9   r    r    r!   r:   s  s    z_NotValidator.__repr__N)r<   r=   r>   r	   rV   r   r5   r   r   	Exceptionr   rY   r   r6   r:   r    r    r    r!   r   R  s   r   )r5   r   c                C   s4   zt |}W n tk
r&   |f}Y nX t| ||S )a:  
    A validator that wraps and logically 'inverts' the validator passed to it.
    It will raise a `ValueError` if the provided validator *doesn't* raise a
    `ValueError` or `TypeError` (by default), and will suppress the exception
    if the provided validator *does*.

    Intended to be used with existing validators to compose logic without
    needing to create inverted variants, for example, ``not_(in_(...))``.

    Args:
        validator: A validator to be logically inverted.

        msg (str):
            Message to raise if validator fails. Formatted with keys
            ``exc_types`` and ``validator``.

        exc_types (tuple[type, ...]):
            Exception type(s) to capture. Other types raised by child
            validators will not be intercepted and pass through.

    Raises:
        ValueError:
            With a human readable error message, the attribute (of type
            `attrs.Attribute`), the validator that failed to raise an
            exception, the value it got, and the expected exception types.

    .. versionadded:: 22.2.0
    )rY   r/   r   )rV   r5   r   r    r    r!   r   w  s
    c                   @   s"   e Zd Ze Zdd Zdd ZdS )_OrValidatorc              	   C   sZ   | j D ]4}z|||| W n tk
r2   Y qY qX  d S qd| j d|}t|d S )NzNone of z satisfied for value )
validatorsr   rC   )r1   r2   r3   r4   vr5   r    r    r!   r6     s    

z_OrValidator.__call__c                 C   s   d| j dS )Nz<or validator wrapping r7   )r   r9   r    r    r!   r:     s    z_OrValidator.__repr__N)r<   r=   r>   r	   r   r6   r:   r    r    r    r!   r     s   r   c                  G   s6   g }| D ] }| t|tr |jn|g qtt|S )a  
    A validator that composes multiple validators into one.

    When called on a value, it runs all wrapped validators until one of them is
    satisfied.

    Args:
        validators (~collections.abc.Iterable[typing.Callable]):
            Arbitrary number of validators.

    Raises:
        ValueError:
            If no validator is satisfied. Raised with a human-readable error
            message listing all the wrapped validators and the value that
            failed all of them.

    .. versionadded:: 24.1.0
    )extendr+   r   r   rY   )r   valsr   r    r    r!   r     s    )r   N)N)N)5__doc__rz   rH   
contextlibr   r   _configr   r   _maker   r   r	   r
   
convertersr   
exceptionsr   __all__r   r   r   r%   r   r@   r   rT   r   rZ   r   r`   r   rd   r   ro   r   rt   r   r   r   r   r}   r   r   r   r   r   r   rC   r/   r   r   r   r    r    r    r!   <module>   s   

0#

$$