B
    *U\."                 @   sP  d Z ddlmZmZmZ ddlmZmZmZm	Z	 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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_function   )_AndValidatorand_attribattrsr   in_instance_of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 )zP
        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typeZactualvalueN)
isinstancer   	TypeErrorformatr   	__class__)selfinstattrr    r   .lib/python3.7/site-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__#   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 :exc:`TypeError` if the initializer is called
    with a wrong type for this particular attribute (checks are performed using
    :func:`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 :class:`attr.Attribute`), the expected type, and the value it
        got.
    )r   )r   r   r   r   r   )   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 )zP
        We use a callable class to be able to change the ``__repr__``.
        z<'{name}' must provide {interface!r} which {value!r} doesn't.)r   	interfacer   N)r$   Z
providedByr   r   r   )r   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}>)r$   )r   r$   )r   r   r   r   r   L   s    z_ProvidesValidator.__repr__N)r    r!   r"   r   r$   r   r   r   r   r   r   r#   9   s   r#   c             C   s   t | S )a  
    A validator that raises a :exc:`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 :class:`attr.Attribute`), the expected interface, and the
        value it got.
    )r#   )r$   r   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)	validator)r   r   r   r   r   r   r   r   f   s    z_OptionalValidator.__call__c             C   s   dj t| jdS )Nz'<optional validator for {what} or None>)Zwhat)r   r   r&   )r   r   r   r   r   l   s    z_OptionalValidator.__repr__N)r    r!   r"   r   r&   r   r   r   r   r   r   r%   b   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 :class:`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   r   s    
c               @   s"   e Zd Ze Zdd Zdd ZdS )_InValidatorc             C   sH   y|| 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   )r)   r   
ValueErrorr   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}>)r)   )r   r)   )r   r   r   r   r      s    z_InValidator.__repr__N)r    r!   r"   r   r)   r   r   r   r   r   r   r(      s   r(   c             C   s   t | S )a  
    A validator that raises a :exc:`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, :class:`enum.Enum`, ...

    :raises ValueError: With a human readable error message, the attribute (of
       type :class:`attr.Attribute`), the expected options, and the value it
       got.

    .. versionadded:: 17.1.0
    )r(   )r)   r   r   r   r
      s    c               @   s   e Zd Zdd Zdd ZdS )_IsCallableValidatorc             C   s   t |stdj|jddS )zP
        We use a callable class to be able to change the ``__repr__``.
        z'{name}' must be callable)r   N)callabler   r   r   )r   r   r   r   r   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   r+      s   r+   c               C   s   t  S )a;  
    A validator that raises a :class:`TypeError` if the initializer is called
    with a value for this particular attribute that is not callable.

    .. versionadded:: 19.1.0

    :raises TypeError: With a human readable error message containing the
        attribute (of type :class:`attr.Attribute`) name.
    )r+   r   r   r   r   is_callable   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 )_DeepIterable)r&   N)defaultr&   c             C   s8   | j dk	r|  ||| x|D ]}| ||| qW dS )zP
        We use a callable class to be able to change the ``__repr__``.
        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_identifierr2   )r0   r   r1   )r   r5   r   r   r   r      s
    z_DeepIterable.__repr__)
r    r!   r"   r   r-   r1   r   r0   r   r   r   r   r   r   r.      s
   
r.   Nc             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
    )r.   )r1   r0   r   r   r   deep_iterable   s    r6   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 )_DeepMapping)r&   N)r/   r&   c             C   sJ   | j dk	r|  ||| x,|D ]$}| ||| | ||||  qW dS )zP
        We use a callable class to be able to change the ``__repr__``.
        N)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   r9   r:   )r   r   r   r   r     s    z_DeepMapping.__repr__)r    r!   r"   r   r-   r9   r:   r   r8   r   r   r   r   r   r   r7      s
   r7   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
    )r7   )r9   r:   r8   r   r   r   deep_mapping  s    r<   )N)N)__doc__Z
__future__r   r   r   _maker   r   r   r	   __all__objectr   r   r#   r   r%   r   r(   r
   r+   r-   r.   r6   r7   r<   r   r   r   r   <module>   s0   
