o
    Df                     @  s   d Z ddlmZ ddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZ ddlZddlmZmZ ddlmZ ddlmZmZ dd	lmZ d
dgZG dd
 d
ejZG dd dejZdS )zI
Utility for running a prompt_toolkit application in an asyncssh server.
    )annotationsN)get_running_loop)AnyCallable	CoroutineTextIOcast)
AppSessioncreate_app_session)Size)	PipeInputcreate_pipe_input)Vt100_OutputPromptToolkitSSHSessionPromptToolkitSSHServerc                   @  s\   e Zd Zd$ddZd%d
dZd&ddZd'ddZd(ddZd(ddZd)ddZ	d*d!d"Z
d#S )+r   interact>Callable[[PromptToolkitSSHSession], Coroutine[Any, Any, None]]
enable_cprboolreturnNonec                  sN   | _ | _d  _d  _d  _d  _d  _G  fddd}tt|  _	d S )Nc                      s@   e Zd Zd fddZddd	Zdd
dZed fddZdS )z0PromptToolkitSSHSession.__init__.<locals>.Stdoutdatastrr   r   c                   s@   z j d ur j |dd W d S W d S  ty   Y d S w )N
z
)_chanwritereplaceBrokenPipeError)sr   self Z/home/ubuntu/webapp/venv/lib/python3.10/site-packages/prompt_toolkit/contrib/ssh/server.pyr   ,   s   
z6PromptToolkitSSHSession.__init__.<locals>.Stdout.writer   c                 S     dS NTr!   r   r!   r!   r"   isatty3      z7PromptToolkitSSHSession.__init__.<locals>.Stdout.isattyc                 S  s   d S Nr!   r%   r!   r!   r"   flush6   r'   z6PromptToolkitSSHSession.__init__.<locals>.Stdout.flushc                   s"    j d usJ t j j d S )Nr   )r   r   
_orig_changet_encodingr%   r   r!   r"   encoding9   s   z9PromptToolkitSSHSession.__init__.<locals>.Stdout.encodingN)r   r   r   r   r   r   r   r   )r   r   )__name__
__module____qualname__r   r&   r)   propertyr,   r!   r   r!   r"   Stdout+   s    

r3   )
r   r   interact_taskr   app_session_input_outputr   r   stdout)r    r   r   r3   r!   r   r"   __init__   s   z PromptToolkitSSHSession.__init__r   c                 C  s4   | j du rtdddS | j  \}}}}t||dS )zU
        Callable that returns the current `Size`, required by Vt100_Output.
        N   O   )rowscolumns)r   r   get_terminal_sizer    widthheightpixwidth	pixheightr!   r!   r"   	_get_size@   s   
z!PromptToolkitSSHSession._get_sizechanr   c                 C  s
   || _ d S r(   )r   )r    rE   r!   r!   r"   connection_madeJ   s   
z'PromptToolkitSSHSession.connection_madec                 C  r#   r$   r!   r   r!   r!   r"   shell_requestedM   r'   z'PromptToolkitSSHSession.shell_requestedc                 C  s   t  |  | _d S r(   )r   create_task	_interactr4   r   r!   r!   r"   session_startedP   s   z'PromptToolkitSSHSession.session_startedc              
     s.  | j d u r
tdt| j dr| j jd ur| j d | j  }t| j| j|| j	d| _
t ]| _t| j| j
d;}|| _z$z
| | I d H  W n tyV   t  Y nw W | j   | j  n| j   | j  w W d    n1 sxw   Y  W d    d S W d    d S 1 sw   Y  d S )Nz,`_interact` called before `connection_made`.set_line_modeF)termr   )inputoutput)r   	Exceptionhasattr_editorrK   get_terminal_typer   r8   rD   r   r7   r   r6   r
   r5   r   BaseException	traceback	print_excclose)r    rL   sessionr!   r!   r"   rI   S   s4   




"z!PromptToolkitSSHSession._interactr@   intrA   rB   objectrC   c                 C  s&   | j r| j jr| j j  d S d S d S r(   )r5   app
_on_resizer?   r!   r!   r"   terminal_size_changedo   s   z-PromptToolkitSSHSession.terminal_size_changedr   r   datatypec                 C  s   | j d u rd S | j | d S r(   )r6   	send_text)r    r   r]   r!   r!   r"   data_receivedv   s   
z%PromptToolkitSSHSession.data_receivedNr   r   r   r   r   r   )r   r   )rE   r   r   r   r-   r.   )
r@   rX   rA   rX   rB   rY   rC   rY   r   r   )r   r   r]   rY   r   r   )r/   r0   r1   r9   rD   rF   rG   rJ   rI   r\   r_   r!   r!   r!   r"   r      s    

)





c                   @  s4   e Zd ZdZdddd
dZdddZdddZdS )r   aZ  
    Run a prompt_toolkit application over an asyncssh server.

    This takes one argument, an `interact` function, which is called for each
    connection. This should be an asynchronous function that runs the
    prompt_toolkit applications. This function runs in an `AppSession`, which
    means that we can have multiple UI interactions concurrently.

    Example usage:

    .. code:: python

        async def interact(ssh_session: PromptToolkitSSHSession) -> None:
            await yes_no_dialog("my title", "my text").run_async()

            prompt_session = PromptSession()
            text = await prompt_session.prompt_async("Type something: ")
            print_formatted_text('You said: ', text)

        server = PromptToolkitSSHServer(interact=interact)
        loop = get_running_loop()
        loop.run_until_complete(
            asyncssh.create_server(
                lambda: MySSHServer(interact),
                "",
                port,
                server_host_keys=["/etc/ssh/..."],
            )
        )
        loop.run_forever()

    :param enable_cpr: When `True`, the default, try to detect whether the SSH
        client runs in a terminal that responds to "cursor position requests".
        That way, we can properly determine how much space there is available
        for the UI (especially for drop down menus) to render.
    Tr   r   r   r   r   r   r   c                C  s   || _ || _d S r(   )r   r   )r    r   r   r!   r!   r"   r9      s   
zPromptToolkitSSHServer.__init__usernamer   c                 C  r#   )NFr!   )r    rb   r!   r!   r"   
begin_auth   s   z!PromptToolkitSSHServer.begin_authr   c                 C  s   t | j| jdS )Nra   )r   r   r   r   r!   r!   r"   session_requested   s   z(PromptToolkitSSHServer.session_requestedNr`   )rb   r   r   r   )r   r   )r/   r0   r1   __doc__r9   rc   rd   r!   r!   r!   r"   r   ~   s    )
	)re   
__future__r   asynciorT   r   typingr   r   r   r   r   asyncssh"prompt_toolkit.application.currentr	   r
   prompt_toolkit.data_structuresr   prompt_toolkit.inputr   r   prompt_toolkit.output.vt100r   __all__SSHServerSessionr   	SSHServerr   r!   r!   r!   r"   <module>   s    h