U
    ]¥]Ä,  ã                	   @   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 ddd	d
dddddg	Ze
dddd�G dd„ deƒƒZdd„ Ze
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)ÚNotCallableErrorr   Údeep_iterableÚdeep_mappingÚin_Úinstance_ofÚis_callableÚ
matches_reÚoptionalÚprovidesFT)Ú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$   ú1/usr/lib/python3/dist-packages/attr/validators.pyÚ__call__   s    ûöz_InstanceOfValidator.__call__c                 C   s   dj | jd�S )Nz)<instance_of validator for type {type!r}>©r   )r   r   ©r!   r$   r$   r%   Ú__repr__0   s    ÿz_InstanceOfValidator.__repr__N)Ú__name__Ú
__module__Ú__qualname__r   r   r&   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).

    :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   Úfrozenc                   @   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   r/   Úpatternr    r$   r$   r%   r&   L   s    
  þùz_MatchesReValidator.__call__c                 C   s   dj | jd�S )Nz,<matches_re validator for pattern {regex!r}>)r/   )r   r/   r(   r$   r$   r%   r)   [   s    ÿz_MatchesReValidator.__repr__N)	r*   r+   r,   r   r/   Úflagsr0   r&   r)   r$   r$   r$   r%   r.   F   s
   r.   c                 C   sª   t tddƒ}|dtjtjf}||krJtdd tdd„ t|ƒD ƒƒ¡f ƒ‚t | |¡}|tjkrh|j}n6|tjkrz|j}n$|r†|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Úmatchr1   ÚjoinÚsortedÚsetÚcompiler4   r   r.   )r/   r3   Úfuncr4   Zvalid_funcsr2   r0   r$   r$   r%   r   a   s.    ÿÿÿÿÿ

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)rC   Z
providedByr   r   r   r    r$   r$   r%   r&   ’   s      þùz_ProvidesValidator.__call__c                 C   s   dj | jd�S )Nz0<provides validator for interface {interface!r}>©rC   )r   rC   r(   r$   r$   r%   r)   ¡   s    ÿz_ProvidesValidator.__repr__N)r*   r+   r,   r   rC   r&   r)   r$   r$   r$   r%   rB   Ž   s   rB   c                 C   s   t | ƒS )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 zope.interface.Interface interface: The interface to check for.

    :raises TypeError: With a human readable error message, the attribute
        (of type `attr.Attribute`), the expected interface, and the
        value it got.
    )rB   rD   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©Ú	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>)Zwhat)r   r   rG   r(   r$   r$   r%   r)   Á   s    ÿz_OptionalValidator.__repr__N)r*   r+   r,   r   rG   r&   r)   r$   r$   r$   r%   rE   ·   s   rE   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   ÚlistrE   r   rF   r$   r$   r%   r   Ç   s    
c                   @   s"   e Zd Zeƒ Zdd„ Zdd„ ZdS )Ú_InValidatorc                 C   sH   z|| j k}W n tk
r&   d}Y nX |sDtdj|j| j |d�ƒ‚d S )NFz/'{name}' must be in {options!r} (got {value!r}))r   Úoptionsr   )rJ   r   r1   r   r   )r!   r"   r#   r   Z
in_optionsr$   r$   r%   r&   Ý   s    
  ÿÿz_InValidator.__call__c                 C   s   dj | jd�S )Nz(<in_ validator with options {options!r}>©rJ   )r   rJ   r(   r$   r$   r%   r)   ê   s    ÿz_InValidator.__repr__N)r*   r+   r,   r   rJ   r&   r)   r$   r$   r$   r%   rI   Ù   s   rI   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 `attr.Attribute`), the expected options, and the value it
       got.

    .. versionadded:: 17.1.0
    )rI   rK   r$   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   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%   rL     s   rL   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.
    )rL   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 )Ú_DeepIterablerF   N©ÚdefaultrG   c                 C   s4   | j dk	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 k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_identifierrV   )rT   r   rU   )r!   rY   r$   r$   r%   r)   8  s    ÿýÿûz_DeepIterable.__repr__)
r*   r+   r,   r   r   rU   r   rT   r&   r)   r$   r$   r$   r%   rP   '  s    ÿ
rP   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
    )rP   )rU   rT   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 )Ú_DeepMappingrF   NrQ   c                 C   sF   | j dk	r|   |||¡ |D ]$}|  |||¡ |  |||| ¡ qdS rS   )Ú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}>)r^   r   )r   r\   r]   r(   r$   r$   r%   r)   g  s
    ÿ þz_DeepMapping.__repr__)r*   r+   r,   r   r   r\   r]   r   r[   r&   r)   r$   r$   r$   r%   rZ   V  s
   rZ   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
    )rZ   )r\   r]   r[   r$   r$   r%   r   m  s    )r   N)N)N)Ú__doc__Z
__future__r   r   r   r:   Ú_maker   r   r   r	   Ú
exceptionsr
   Ú__all__Úobjectr   r   r.   r   rB   r   rE   r   rI   r   rL   r   rP   r   rZ   r   r$   r$   r$   r%   Ú<module>   sL   ÷

-
