o
    �õ±j¸  ã                   @  sÒ   d Z ddlmZ ddlmZmZ ddl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©Ú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__© úŒ/root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/prompt_toolkit/validation.pyr   "   s   
zValidationError.__init__c                 C  s   | j j› d| j›d| j›d�S )Nz(cursor_position=z
, message=ú))r   Ú__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    Úvalidate8   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_asyncB   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)Úclsr/   r1   r2   r   r   r    Úfrom_callableM   s   zValidator.from_callableN©r*   r	   r   r   )r.   F)r/   r0   r1   r   r2   r3   r   r   )	r"   r&   r'   r(   r   r,   r-   Úclassmethodr6   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 )r4   z0
    Validate input from a simple callable.
    Úfuncr0   r1   r   r2   r3   r   r   c                 C  s   || _ || _|| _d S r   )r:   r1   r2   )r   r:   r1   r2   r   r   r    r   k   s   
z_ValidatorFromCallable.__init__c                 C  s   d| j ›d�S )NzValidator.from_callable(r!   )r:   r#   r   r   r    r$   r   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:   Útextr2   Úlenr   r1   )r   r*   Úindexr   r   r    r,   u   s   úz_ValidatorFromCallable.validateN)r:   r0   r1   r   r2   r3   r   r   r%   r7   )r"   r&   r'   r(   r   r$   r,   r   r   r   r    r4   f   s
    

r4   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   )r?   )r   r?   r   r   r    r   †   ó   
zThreadedValidator.__init__r*   r	   c                 C  s   | j  |¡ d S r   )r?   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‘   rA   z?ThreadedValidator.validate_async.<locals>.run_validation_threadN)r   r   r   )r   r*   rC   r   rB   r    r-   Œ   s   €z ThreadedValidator.validate_asyncN)r?   r   r   r   r7   ©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.validateNr7   )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.)
    r?   r   Úfilterr
   r   r   c                 C  s   || _ t|ƒ| _d S r   )r?   r   rE   )r   r?   rE   r   r   r    r   ¦   s   zConditionalValidator.__init__r*   r	   c                 C  s   |   ¡ r| j |¡ d S d S r   )rE   r?   r,   r+   r   r   r    r,   ª   s   ÿzConditionalValidator.validateN)r?   r   rE   r
   r   r   r7   )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  r@   r   )rF   )r   rF   r   r   r    r   ·   rA   zDynamicValidator.__init__r*   r	   c                 C  s   |   ¡ ptƒ }| |¡ d S r   )rF   r   r,   ©r   r*   r?   r   r   r    r,   º   s   zDynamicValidator.validatec                 Ã  s$   �|   ¡ ptƒ }| |¡I d H  d S r   )rF   r   r-   rH   r   r   r    r-   ¾   s   €zDynamicValidator.validate_asyncN)rF   rG   r   r   r7   rD   r   r   r   r    r   °   s
    

r   N)r(   Ú
__future__r   Úabcr   r   Úcollections.abcr   Úprompt_toolkit.eventloopr   r*   r	   Úfiltersr
   r   Ú__all__Ú	Exceptionr   r   r4   r   r   r   r   r   r   r   r    Ú<module>   s    
;	