a
    &ñ»j1T  ã                   @   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 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 dQd+d,„Z!edddd�G d-d.„ d.ƒƒZ"dRd/d0„Z#edddd�G d1d2„ d2ƒƒZ$d3d4„ Z%d5d6„ Z&d7d8„ Z'd9d:„ Z(edddd�G d;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ƒƒZ-dEdF„ Z.edddd�G dGdH„ dHƒƒZ/de0e1fdIœdJdK„Z2edddd�G dLdM„ dMƒƒZ3dNdO„ Z4dS )Sz
Commonly useful validators.
é    N)Úcontextmanager)ÚPatterné   ©Ú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Ú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    ú5E:\soccer\ml_env\Lib\site-packages\attr/validators.pyr   *   s    r   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    	r   c                  c   s0   t ƒ } tdƒ zdV  W t| ƒ n
t| ƒ 0 dS )zï
    Context manager that disables running validators within its context.

    .. warning::

        This context manager is not thread-safe!

    .. versionadded:: 21.3.0
    .. versionchanged:: 26.1.0 The contextmanager is nestable.
    FNr   )Úprevr    r    r!   r   H   s
    r   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-   ©r2   r    r    r!   Ú__repr__m   s    z_InstanceOfValidator.__repr__N©Ú__name__Ú
__module__Ú__qualname__r
   r-   r7   r;   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
    `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&   r9   r    r    r!   r   q   s    r   )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Ú
ValueErrorr1   r    r    r!   r7   ‡   s    
üz_MatchesReValidator.__call__c                 C   s   d| j ›d�S )Nz"<matches_re validator for pattern r8   )rC   r:   r    r    r!   r;   ”   s    z_MatchesReValidator.__repr__N)r=   r>   r?   r
   rC   rB   r7   r;   r    r    r    r!   rA   ‚   s   rA   c                 C   s¤   t jdt jt jf}||vrDd d tdd„ t|ƒD ƒƒ¡¡}t|ƒ‚t	| t
ƒrd|r^d}t|ƒ‚| }nt  | |¡}|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*.

    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>±   ó    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ÚsetrD   r,   r   r0   ÚcompilerA   )ÚregexÚflagsÚfuncZvalid_funcsr6   rC   rB   r    r    r!   r   ˜   s(    ÿÿ


r   c                   @   s"   e Zd Zeƒ Zdd„ Zdd„ ZdS )Ú_OptionalValidatorc                 C   s   |d u rd S |   |||¡ d S )N©Ú	validator©r2   r3   r4   r5   r    r    r!   r7   Ì   s    z_OptionalValidator.__call__c                 C   s   d| j ›d�S )Nz<optional validator for z	 or None>rW   r:   r    r    r!   r;   Ò   s    z_OptionalValidator.__repr__N)r=   r>   r?   r
   rX   r7   r;   r    r    r    r!   rV   È   s   rV   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ÚtuplerV   r   rW   r    r    r!   r   Ö   s    r   c                   @   s,   e Zd Zeƒ Zedd�Zdd„ Zdd„ ZdS )Ú_InValidatorF)Úhashc                 C   sZ   z|| j v }W n ty$   d}Y n0 |sVd|j› d| j›d|›d�}t||| j|ƒ‚d S )NFr(   z' must be in r*   ú))Úoptionsr0   r.   Ú_original_optionsrD   )r2   r3   r4   r5   Z
in_optionsr6   r    r    r!   r7   ð   s    
üz_InValidator.__call__c                 C   s   d| j ›d�S )Nz<in_ validator with options r8   )r`   r:   r    r    r!   r;   ÿ   s    z_InValidator.__repr__N)r=   r>   r?   r
   r_   r`   r7   r;   r    r    r    r!   r\   ë   s   
r\   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,   rZ   ÚdictrQ   r[   r\   )r_   Zrepr_optionsr    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.   r5   Úactual)r6   r5   N)Úcallabler   rN   r.   r/   )r2   r3   r4   r5   Úmessager    r    r!   r7   (  s    ÿ
ÿüz_IsCallableValidator.__call__c                 C   s   dS )Nz<is_callable validator>r    r:   r    r    r!   r;   8  s    z_IsCallableValidator.__repr__N)r=   r>   r?   r7   r;   r    r    r    r!   rb   &  s   rb   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.
    )rb   r    r    r    r!   r   <  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 )Ú_DeepIterablerW   N)ÚdefaultrX   c                 C   s4   | j dur|   |||¡ |D ]}|  |||¡ qdS ©r'   N©Úiterable_validatorÚmember_validator)r2   r3   r4   r5   Úmemberr    r    r!   r7   S  s    
z_DeepIterable.__call__c                 C   s.   | j d u rdn
d| j ›�}d|› d| j›d�S )NÚ ú z<deep_iterable validator forz iterables of r8   ri   )r2   Ziterable_identifierr    r    r!   r;   ]  s    ÿ
ýÿÿz_DeepIterable.__repr__)
r=   r>   r?   r
   r   rk   r   rj   r7   r;   r    r    r    r!   rf   L  s   
ÿ
rf   c                 C   s6   t | ttfƒrt| Ž } t |ttfƒr,t|Ž }t| |ƒS )aÍ  
    A validator that performs deep validation of an iterable.

    Args:
        member_validator: Validator(s) to apply to iterable members.

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

    Raises
        TypeError: if any sub-validators fail

    .. versionadded:: 19.1.0

    .. versionchanged:: 25.4.0
       *member_validator* and *iterable_validator* can now be a list or tuple
       of validators.
    )r,   rZ   r[   r	   rf   ©rk   rj   r    r    r!   r   i  s
    r   c                   @   sL   e Zd Zeeeƒ ƒd�Zeeeƒ ƒd�Zeeeƒ ƒd�Zdd„ Z	dd„ Z
dS )Ú_DeepMappingrW   c                 C   sZ   | j dur|   |||¡ |D ]8}| jdur8|  |||¡ | jdur|  |||| ¡ qdS rh   )Úmapping_validatorÚkey_validatorÚvalue_validator)r2   r3   r4   r5   Úkeyr    r    r!   r7   ‰  s    


z_DeepMapping.__call__c                 C   s   d| j ›d| j›d�S )Nz,<deep_mapping validator for objects mapping z to r8   )rr   rs   r:   r    r    r!   r;   –  s    z_DeepMapping.__repr__N)r=   r>   r?   r
   r   r   rr   rs   rq   r7   r;   r    r    r    r!   rp   ƒ  s
   rp   c                 C   sj   | du r|du rd}t |ƒ‚t| ttfƒr2t| Ž } t|ttfƒrHt|Ž }t|ttfƒr^t|Ž }t| ||ƒS )a®  
    A validator that performs deep validation of a dictionary.

    All validators are optional, but at least one of *key_validator* or
    *value_validator* must be provided.

    Args:
        key_validator: Validator(s) to apply to dictionary keys.

        value_validator: Validator(s) to apply to dictionary values.

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

    .. versionadded:: 19.1.0

    .. versionchanged:: 25.4.0
       *key_validator* and *value_validator* are now optional, but at least one
       of them must be provided.

    .. versionchanged:: 25.4.0
       *key_validator*, *value_validator*, and *mapping_validator* can now be a
       list or tuple of validators.

    Raises:
        TypeError: If any sub-validator fails on validation.

        ValueError:
            If neither *key_validator* nor *value_validator* is provided on
            instantiation.
    NzAAt least one of key_validator or value_validator must be provided)rD   r,   rZ   r[   r	   rp   )rr   rs   rq   r6   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¡s8d|j› d| j› d| j› d|› �}t|ƒ‚dS )r'   r(   r)   rn   ú: N)Úcompare_funcÚboundr.   Ú
compare_oprD   r1   r    r    r!   r7   Ò  s    "z_NumberValidator.__call__c                 C   s   d| j › d| j› d�S )Nz<Validator for x rn   r8   )ry   rx   r:   r    r    r!   r;   Ú  s    z_NumberValidator.__repr__N)	r=   r>   r?   r
   rx   ry   rw   r7   r;   r    r    r    r!   ru   Ì  s
   ru   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
    ú<)ru   Úoperatorr   ©Úvalr    r    r!   r   Þ  s    r   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<=)ru   r{   r   r|   r    r    r!   r   í  s    r   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>=)ru   r{   r   r|   r    r    r!   r   ü  s    r   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.gt` to compare the values.

    Args:
       val: Exclusive lower bound for values

    .. versionadded:: 21.3.0
    r8   )ru   r{   r   r|   r    r    r!   r     s    r   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 <= rv   N)ÚlenÚ
max_lengthr.   rD   r1   r    r    r!   r7     s    z_MaxLengthValidator.__call__c                 C   s   d| j › d�S )Nz<max_len validator for r8   )r�   r:   r    r    r!   r;   &  s    z_MaxLengthValidator.__repr__N)r=   r>   r?   r
   r�   r7   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    
r   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 >= rv   N)r€   Ú
min_lengthr.   rD   r1   r    r    r!   r7   ;  s    z_MinLengthValidator.__call__c                 C   s   d| j › d�S )Nz<min_len validator for r8   )r…   r:   r    r    r!   r;   C  s    z_MinLengthValidator.__repr__N)r=   r>   r?   r
   r…   r7   r;   r    r    r    r!   r„   7  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   G  s    
r   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.   r0   r1   r    r    r!   r7   X  s    üz_SubclassOfValidator.__call__c                 C   s   d| j ›d�S )Nz <subclass_of validator for type r8   r9   r:   r    r    r!   r;   e  s    z_SubclassOfValidator.__repr__Nr<   r    r    r    r!   r†   T  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†   r9   r    r    r!   Ú_subclass_ofi  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)Ú	converterro   rW   c                 C   sP   z|   |||¡ W n | jy&   Y n&0 t| jj| j | jd�|| j || jƒ‚d S )N©rX   Ú	exc_types)rX   rŒ   rD   r6   rN   rY   r    r    r!   r7   Š  s    þøz_NotValidator.__call__c                 C   s   d| j ›d| j›d�S )Nz<not_ validator wrapping z, capturing r8   r‹   r:   r    r    r!   r;   ›  s    z_NotValidator.__repr__N)r=   r>   r?   r
   rX   r   r6   r   rˆ   Ú	Exceptionr   r[   rŒ   r7   r;   r    r    r    r!   r‰   z  s   ÿÿþÿr‰   )r6   rŒ   c                C   s2   zt |ƒ}W n ty$   |f}Y n0 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
    )r[   r0   r‰   )rX   r6   rŒ   r    r    r!   r   Ÿ  s
    r   c                   @   s"   e Zd Zeƒ Zdd„ Zdd„ ZdS )Ú_OrValidatorc              	   C   sX   | j D ]2}z||||ƒ W n ty0   Y qY q0  d S qd| j ›d|›�}t|ƒ‚d S )NzNone of z satisfied for value )Ú
validatorsr�   rD   )r2   r3   r4   r5   Úvr6   r    r    r!   r7   Ç  s    

z_OrValidator.__call__c                 C   s   d| j ›d�S )Nz<or validator wrapping r8   )r�   r:   r    r    r!   r;   Ó  s    z_OrValidator.__repr__N)r=   r>   r?   r
   r�   r7   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�   r[   )r�   Úvalsr�   r    r    r!   r   ×  s    r   )r   N)N)NNN)5Ú__doc__r{   rJ   Ú
contextlibr   r   Ú_configr   r   Ú_maker   r	   r
   r   Ú
convertersr   Ú
exceptionsr   Ú__all__r   r   r   r&   r   rA   r   rV   r   r\   r   rb   r   rf   r   rp   r   ru   r   r   r   r   r~   r   r„   r   r†   rˆ   r‰   rD   r0   r   rŽ   r   r    r    r    r!   Ú<module>   sp   

0#
 ÿ
2$$