o
    Df=                     @  s   U d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	 er1d 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Zd'ddZd(ddZed)ddZe	d*d+d"d#Zed,d$d%ZdS )-    )annotations)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                   @  sD   e Zd ZdZ	ddd	d
ZdddZedddZe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Input | NoneoutputOutput | NonereturnNonec                 C  s   || _ || _d | _d S N)_input_outputapp)selfr   r    r   [/home/ubuntu/webapp/venv/lib/python3.10/site-packages/prompt_toolkit/application/current.py__init__)   s   
zAppSession.__init__strc                 C  s   d| j dS )NzAppSession(app=))r   )r   r   r   r   __repr__3   s   zAppSession.__repr__r   c                 C  $   | j d u rddlm} | | _ | j S )Nr   create_input)r   prompt_toolkit.input.defaultsr&   )r   r&   r   r   r   r   6      
zAppSession.inputr	   c                 C  r$   )Nr   create_output)r   prompt_toolkit.output.defaultsr*   )r   r*   r   r   r   r   >   r(   zAppSession.outputNN)r   r   r   r   r   r   )r   r!   )r   r   )r   r	   )	__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   )r2   getr   r   r   r   r   L   s   r   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)r2   r4   r   dummyr6   )sessionr6   r   r   r   r   P   s
   
r   Application[Any] | Nonec                  C  s   t  } | jS )zj
    Get the current active (running) Application, or return `None` if no
    application is running.
    r2   r4   r   )r8   r   r   r   r   l   s   r   r   Generator[None, None, None]c                 c  s2    t  }|j}| |_z	dV  W ||_dS ||_w )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.
    Nr:   )r   r8   previous_appr   r   r   r   u   s   r   Nr   r   r   r   !Generator[AppSession, None, None]c              	   c  s\    | du r	t  j} |du rt  j}t| |d}t|}z|V  W t| dS t| w )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.
    Nr   r   )r   r   r   r   r2   setreset)r   r   r8   tokenr   r   r   r      s   
r   c                  c  sf    ddl m}  ddlm} | dd}|dd}t||d}|V  W d   dS 1 s,w   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)always_prefer_ttyr>   N)r'   r&   r+   r*   r   )r&   r*   r   r   app_sessionr   r   r   r      s   

"r   )r   r   )r   r5   )r   r9   )r   r5   r   r;   r,   )r   r   r   r   r   r=   )r   r=   )
__future__r   
contextlibr   contextvarsr   typingr   r   r   prompt_toolkit.input.baser   prompt_toolkit.output.baser	   applicationr   __all__r   r2   __annotations__r   r   r   r   r   r   r   r   r   r   <module>   s.    /


	 