a
    õ£Be"  ã                   @  s  U d dl mZ d dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
 erjd dlmZ d dlmZ dd	lmZ g d
¢ZG dd„ dƒZedeƒ d�Zded< ddœdd„Zddœdd„Zddœdd„Zedddœdd„ƒZed%dddd œd!d"„ƒZeddœd#d$„ƒZdS )&é    )ÚannotationsN)Úcontextmanager)Ú
ContextVar)ÚTYPE_CHECKINGÚAnyÚ	Generator)ÚInput)ÚOutputé   )ÚApplication)Ú
AppSessionÚget_app_sessionÚget_appÚget_app_or_noneÚset_appÚcreate_app_sessionÚcreate_app_session_from_ttyc                   @  sV   e Zd ZdZdddddœdd„Zd	d
œdd„Zedd
œdd„ƒZedd
œdd„ƒZdS )r   aY  
    An AppSession is an interactive session, usually connected to one terminal.
    Within one such session, interaction with many applications can happen, one
    after the other.

    The input/output device is not supposed to change during one session.

    Warning: Always use the `create_app_session` function to create an
    instance, so that it gets activated correctly.

    :param input: Use this as a default input for all applications
        running in this session, unless an input is passed to the `Application`
        explicitly.
    :param output: Use this as a default output.
    NúInput | NoneúOutput | NoneÚNone©ÚinputÚoutputÚreturnc                 C  s   || _ || _d | _d S ©N)Ú_inputÚ_outputÚapp)Úselfr   r   © r   úe/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/prompt_toolkit/application/current.pyÚ__init__*   s    zAppSession.__init__Ústr©r   c                 C  s   d| j ›d�S )NzAppSession(app=ú))r   )r   r   r   r    Ú__repr__4   s    zAppSession.__repr__r   c                 C  s$   | j d u rddlm} |ƒ | _ | j S )Nr   ©Úcreate_input)r   Úprompt_toolkit.input.defaultsr'   )r   r'   r   r   r    r   7   s    
zAppSession.inputr	   c                 C  s$   | j d u rddlm} |ƒ | _ | j S )Nr   ©Úcreate_output)r   Úprompt_toolkit.output.defaultsr*   )r   r*   r   r   r    r   ?   s    
zAppSession.output)NN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r!   r%   Úpropertyr   r   r   r   r   r    r      s    ÿ
r   Ú_current_app_session)ÚdefaultzContextVar[AppSession]r#   c                   C  s   t  ¡ S r   )r1   Úgetr   r   r   r    r   M   s    r   zApplication[Any]c                  C  s*   t  ¡ } | jdur| jS ddlm} |ƒ S )a'  
    Get the current active (running) Application.
    An :class:`.Application` is active during the
    :meth:`.Application.run_async` call.

    We assume that there can only be one :class:`.Application` active at the
    same time. There is only one terminal window, with only one stdin and
    stdout. This makes the code significantly easier than passing around the
    :class:`.Application` everywhere.

    If no :class:`.Application` is running, then return by default a
    :class:`.DummyApplication`. For practical reasons, we prefer to not raise
    an exception. This way, we don't have to check all over the place whether
    an actual `Application` was returned.

    (For applications like pymux where we can have more than one `Application`,
    we'll use a work-around to handle that.)
    Nr
   )ÚDummyApplication)r1   r3   r   Údummyr4   )Úsessionr4   r   r   r    r   Q   s
    
r   zApplication[Any] | Nonec                  C  s   t  ¡ } | jS )zj
    Get the current active (running) Application, or return `None` if no
    application is running.
    ©r1   r3   r   )r6   r   r   r    r   m   s    r   zGenerator[None, None, None])r   r   c                 c  s2   t  ¡ }|j}| |_zdV  W ||_n||_0 dS )aµ  
    Context manager that sets the given :class:`.Application` active in an
    `AppSession`.

    This should only be called by the `Application` itself.
    The application will automatically be active while its running. If you want
    the application to be active in other threads/coroutines, where that's not
    the case, use `contextvars.copy_context()`, or use `Application.context` to
    run it in the appropriate context.
    Nr7   )r   r6   Zprevious_appr   r   r    r   v   s    r   r   r   z!Generator[AppSession, None, None]r   c              	   c  sn   t jdkrtdƒ‚| du r"tƒ j} |du r2tƒ j}t| |d�}t |¡}z|V  W t 	|¡ nt 	|¡ 0 dS )zï
    Create a separate AppSession.

    This is useful if there can be multiple individual `AppSession`s going on.
    Like in the case of an Telnet/SSH server. This functionality uses
    contextvars and requires at least Python 3.7.
    )é   é   z(Application sessions require Python 3.7.N©r   r   )
ÚsysÚversion_infoÚRuntimeErrorr   r   r   r   r1   ÚsetÚreset)r   r   r6   Útokenr   r   r    r   Œ   s    

r   c                  c  sb   ddl m}  ddlm} | dd�}|dd�}t||d��}|V  W d  ƒ n1 sT0    Y  dS )a‘  
    Create `AppSession` that always prefers the TTY input/output.

    Even if `sys.stdin` and `sys.stdout` are connected to input/output pipes,
    this will still use the terminal for interaction (because `sys.stderr` is
    still connected to the terminal).

    Usage::

        from prompt_toolkit.shortcuts import prompt

        with create_app_session_from_tty():
            prompt('>')
    r   r&   r)   T)Zalways_prefer_ttyr:   N)r(   r'   r+   r*   r   )r'   r*   r   r   Zapp_sessionr   r   r    r   «   s    

r   )NN)Ú
__future__r   r;   Ú
contextlibr   Úcontextvarsr   Útypingr   r   r   Zprompt_toolkit.input.baser   Zprompt_toolkit.output.baser	   Zapplicationr   Ú__all__r   r1   Ú__annotations__r   r   r   r   r   r   r   r   r   r    Ú<module>   s.   /ÿ	 ÿ