
    ړi                        d Z ddlmZ ddlZddlmZmZmZmZ ddl	Z	ddl
mZ ddlmZ ddlmZ ddlmZmZ dd	lmZ d
gZ G d d
e	j,                  e         Zy)a&  
Tool for embedding a REPL inside a Python 3 asyncio process.
See ./examples/asyncio-ssh-python-embed.py for a demo.

Note that the code in this file is Python 3 only. However, we
should make sure not to use Python 3-only syntax, because this
package should be installable in Python 2 as well!
    )annotationsN)AnyAnyStrTextIOcast)Size)create_pipe_input)Vt100_Output)_GetNamespace
_Namespace)
PythonReplReplSSHServerSessionc                      e Zd ZdZ	 d	 	 	 	 	 ddZddZddZddZ	 	 	 	 	 	 	 	 	 	 ddZddZ	d	d
dd	 	 	 	 	 	 	 	 	 ddZ
y)r   z
    SSH server session that runs a Python REPL.

    :param get_globals: callable that returns the current globals.
    :param get_locals: (optional) callable that returns the current locals.
    Nc                0    d  _         d fd}t                _         G  fdd      }t        ||xs | j                  t	        t        t         |              j                               _        d j                  _	        d j                  _
        y )Nc                 N            } | j                  dj                         | S )Nprint)
setdefault_print)dataget_globalsselfs    E/tmp/pip-target-20744g9h/lib/python/ptpython/contrib/asyncssh_repl.py_globalsz/ReplSSHServerSession.__init__.<locals>._globals'   s!    =DOOGT[[1K    c                  "    e Zd Zd fdZddZy)-ReplSSHServerSession.__init__.<locals>.Stdoutc                z    j                   .|j                  dd      }j                   j                  |       y y )N
z
)_chanreplacewrite)sr   r   s     r   r!   z3ReplSSHServerSession.__init__.<locals>.Stdout.write4   s4    ::)<<f5DJJ$$T* *r   c                     y N )r"   s    r   flushz3ReplSSHServerSession.__init__.<locals>.Stdout.flush9   s    r   N)r   strreturnNone)r(   r)   )__name__
__module____qualname__r!   r&   r   s   r   Stdoutr   3   s    +
r   r.   )r   
get_localsinputoutputF)r(   r   )r   r	   _input_piper   r
   r   r   	_get_sizereplenable_open_in_editorenable_system_bindings)r   r   r/   r   r.   s   ``   r   __init__zReplSSHServerSession.__init__"   s{     
	 -.	 	  !-X""VVX 6G	
	 +0		'+0		(r   c                    | j                   t        dd      S | j                   j                         \  }}}}t        ||      S )zU
        Callable that returns the current `Size`, required by Vt100_Output.
           O   )rowscolumns)r   r   get_terminal_sizer   widthheightpixwidth	pixheights        r   r3   zReplSSHServerSession._get_sizeI   sC     ::R,,151M1M1O.E68YVU33r   c                      _         t        j                   j                  j	                               }d fd}|j                  |       y)z:
        Client connected, run repl in coroutine.
        c                4    j                          d _        y r$   )closer   )_chanr   s    r   donez2ReplSSHServerSession.connection_made.<locals>.done]   s    JJLDJr   N)rF   objectr(   r)   )r   asyncioensure_futurer4   	run_asyncadd_done_callback)r   rG   frH   s   ``  r   connection_madez$ReplSSHServerSession.connection_madeS   s@     
 !!$))"5"5"78	 	
D!r   c                     y)NTr%   r-   s    r   shell_requestedz$ReplSSHServerSession.shell_requestedc   s    r   c                L    | j                   j                  j                          y)zE
        When the terminal size changes, report back to CLI.
        N)r4   app
_on_resizer>   s        r   terminal_size_changedz*ReplSSHServerSession.terminal_size_changedf   s     			  "r   c                :    | j                   j                  |       y)zT
        When data is received, send to inputstream of the CLI and repaint.
        N)r2   send)r   r   datatypes      r   data_receivedz"ReplSSHServerSession.data_receivedn   s     	d#r    r   )sependfilec               ~    |j                  t        t        |            }| j                  j	                  ||z          y)zU
        Alternative 'print' function that prints back into the SSH channel.
        N)joinmapr'   r   r!   )r   r[   r\   r]   r   data_as_strs         r   r   zReplSSHServerSession._printt   s/     hhs3~.

s*+r   r$   )r   r   r/   z_GetNamespace | Noner(   r)   )r(   r   )rG   r   r(   r)   )r(   bool)
r?   intr@   rc   rA   rc   rB   rc   r(   r)   )r   r   rX   z
int | Noner(   r)   )
r   rI   r[   r'   r\   r'   r]   r   r(   r)   )r*   r+   r,   __doc__r7   r3   rO   rQ   rU   rY   r   r%   r   r   r   r      s     NR%1(%16J%1	%1N4" ##"%#14#AD#	#$ ),$
,
,"%
,25
,DG
,	
,r   )rd   
__future__r   rJ   typingr   r   r   r   asyncsshprompt_toolkit.data_structuresr   prompt_toolkit.inputr	   prompt_toolkit.output.vt100r
   ptpython.python_inputr   r   ptpython.replr   __all__SSHServerSessionr'   r   r%   r   r   <module>ro      sK    #  , ,  / 2 4 ; $!
"d,844S9 d,r   