o
    i                     @  s   d 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
 ddlmZ ddlmZmZ g d	ZG d
d deZG dd ded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.)
    )annotations)ABCMetaabstractmethod)CallableOptionalrun_in_executor_with_context   )Document)FilterOrBool	to_filter)ConditionalValidatorValidationError	ValidatorThreadedValidatorDummyValidatorDynamicValidatorc                      s.   e Zd ZdZdd fd
dZd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    cursor_positionintmessagestrreturnNonec                   s   t  | || _|| _d S N)super__init__r   r   )selfr   r   	__class__ T/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/prompt_toolkit/validation.pyr   !   s   
zValidationError.__init__c                 C  s   d | jj| j| jS )Nz&{}(cursor_position={!r}, message={!r}))formatr   __name__r   r   r   r    r    r!   __repr__&   s
   zValidationError.__repr__)r   r   )r   r   r   r   r   r   r   r   )r#   
__module____qualname____doc__r   r%   __classcell__r    r    r   r!   r      s    r   c                   @  s<   e Zd ZdZedddZddd	Ze	
	d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`.
    documentr
   r   r   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   r+   r    r    r!   validate;   s   zValidator.validatec                   s$   z|  | W dS  ty    w )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,   r    r    r!   validate_asyncE   s   zValidator.validate_asyncInvalid inputFvalidate_funcCallable[[str], bool]error_messager   move_cursor_to_endboolc                 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)clsr0   r2   r3   r    r    r!   from_callableP   s   zValidator.from_callableNr+   r
   r   r   )r/   F)r0   r1   r2   r   r3   r4   r   r   )	r#   r'   r(   r)   r   r-   r.   classmethodr7   r    r    r    r!   r   .   s    
	r   )	metaclassc                   @  s.   e Zd ZdZdd
dZdddZdddZdS )r5   z0
    Validate input from a simple callable.
    funcr1   r2   r   r3   r4   r   r   c                 C  s   || _ || _|| _d S r   )r;   r2   r3   )r   r;   r2   r3   r    r    r!   r   n   s   
z_ValidatorFromCallable.__init__c                 C  s   d| j dS )NzValidator.from_callable())r;   r$   r    r    r!   r%   u   s   z_ValidatorFromCallable.__repr__r+   r
   c                 C  s4   |  |js| jrt|j}nd}t|| jdd S )Nr   )r   r   )r;   textr3   lenr   r2   )r   r+   indexr    r    r!   r-   x   s   z_ValidatorFromCallable.validateN)r;   r1   r2   r   r3   r4   r   r   r&   r8   )r#   r'   r(   r)   r   r%   r-   r    r    r    r!   r5   i   s
    

r5   c                   @  .   e Zd ZdZdddZdd
dZd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.)
    	validatorr   r   r   c                 C  
   || _ d S r   )rA   )r   rA   r    r    r!   r         
zThreadedValidator.__init__r+   r
   c                 C  s   | j | d S r   )rA   r-   r,   r    r    r!   r-      s   zThreadedValidator.validatec                   s$   d fdd}t |I dH  dS )z:
        Run the `validate` function in a thread.
        r   r   c                     s
     S r   )r-   r    r+   r   r    r!   run_validation_thread   rC   z?ThreadedValidator.validate_async.<locals>.run_validation_threadN)r   r   r   )r   r+   rE   r    rD   r!   r.      s   z ThreadedValidator.validate_asyncN)rA   r   r   r   r8   r#   r'   r(   r)   r   r-   r.   r    r    r    r!   r      s
    

r   c                   @  s   e Zd ZdZd	ddZdS )
r   z1
    Validator class that accepts any input.
    r+   r
   r   r   c                 C  s   d S r   r    r,   r    r    r!   r-      s   zDummyValidator.validateNr8   )r#   r'   r(   r)   r-   r    r    r    r!   r      s    r   c                   @  s$   e Zd ZdZddd	ZdddZdS )r   zq
    Validator that can be switched on/off according to
    a filter. (This wraps around another validator.)
    rA   r   filterr   r   r   c                 C  s   || _ t|| _d S r   )rA   r   rG   )r   rA   rG   r    r    r!   r      s   zConditionalValidator.__init__r+   r
   c                 C  s   |   r| j| d S d S r   )rG   rA   r-   r,   r    r    r!   r-      s   zConditionalValidator.validateN)rA   r   rG   r   r   r   r8   )r#   r'   r(   r)   r   r-   r    r    r    r!   r      s    
r   c                   @  r@   )r   z
    Validator class that can dynamically returns any Validator.

    :param get_validator: Callable that returns a :class:`.Validator` instance.
    get_validatorCallable[[], Validator | None]r   r   c                 C  rB   r   )rH   )r   rH   r    r    r!   r      rC   zDynamicValidator.__init__r+   r
   c                 C  s   |   pt }|| d S r   )rH   r   r-   r   r+   rA   r    r    r!   r-      s   zDynamicValidator.validatec                   s$   |   pt }||I d H  d S r   )rH   r   r.   rJ   r    r    r!   r.      s   zDynamicValidator.validate_asyncN)rH   rI   r   r   r8   rF   r    r    r    r!   r      s
    

r   N)r)   
__future__r   abcr   r   typingr   r   prompt_toolkit.eventloopr   r+   r
   filtersr   r   __all__	Exceptionr   r   r5   r   r   r   r   r    r    r    r!   <module>   s    
;	