a
    �Ï?eÞP  ã                   @   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 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$dRd3d4„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ƒƒZ-dEdF„ Z.edddd�G dGdH„ dHƒƒZ/dIdJ„ Z0edddd�G dKdL„ dLƒƒZ1de2e3fdMœdNdO„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Ú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    úR/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/attr/validators.pyr   +   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   =   s    	r   c                   c   s*   t dƒ zdV  W t dƒ n
t dƒ 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
    FNTr   r    r    r    r!   r   I   s    r   FT)ÚreprÚslotsÚhashc                   @   s"   e Zd Zeƒ Zdd„ Zdd„ ZdS )Ú_InstanceOfValidatorc                 C   s4   t || jƒs0t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___   s    ûöz_InstanceOfValidator.__call__c                 C   s   dj | jd�S )Nz)<instance_of validator for type {type!r}>©r(   ©r-   r(   ©r0   r    r    r!   Ú__repr__q   s    ÿz_InstanceOfValidator.__repr__N©Ú__name__Ú
__module__Ú__qualname__r	   r(   r3   r7   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).

    :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%   r4   r    r    r!   r   w   s    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'   r>   r/   r    r    r!   r3   Œ   s    
þùz_MatchesReValidator.__call__c                 C   s   dj | jd�S )Nz.<matches_re validator for pattern {pattern!r}>)r>   )r-   r>   r6   r    r    r!   r7   ›   s    ÿz_MatchesReValidator.__repr__N)r9   r:   r;   r	   r>   r?   r3   r7   r    r    r    r!   r=   ‡   s   r=   c              	   C   sœ   t jdt jt jf}||vr@td d tdd„ t|ƒD ƒƒ¡¡ƒ‚t	| t
ƒr\|rVtdƒ‚| }nt  | |¡}|t ju rz|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                 s   s   | ]}|r|j pd V  qdS )ÚNoneN)r9   )Ú.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Úmatchr@   r-   ÚjoinÚsortedÚsetr+   r   r,   Úcompiler=   )ÚregexÚflagsÚfuncZvalid_funcsr>   r?   r    r    r!   r   ¡   s0    ÿÿÿÿ

ÿ

r   c                   @   s"   e Zd Zeƒ Zdd„ Zdd„ ZdS )Ú_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)rQ   Z
providedByr,   r-   r'   r/   r    r    r!   r3   Õ   s    
þùz_ProvidesValidator.__call__c                 C   s   dj | jd�S )Nz0<provides validator for interface {interface!r}>)rQ   )r-   rQ   r6   r    r    r!   r7   ä   s    ÿz_ProvidesValidator.__repr__N)r9   r:   r;   r	   rQ   r3   r7   r    r    r    r!   rP   Ñ   s   rP   c                 C   s    ddl }|jdtdd� t| ƒS )aE  
    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.

    .. deprecated:: 23.1.0
    r   NzZattrs's zope-interface support is deprecated and will be removed in, or after, April 2024.é   )Ú
stacklevel)ÚwarningsÚwarnÚDeprecationWarningrP   )rQ   rT   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©Ú	validatorr/   r    r    r!   r3   	  s    z_OptionalValidator.__call__c                 C   s   dj t| jƒd�S )Nz'<optional validator for {what} or None>)Úwhat)r-   r"   rY   r6   r    r    r!   r7     s    ÿz_OptionalValidator.__repr__N)r9   r:   r;   r	   rY   r3   r7   r    r    r    r!   rW     s   rW   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.

    :param Callable | tuple[Callable] | list[Callable] validator: 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ÚtuplerW   r   rX   r    r    r!   r     s    r   c                   @   s"   e Zd Zeƒ Zdd„ Zdd„ ZdS )Ú_InValidatorc                 C   sN   z|| j v }W n ty$   d}Y n0 |sJtdj|j| j |d�|| j |ƒ‚d S )NFz/'{name}' must be in {options!r} (got {value!r}))r'   Úoptionsr*   )r^   r,   r@   r-   r'   )r0   r1   r2   r*   Z
in_optionsr    r    r!   r3   ,  s    

ÿúz_InValidator.__call__c                 C   s   dj | jd�S )Nz(<in_ validator with options {options!r}>©r^   )r-   r^   r6   r    r    r!   r7   <  s    ÿz_InValidator.__repr__N)r9   r:   r;   r	   r^   r3   r7   r    r    r    r!   r]   (  s   r]   c                 C   s   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.
    )r]   r_   r    r    r!   r   B  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'   r.   )r0   r1   r2   r*   Úmessager    r    r!   r3   Z  s    ÿ
ÿüz_IsCallableValidator.__call__c                 C   s   dS )Nz<is_callable validator>r    r6   r    r    r!   r7   j  s    z_IsCallableValidator.__repr__N)r9   r:   r;   r3   r7   r    r    r    r!   r`   X  s   r`   c                   C   s   t ƒ S )ax  
    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   n  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 )Ú_DeepIterablerX   N©ÚdefaultrY   c                 C   s4   | j dur|   |||¡ |D ]}|  |||¡ qdS ©r&   N)Úiterable_validatorÚmember_validator)r0   r1   r2   r*   Úmemberr    r    r!   r3   „  s    
z_DeepIterable.__call__c                 C   s*   | j d u rdn
d| j ›�}dj|| jd�S )NÚ ú zJ<deep_iterable validator for{iterable_identifier} iterables of {member!r}>)Úiterable_identifierrj   )rh   r-   ri   )r0   rm   r    r    r!   r7   Ž  s    ÿ
ýÿûz_DeepIterable.__repr__)
r9   r:   r;   r	   r   ri   r   rh   r3   r7   r    r    r    r!   rd   }  s   
ÿ
rd   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+   r[   r\   r   rd   ©ri   rh   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 )Ú_DeepMappingrX   Nre   c                 C   sF   | j dur|   |||¡ |D ]$}|  |||¡ |  |||| ¡ qdS rg   )Úmapping_validatorÚkey_validatorÚvalue_validator)r0   r1   r2   r*   Úkeyr    r    r!   r3   ´  s
    
z_DeepMapping.__call__c                 C   s   dj | j| jd�S )NzA<deep_mapping validator for objects mapping {key!r} to {value!r}>)rs   r*   )r-   rq   rr   r6   r    r    r!   r7   ¿  s    ÿþz_DeepMapping.__repr__)r9   r:   r;   r	   r   rq   rr   r   rp   r3   r7   r    r    r    r!   ro   ®  s
   ro   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
    )ro   )rq   rr   rp   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_funcrv   r@   r-   r'   Ú
compare_opr/   r    r    r!   r3   Û  s    üÿz_NumberValidator.__call__c                 C   s   dj | j| jd�S )Nz<Validator for x {op} {bound}>)ru   rv   )r-   rx   rv   r6   r    r    r!   r7   é  s    ÿz_NumberValidator.__repr__N)	r9   r:   r;   r	   rv   rx   rw   r3   r7   r    r    r    r!   rt   Õ  s
   rt   c                 C   s   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
    ú<)rt   Úoperatorr   ©Úvalr    r    r!   r   ï  s    	r   c                 C   s   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<=)rt   rz   r   r{   r    r    r!   r   û  s    	r   c                 C   s   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>=)rt   rz   r   r{   r    r    r!   r     s    	r   c                 C   s   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
    ú>)rt   rz   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   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_lengthr@   r-   r'   r/   r    r    r!   r3   #  s    ÿÿz_MaxLengthValidator.__call__c                 C   s   d| j › d�S )Nz<max_len validator for r}   )r�   r6   r    r    r!   r7   .  s    z_MaxLengthValidator.__repr__N)r9   r:   r;   r	   r�   r3   r7   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*.

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

    .. versionadded:: 21.3.0
    )r~   ©Úlengthr    r    r!   r   2  s    	r   c                   @   s"   e Zd Zeƒ Zdd„ Zdd„ ZdS )Ú_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_lengthr@   r-   r'   r/   r    r    r!   r3   B  s    ÿÿz_MinLengthValidator.__call__c                 C   s   d| j › d�S )Nz<min_len validator for r}   )r†   r6   r    r    r!   r7   M  s    z_MinLengthValidator.__repr__N)r9   r:   r;   r	   r†   r3   r7   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*.

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

    .. versionadded:: 22.1.0
    )r„   r‚   r    r    r!   r   Q  s    	r   c                   @   s"   e Zd Zeƒ Zdd„ Zdd„ ZdS )Ú_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'   r/   r    r    r!   r3   a  s    ü÷z_SubclassOfValidator.__call__c                 C   s   dj | jd�S )Nz)<subclass_of validator for type {type!r}>r4   r5   r6   r    r    r!   r7   r  s    ÿz_SubclassOfValidator.__repr__Nr8   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).

    :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‡   r4   r    r    r!   Ú_subclass_ofx  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   rX   c                 C   sP   z|   |||¡ W n | jy&   Y n&0 t| jj| j | jd�|| j || jƒ‚d S )N)rY   Ú	exc_types)rY   rŒ   r@   ra   r-   r/   r    r    r!   r3   ˜  s    þøz_NotValidator.__call__c                 C   s   dj | j| jd�S )Nz;<not_ validator wrapping {what!r}, capturing {exc_types!r}>)rZ   rŒ   )r-   rY   rŒ   r6   r    r    r!   r7   ©  s
    ÿüz_NotValidator.__repr__N)r9   r:   r;   r	   rY   r   ra   r   r‰   Ú	Exceptionr   r\   rŒ   r3   r7   r    r    r    r!   rŠ   ˆ  s   ÿÿþÿrŠ   )ra   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_(...))``.

    :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
    )r\   r,   rŠ   )rY   ra   rŒ   r    r    r!   r   ²  s
    r   )r   N)N)N)5Ú__doc__rz   rE   Ú
contextlibr   r   Ú_configr   r   Ú_maker   r   r	   r
   Ú
convertersr   Ú
exceptionsr   Ú__all__r   r   r   r%   r   r=   r   rP   r   rW   r   r]   r   r`   r   rd   r   ro   r   rt   r   r   r   r   r~   r   r„   r   r‡   r‰   rŠ   r@   r,   r   r    r    r    r!   Ú<module>   sn   

0

)