a
    õ£Beé  ã                   @  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dddœ‡ fdd	„Zd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   Ú ÚintÚstrÚNone)Úcursor_positionÚmessageÚreturnc                   s   t ƒ  |¡ || _|| _d S ©N)ÚsuperÚ__init__r   r   )Úselfr   r   ©Ú	__class__© ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/prompt_toolkit/validation.pyr   !   s    zValidationError.__init__©r   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#   Ú
__module__Ú__qualname__Ú__doc__r   r%   Ú__classcell__r   r   r   r    r      s   r   c                   @  sN   e Zd ZdZedddœdd„ƒZdddœdd„Zedddd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`.
    r	   r   ©Údocumentr   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 n ty"   ‚ Y n0 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,   r   r   r    Úvalidate_asyncE   s    zValidator.validate_asyncúInvalid inputFúCallable[[str], bool]r   Úbool)Úvalidate_funcÚerror_messageÚmove_cursor_to_endr   c                 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)Úclsr2   r3   r4   r   r   r    Úfrom_callableP   s    zValidator.from_callableN)r/   F)	r#   r&   r'   r(   r   r-   r.   Úclassmethodr7   r   r   r   r    r   .   s   	  ür   )Ú	metaclassc                   @  sB   e Zd ZdZdddddœdd„Zdd	œd
d„Zdddœdd„ZdS )r5   z0
    Validate input from a simple callable.
    r0   r   r1   r   )Úfuncr3   r4   r   c                 C  s   || _ || _|| _d S r   )r:   r3   r4   )r   r:   r3   r4   r   r   r    r   n   s    z_ValidatorFromCallable.__init__r!   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¡s0| jrt|jƒ}nd}t|| jd�‚d S )Nr   )r   r   )r:   Útextr4   Úlenr   r3   )r   r+   Úindexr   r   r    r-   x   s
    z_ValidatorFromCallable.validateN)r#   r&   r'   r(   r   r%   r-   r   r   r   r    r5   i   s   r5   c                   @  s@   e Zd ZdZdddœdd„Zdddœd	d
„Zdd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.)
    r   r   )Ú	validatorr   c                 C  s
   || _ d S r   )r?   )r   r?   r   r   r    r   ‰   s    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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”   s    z?ThreadedValidator.validate_async.<locals>.run_validation_threadNr   )r   r+   rA   r   r@   r    r.   �   s    z ThreadedValidator.validate_asyncN©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œdd„ZdS )r   z1
    Validator class that accepts any input.
    r	   r   r*   c                 C  s   d S r   r   r,   r   r   r    r-   Ÿ   s    zDummyValidator.validateN)r#   r&   r'   r(   r-   r   r   r   r    r   š   s   r   c                   @  s2   e Zd ZdZddddœdd„Zdd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
   r   )r?   Úfilterr   c                 C  s   || _ t|ƒ| _d S r   )r?   r   rC   )r   r?   rC   r   r   r    r   ©   s    zConditionalValidator.__init__r	   r*   c                 C  s   |   ¡ r| j |¡ d S r   )rC   r?   r-   r,   r   r   r    r-   ­   s    zConditionalValidator.validateN)r#   r&   r'   r(   r   r-   r   r   r   r    r   £   s   r   c                   @  s@   e Zd ZdZdddœdd„Zdddœd	d
„Zdddœ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.
    zCallable[[], Validator | None]r   )Úget_validatorr   c                 C  s
   || _ d S r   )rD   )r   rD   r   r   r    r   º   s    zDynamicValidator.__init__r	   r*   c                 C  s   |   ¡ ptƒ }| |¡ d S r   )rD   r   r-   ©r   r+   r?   r   r   r    r-   ½   s    zDynamicValidator.validatec                 Ã  s"   |   ¡ ptƒ }| |¡I d H  d S r   )rD   r   r.   rE   r   r   r    r.   Á   s    zDynamicValidator.validate_asyncNrB   r   r   r   r    r   ³   s   r   N)r(   Ú
__future__r   Úabcr   r   Útypingr   Zprompt_toolkit.eventloopr   r+   r	   Úfiltersr
   r   Ú__all__Ú	Exceptionr   r   r5   r   r   r   r   r   r   r   r    Ú<module>   s   
;	