B
    't\                 @   s   d 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
 ddlmZmZ dd	d
dddgZG dd	 d	eZG dd
 d
ee	eZG dd deZG dd deZG dd deZG dd deZG dd deZdS )zV
Input validation for a `Buffer`.
(Validators will be called before accepting input.)
    )unicode_literals   )	to_filter)Futurerun_in_executor)ABCMetaabstractmethod)with_metaclass	text_typeConditionalValidatorValidationError	ValidatorThreadedValidatorDummyValidatorDynamicValidatorc                   s*   e Zd ZdZd fdd	Zdd Z  ZS )	r   z
    Error raised by :meth:`.Validator.validate`.

    :param cursor_position: The cursor position where the error occurred.
    :param message: Text.
    r    c                s    t t| | || _|| _d S )N)superr   __init__cursor_positionmessage)selfr   r   )	__class__ 8lib/python3.7/site-packages/prompt_toolkit/validation.pyr      s    zValidationError.__init__c             C   s   d| j j| j| jf S )Nz"%s(cursor_position=%r, message=%r))r   __name__r   r   )r   r   r   r   __repr__"   s    zValidationError.__repr__)r   r   )r   
__module____qualname____doc__r   r   __classcell__r   r   )r   r   r      s   c               @   s2   e Zd ZdZedd Zdd Zeddd	Zd
S )r   a  
    Abstract base class for an input validator.

    A validator is typically created in one of the following two ways:

    - Either by overriding this class and implementing the `validate` method.
    - Or by passing a callable to `Validator.from_callable`.

    If the validation takes some time and needs to happen in a background
    thread, this can be wrapped in a :class:`.ThreadedValidator`.
    c             C   s   dS )z
        Validate the input.
        If invalid, this should raise a :class:`.ValidationError`.

        :param document: :class:`~prompt_toolkit.document.Document` instance.
        Nr   )r   documentr   r   r   validate3   s    zValidator.validatec          
   C   sF   y|  | W n( tk
r6 } z
t|S d}~X Y nX tdS dS )z
        Return a `Future` which is set when the validation is ready.
        This function can be overloaded in order to provide an asynchronous
        implementation.
        N)r!   r   r   ZfailZsucceed)r   r    er   r   r   get_validate_future=   s
    zValidator.get_validate_futureInvalid inputFc             C   s   t |||S )aF  
        Create a validator from a simple validate callable. E.g.:

        .. code:: python

            def is_valid(text):
                return text in ['hello', 'world']
            Validator.from_callable(is_valid, error_message='Invalid input')

        :param validate_func: Callable that takes the input string, and returns
            `True` if the input is valid input.
        :param error_message: Message to be displayed if the input is invalid.
        :param move_cursor_to_end: Move the cursor to the end of the input, if
            the input is invalid.
        )_ValidatorFromCallable)clsZvalidate_funcerror_messagemove_cursor_to_endr   r   r   from_callableJ   s    zValidator.from_callableN)r$   F)	r   r   r   r   r   r!   r#   classmethodr)   r   r   r   r   r   '   s   
 c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r%   z0
    Validate input from a simple callable.
    c             C   s0   t |stt|tst|| _|| _|| _d S )N)callableAssertionError
isinstancer
   funcr'   r(   )r   r.   r'   r(   r   r   r   r   d   s
    z_ValidatorFromCallable.__init__c             C   s   d| j f S )NzValidator.from_callable(%r))r.   )r   r   r   r   r   l   s    z_ValidatorFromCallable.__repr__c             C   s4   |  |js0| jrt|j}nd}t|| jdd S )Nr   )r   r   )r.   textr(   lenr   r'   )r   r    indexr   r   r   r!   o   s    z_ValidatorFromCallable.validateN)r   r   r   r   r   r   r!   r   r   r   r   r%   `   s   r%   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   z
    Wrapper that runs input validation in a thread.
    (Use this to prevent the user interface from becoming unresponsive if the
    input validation takes too much time.)
    c             C   s   t |tst|| _d S )N)r-   r   r,   	validator)r   r2   r   r   r   r      s    zThreadedValidator.__init__c             C   s   | j |S )N)r2   r!   )r   r    r   r   r   r!      s    zThreadedValidator.validatec                s    fdd}t |}|S )z:
        Run the `validate` function in a thread.
        c                  s
     S )N)r!   r   )r    r   r   r   run_validation_thread   s    zDThreadedValidator.get_validate_future.<locals>.run_validation_thread)r   )r   r    r3   fr   )r    r   r   r#      s    z%ThreadedValidator.get_validate_futureN)r   r   r   r   r   r!   r#   r   r   r   r   r   z   s   c               @   s   e Zd ZdZdd ZdS )r   z1
    Validator class that accepts any input.
    c             C   s   d S )Nr   )r   r    r   r   r   r!      s    zDummyValidator.validateN)r   r   r   r   r!   r   r   r   r   r      s   c               @   s    e Zd ZdZdd Zdd ZdS )r   zq
    Validator that can be switched on/off according to
    a filter. (This wraps around another validator.)
    c             C   s"   t |tst|| _t|| _d S )N)r-   r   r,   r2   r   filter)r   r2   r5   r   r   r   r      s    zConditionalValidator.__init__c             C   s   |   r| j| d S )N)r5   r2   r!   )r   r    r   r   r   r!      s    zConditionalValidator.validateN)r   r   r   r   r   r!   r   r   r   r   r      s   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	r   z
    Validator class that can dynamically returns any Validator.

    :param get_validator: Callable that returns a :class:`.Validator` instance.
    c             C   s   t |st|| _d S )N)r+   r,   get_validator)r   r6   r   r   r   r      s    zDynamicValidator.__init__c             C   s&   |   pt }t|tst||S )N)r6   r   r-   r   r,   r!   )r   r    r2   r   r   r   r!      s    zDynamicValidator.validatec             C   s&   |   pt }t|tst||S )N)r6   r   r-   r   r,   r#   )r   r    r2   r   r   r   r#      s    z$DynamicValidator.get_validate_futureN)r   r   r   r   r   r!   r#   r   r   r   r   r      s   N)r   Z
__future__r   filtersr   Z	eventloopr   r   abcr   r   Zsixr	   r
   __all__	Exceptionr   objectr   r%   r   r   r   r   r   r   r   r   <module>   s$   9