o
    iP                     @   s  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 ddlmZ zejZW n eyE   eed	ZY nw g d
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 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 d-d. d.Z%dQd/d0Z&eddddG d1d2 d2Z'dQd3d4Z(eddddG d5d6 d6Z)d7d8 Z*d9d: Z+d;d< Z,d=d> Z-eddddG d?d@ d@Z.dAdB Z/eddddG dCdD dDZ0dEdF Z1eddddG dGdH dHZ2dIdJ Z3eddddG dKdL dLZ4de5e6fdMdNdOZ7dS )Rz
Commonly useful validators.
    N)contextmanager   )get_run_validatorsset_run_validators)_AndValidatorand_attribattrs)default_if_none)NotCallableError )r   deep_iterabledeep_mappingdisabledgeget_disabledgtin_instance_ofis_callablelelt
matches_remax_lenmin_lennot_optionalprovidesset_disabledc                 C   s   t |   dS )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    r    J/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/attr/validators.pyr   0   s   r   c                   C   s   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!   r   B      	r   c                   c   s*    t d z
dV  W t d dS t d 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   N   s
   r   FT)reprslotshashc                   @   "   e Zd Ze Zdd Zdd ZdS )_InstanceOfValidatorc                 C   s4   t || jstdj|j| j|j|d|| j|dS )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,   r    r    r!   __call__d   s   z_InstanceOfValidator.__call__c                 C      dj | jdS )Nz)<instance_of validator for type {type!r}>r*   r/   r*   r2   r    r    r!   __repr__v      z_InstanceOfValidator.__repr__N__name__
__module____qualname__r   r*   r5   r:   r    r    r    r!   r'   `   s    r'   c                 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 type

    :raises TypeError: With a human readable error message, the attribute
        (of type `attrs.Attribute`), the expected type, and the value it
        got.
    )r'   r7   r    r    r!   r   |      r   )r#   frozenr$   c                   @   s(   e Zd Ze Ze Zdd Zdd ZdS )_MatchesReValidatorc                 C   s0   |  |stdj|j| jj|d|| j|dS )r(   z9'{name}' must match regex {pattern!r} ({value!r} doesn't))r)   patternr,   N)
match_func
ValueErrorr/   r)   rD   r1   r    r    r!   r5      s   
z_MatchesReValidator.__call__c                 C   r6   )Nz.<matches_re validator for pattern {pattern!r}>)rD   )r/   rD   r9   r    r    r!   r:      r;   z_MatchesReValidator.__repr__N)r=   r>   r?   r   rD   rE   r5   r:   r    r    r    r!   rC      s
    rC   c              	   C   s   t jdt jt jf}||vr tddtdd t|D t	| t
r.|r+td| }nt | |}|t ju r=|j}n|t ju rF|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                 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matchrF   r/   joinsortedsetr-   Patternr.   compilerC   )regexflagsfuncvalid_funcsrD   rE   r    r    r!   r      s0   




r   c                   @   r&   )_ProvidesValidatorc                 C   s0   | j |stdj|j| j |d|| j |dS )r(   z<'{name}' must provide {interface!r} which {value!r} doesn't.)r)   	interfacer,   N)rY   
providedByr.   r/   r)   r1   r    r    r!   r5      s   
z_ProvidesValidator.__call__c                 C   r6   )Nz0<provides validator for interface {interface!r}>rY   )r/   rY   r9   r    r    r!   r:      r;   z_ProvidesValidator.__repr__N)r=   r>   r?   r   rY   r5   r:   r    r    r    r!   rX      s    rX   c                 C   r@   )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.
    )rX   r[   r    r    r!   r      s   r   c                   @   r&   )_OptionalValidatorc                 C   s   |d u rd S |  ||| d S N	validatorr1   r    r    r!   r5     s   z_OptionalValidator.__call__c                 C   s   dj t| jdS )Nz'<optional validator for {what} or None>)what)r/   r#   r_   r9   r    r    r!   r:   
     z_OptionalValidator.__repr__N)r=   r>   r?   r   r_   r5   r:   r    r    r    r!   r\      s    r\   c                 C   s   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.
    )r-   listr\   r   r^   r    r    r!   r     s   
r   c                   @   r&   )_InValidatorc                 C   sN   z|| j v }W n ty   d}Y nw |s%tdj|j| j |d|| j |d S )NFz/'{name}' must be in {options!r} (got {value!r}))r)   optionsr,   )rd   r.   rF   r/   r)   )r2   r3   r4   r,   
in_optionsr    r    r!   r5   &  s   
z_InValidator.__call__c                 C   r6   )Nz(<in_ validator with options {options!r}>rd   )r/   rd   r9   r    r    r!   r:   6  r;   z_InValidator.__repr__N)r=   r>   r?   r   rd   r5   r:   r    r    r    r!   rc   "  s    rc   c                 C   r@   )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.
    )rc   rf   r    r    r!   r   <  s   r   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)   r,   r+   )msgr,   N)callabler   r/   r)   r0   )r2   r3   r4   r,   messager    r    r!   r5   T  s   
z_IsCallableValidator.__call__c                 C   s   dS )Nz<is_callable validator>r    r9   r    r    r!   r:   d  s   z_IsCallableValidator.__repr__N)r=   r>   r?   r5   r:   r    r    r    r!   rg   R  s    rg   c                   C   s   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.
    )rg   r    r    r    r!   r   h  s   r   c                   @   s:   e Zd Zee dZedee dZdd Zdd Z	dS )_DeepIterabler^   Ndefaultr_   c                 C   s4   | j dur|  ||| |D ]	}| ||| qdS r(   N)iterable_validatormember_validator)r2   r3   r4   r,   memberr    r    r!   r5   ~  s
   
z_DeepIterable.__call__c                 C   s*   | j d u rdnd| j }dj|| jdS )Nr    zJ<deep_iterable validator for{iterable_identifier} iterables of {member!r}>)iterable_identifierrq   )ro   r/   rp   )r2   rs   r    r    r!   r:     s   

z_DeepIterable.__repr__)
r=   r>   r?   r   r   rp   r   ro   r5   r:   r    r    r    r!   rk   w  s    

rk   c                 C   s    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
    )r-   rb   tupler   rk   rp   ro   r    r    r!   r     s   
r   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 )_DeepMappingr^   Nrl   c                 C   sF   | j dur|  ||| |D ]}| ||| | ||||  qdS rn   )mapping_validatorkey_validatorvalue_validator)r2   r3   r4   r,   keyr    r    r!   r5     s   
z_DeepMapping.__call__c                 C      dj | j| jdS )NzA<deep_mapping validator for objects mapping {key!r} to {value!r}>)rz   r,   )r/   rx   ry   r9   r    r    r!   r:     s   
z_DeepMapping.__repr__)r=   r>   r?   r   r   rx   ry   r   rw   r5   r:   r    r    r    r!   rv     s    rv   c                 C   s   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
    )rv   )rx   ry   rw   r    r    r!   r     s   r   c                   @   s.   e Zd Ze Ze Ze Zdd Zdd ZdS )_NumberValidatorc                 C   s.   |  || jstdj|j| j| j|ddS )r(   z&'{name}' must be {op} {bound}: {value})r)   opboundr,   N)compare_funcr~   rF   r/   r)   
compare_opr1   r    r    r!   r5     s   z_NumberValidator.__call__c                 C   r{   )Nz<Validator for x {op} {bound}>)r}   r~   )r/   r   r~   r9   r    r    r!   r:     ra   z_NumberValidator.__repr__N)	r=   r>   r?   r   r~   r   r   r5   r:   r    r    r    r!   r|     s    r|   c                 C      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   valr    r    r!   r        	r   c                 C   r   )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   r    r    r!   r     r   r   c                 C   r   )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   r    r    r!   r     r   r   c                 C   r   )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   r    r    r!   r     r   r   c                   @   r&   )_MaxLengthValidatorc                 C   s.   t || jkrtdj|j| jt |ddS )r(   z*Length of '{name}' must be <= {max}: {len})r)   maxlenN)r   
max_lengthrF   r/   r)   r1   r    r    r!   r5        z_MaxLengthValidator.__call__c                 C      d| j  dS )Nz<max_len validator for r   )r   r9   r    r    r!   r:   (     z_MaxLengthValidator.__repr__N)r=   r>   r?   r   r   r5   r:   r    r    r    r!   r         r   c                 C   r@   )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   lengthr    r    r!   r   ,  r"   r   c                   @   r&   )_MinLengthValidatorc                 C   s.   t || jk rtdj|j| jt |ddS )r(   z*Length of '{name}' must be => {min}: {len})r)   minr   N)r   
min_lengthrF   r/   r)   r1   r    r    r!   r5   <  r   z_MinLengthValidator.__call__c                 C   r   )Nz<min_len validator for r   )r   r9   r    r    r!   r:   G  r   z_MinLengthValidator.__repr__N)r=   r>   r?   r   r   r5   r:   r    r    r    r!   r   8  r   r   c                 C   r@   )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   r    r    r!   r   K  r"   r   c                   @   r&   )_SubclassOfValidatorc                 C   s0   t || jstdj|j| j|d|| j|dS )r(   z8'{name}' must be a subclass of {type!r} (got {value!r}).)r)   r*   r,   N)
issubclassr*   r.   r/   r)   r1   r    r    r!   r5   [  s   z_SubclassOfValidator.__call__c                 C   r6   )Nz)<subclass_of validator for type {type!r}>r7   r8   r9   r    r    r!   r:   l  r;   z_SubclassOfValidator.__repr__Nr<   r    r    r    r!   r   W  s    r   c                 C   r@   )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).

    :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   r7   r    r    r!   _subclass_ofr  rA   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)	converterru   r^   c                 C   sN   z	|  ||| W n | jy   Y d S w t| jj| j | jd|| j || j)N)r_   	exc_types)r_   r   rF   rh   r/   r1   r    r    r!   r5     s   z_NotValidator.__call__c                 C   r{   )Nz;<not_ validator wrapping {what!r}, capturing {exc_types!r}>)r`   r   )r/   r_   r   r9   r    r    r!   r:     s
   z_NotValidator.__repr__N)r=   r>   r?   r   r_   r
   rh   r   r   	Exceptionr   rt   r   r5   r:   r    r    r    r!   r     s    r   )rh   r   c                C   s2   zt |}W n ty   |f}Y nw 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_(...))``.

    :param validator: A validator to be logically inverted.
    :param msg: Message to raise if validator fails.
        Formatted with keys ``exc_types`` and ``validator``.
    :type msg: str
    :param exc_types: 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
    )rt   r.   r   )r_   rh   r   r    r    r!   r     s   
r   )r   Nr]   )8__doc__r   rK   
contextlibr   _configr   r   _maker   r   r   r	   
convertersr
   
exceptionsr   rR   AttributeErrorr*   rS   __all__r   r   r   r'   r   rC   r   rX   r   r\   r   rc   r   rg   r   rk   r   rv   r   r|   r   r   r   r   r   r   r   r   r   r   r   rF   r.   r   r    r    r    r!   <module>   sx   


0

)