o
    `,                     @   s  d Z ddlmZmZmZ ddlZddlmZmZm	Z	m
Z
 ddlmZ g dZe
dd	d	d
G dd deZdd Ze
dd	d	dG dd deZd,ddZe
dd	d	d
G dd deZdd Ze
dd	d	d
G dd deZdd Ze
dd	d	d
G dd deZdd Ze
ddd	d
G d d! d!eZd"d# Ze
dd	d	d
G d$d% d%eZd-d&d'Ze
dd	d	d
G d(d) d)eZd-d*d+ZdS ).z
Commonly useful validators.
    )absolute_importdivisionprint_functionN   )_AndValidatorand_attribattrs)NotCallableError)	r   deep_iterabledeep_mappingin_instance_ofis_callable
matches_reoptionalprovidesFT)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%   1/usr/lib/python3/dist-packages/attr/validators.py__call__   s   z_InstanceOfValidator.__call__c                 C      dj | jdS )Nz)<instance_of validator for type {type!r}>r   )r   r   r"   r%   r%   r&   __repr__0      z_InstanceOfValidator.__repr__N)__name__
__module____qualname__r   r   r'   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 types

    :raises TypeError: With a human readable error message, the attribute
        (of type `attr.Attribute`), the expected type, and the value it
        got.
    )r   r)   r%   r%   r&   r   6   s   r   )r   frozenr   c                   @   s.   e Zd Ze 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   z7'{name}' must match regex {regex!r} ({value!r} doesn't))r   regexr   N)
match_func
ValueErrorr   r   r3   patternr!   r%   r%   r&   r'   L   s   
z_MatchesReValidator.__call__c                 C   r(   )Nz,<matches_re validator for pattern {regex!r}>)r3   )r   r3   r*   r%   r%   r&   r+   [   r,   z_MatchesReValidator.__repr__N)	r-   r.   r/   r   r3   flagsr4   r'   r+   r%   r%   r%   r&   r2   F   s    r2   c                 C   s   t tdd}|dtjtjf}||vr%tddtdd t|D f t| |}|tju r4|j}n|tju r=|j}n|rC|j	}ntd
| |}|j}t|||S )a}  
    A validator that raises `ValueError` if the initializer is called
    with a string that doesn't match *regex*.

    :param str regex: a regex string 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 (options
        are `re.fullmatch`, `re.search`, `re.match`, default
        is ``None`` which means either `re.fullmatch` or an emulation of
        it on Python 2). For performance reasons, they won't be used directly
        but on a pre-`re.compile`\ ed pattern.

    .. versionadded:: 19.2.0
    	fullmatchNz'func' must be one of %s.z, c                 s   s    | ]
}|r	|j p
d V  qdS )NoneN)r-   ).0er%   r%   r&   	<genexpr>x   s    
zmatches_re.<locals>.<genexpr>z(?:{})\Z)getattrresearchmatchr5   joinsortedsetcompiler8   r   r2   )r3   r7   funcr8   valid_funcsr6   r4   r%   r%   r&   r   a   s.   

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)rH   
providedByr   r   r   r!   r%   r%   r&   r'      s   
z_ProvidesValidator.__call__c                 C   r(   )Nz0<provides validator for interface {interface!r}>rH   )r   rH   r*   r%   r%   r&   r+      r,   z_ProvidesValidator.__repr__N)r-   r.   r/   r   rH   r'   r+   r%   r%   r%   r&   rG      s    rG   c                 C   r0   )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 `attr.Attribute`), the expected interface, and the
        value it got.
    )rG   rJ   r%   r%   r&   r      s   r   c                   @   r   )_OptionalValidatorc                 C   s   |d u rd S |  ||| d S N	validatorr!   r%   r%   r&   r'      s   z_OptionalValidator.__call__c                 C   s   dj t| jdS )Nz'<optional validator for {what} or None>)what)r   r   rN   r*   r%   r%   r&   r+      s   z_OptionalValidator.__repr__N)r-   r.   r/   r   rN   r'   r+   r%   r%   r%   r&   rK      s    rK   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   listrK   r   rM   r%   r%   r&   r      s   
r   c                   @   r   )_InValidatorc                 C   sF   z|| j v }W n ty   d}Y nw |s!tdj|j| j |dd S )NFz/'{name}' must be in {options!r} (got {value!r}))r   optionsr   )rR   r   r5   r   r   )r"   r#   r$   r   
in_optionsr%   r%   r&   r'      s   
z_InValidator.__call__c                 C   r(   )Nz(<in_ validator with options {options!r}>rR   )r   rR   r*   r%   r%   r&   r+      r,   z_InValidator.__repr__N)r-   r.   r/   r   rR   r'   r+   r%   r%   r%   r&   rQ      s    rQ   c                 C   r0   )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 `attr.Attribute`), the expected options, and the value it
       got.

    .. versionadded:: 17.1.0
    )rQ   rT   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   r    )r"   r#   r$   r   messager%   r%   r&   r'     s   
z_IsCallableValidator.__call__c                 C   s   dS )Nz<is_callable validator>r%   r*   r%   r%   r&   r+     s   z_IsCallableValidator.__repr__N)r-   r.   r/   r'   r+   r%   r%   r%   r&   rU     s    rU   c                   C   s   t  S )aw  
    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 (`attr.Attribute`) name,
        and the value it got.
    )rU   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 )_DeepIterablerM   NdefaultrN   c                 C   s4   | j dur|  ||| |D ]	}| ||| qdS r   N)iterable_validatormember_validator)r"   r#   r$   r   memberr%   r%   r&   r'   /  s
   
z_DeepIterable.__call__c                 C   s,   | j d u rdndj| j d}dj|| jdS )N z {iterable!r})iterablezJ<deep_iterable validator for{iterable_identifier} iterables of {member!r}>)iterable_identifierr_   )r]   r   r^   )r"   rb   r%   r%   r&   r+   9  s   z_DeepIterable.__repr__)
r-   r.   r/   r   r   r^   r   r]   r'   r+   r%   r%   r%   r&   rY   (  s    

rY   c                 C   s
   t | |S )a1  
    A validator that performs deep validation of an iterable.

    :param member_validator: Validator 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
    )rY   )r^   r]   r%   r%   r&   r   H  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 )_DeepMappingrM   NrZ   c                 C   sF   | j dur|  ||| |D ]}| ||| | ||||  qdS r\   )mapping_validatorkey_validatorvalue_validator)r"   r#   r$   r   keyr%   r%   r&   r'   ]  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}>)rg   r   )r   re   rf   r*   r%   r%   r&   r+   h  s   
z_DeepMapping.__repr__)r-   r.   r/   r   r   re   rf   r   rd   r'   r+   r%   r%   r%   r&   rc   W  s    rc   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
    )rc   )re   rf   rd   r%   r%   r&   r   n  s   r   )r   NrL   )__doc__
__future__r   r   r   r>   _maker   r   r   r	   
exceptionsr
   __all__objectr   r   r2   r   rG   r   rK   r   rQ   r   rU   r   rY   r   rc   r   r%   r%   r%   r&   <module>   s<    
-
