a
    lc/                     @   s  U d Z ddlZddlZddlZddlmZmZmZmZm	Z	m
Z
mZmZ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 dd	lmZ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dl&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3 ej4dkr$ddl5Z5ndZ5ee6d< dgZ7e8e9dddZ:ejddddZ;G dd dZ<G dd dZ=e=ddddZ>G dd dZ?dS ) z
Telnet server.
    N)	Any	AwaitableCallableListOptionalSetTextIOTuplecast)create_app_sessionget_app)run_in_terminal)Size)get_event_loop)AnyFormattedTextto_formatted_text)	PipeInputcreate_pipe_input)Vt100_Output)print_formatted_text)	BaseStyle
DummyStyle   )logger)DOECHOIACLINEMODEMODENAWSSBSESENDSUPPRESS_GO_AHEADTTYPEWILLTelnetProtocolParser)      contextvarsTelnetServer)numberreturnc                 C   s
   t | fS N)bytes)r+    r/   `/var/www/html/django/DPS/env/lib/python3.9/site-packages/prompt_toolkit/contrib/telnet/server.pyint2byte.   s    r1   )
connectionr,   c                 C   s   t d | tt t  | tt t  | tt t t	 t
d t t  | tt t  | tt t  | tt t  | tt t t t t  d S )NzInitializing telnet connectionr   )r   infosendr   r   r   r%   r#   r    r   r1   r!   r   r   r$   r"   r2   r/   r/   r0   _initialize_telnet2   s    
&r6   c                   @   s   e Zd ZdZejeddddZeddddZed	d
dZ	dd	ddZ
dd	ddZeed	ddZeed	ddZdS )_ConnectionStdoutzk
    Wrapper around socket which provides `write` and `flush` methods for the
    Vt100_Output output.
    N)r2   encodingr,   c                 C   s"   || _ || _d| _g | _d| _d S )NstrictF)	_encoding_connection_errors_buffer_closed)selfr2   r8   r/   r/   r0   __init__U   s
    z_ConnectionStdout.__init__datar,   c                 C   s2   | dd}| j|j| j| jd |   d S )N
z
)errors)replacer=   appendencoder:   r<   flushr?   rB   r/   r/   r0   write\   s    z_ConnectionStdout.writer,   c                 C   s   dS NTr/   r?   r/   r/   r0   isattya   s    z_ConnectionStdout.isattyc              
   C   s\   z| j s| jd| j W n2 tyP } ztd|  W Y d }~n
d }~0 0 g | _d S )N    z"Couldn't send data over socket: %s)r>   r;   r4   joinr=   OSErrorr   warning)r?   er/   r/   r0   rH   d   s    $z_ConnectionStdout.flushc                 C   s
   d| _ d S rL   )r>   rM   r/   r/   r0   closem   s    z_ConnectionStdout.closec                 C   s   | j S r-   )r:   rM   r/   r/   r0   r8   p   s    z_ConnectionStdout.encodingc                 C   s   | j S r-   )r<   rM   r/   r/   r0   rD   t   s    z_ConnectionStdout.errors)__name__
__module____qualname____doc__socketstrr@   rJ   boolrN   rH   rT   propertyr8   rD   r/   r/   r/   r0   r7   O   s   	r7   c                   @   s   e Zd ZdZdejeeef ed ge	d f dee
e eedd	ddZddd	d
ZeddddZddddZeddddZeddddZeg df ddddZddddZdS )TelnetConnectionz6
    Class that represents one Telnet connection.
    TNr*   )	connaddrinteractserverr8   stylevt100_input
enable_cprr,   c	                    s   |_ |_|_|_|_|_d_t _	|_
 _d _tddd_t| tdfddttt||d_td d	fd
d}	ttd dfdd}
td d fdd}t|	|
|_d _d S )NF(   O   rowscolumnsrK   c                      s    j S r-   )sizer/   rM   r/   r0   get_size   s    z+TelnetConnection.__init__.<locals>.get_size)r8   rA   c                    s    j |  dS )z-TelnetProtocolParser 'data_received' callbackN)rc   Z
send_bytesrB   rM   r/   r0   data_received   s    z0TelnetConnection.__init__.<locals>.data_received)rh   ri   r,   c                    s2   t | |d _ jdur. jr. jdd  dS )z-TelnetProtocolParser 'size_received' callbackrg   Nc                   S   s
   t   S r-   )r   Z
_on_resizer/   r/   r/   r0   <lambda>   rO   zBTelnetConnection.__init__.<locals>.size_received.<locals>.<lambda>)r   rj   vt100_outputcontextrunrg   rM   r/   r0   size_received   s    z0TelnetConnection.__init__.<locals>.size_received)ttyper,   c                    s"   t j|  d_j  dS )z.TelnetProtocolParser 'ttype_received' callback)termrd   N)r   stdoutro   _readyset)rs   rd   rk   r?   r/   r0   ttype_received   s    
z1TelnetConnection.__init__.<locals>.ttype_received)r^   r_   r`   ra   r8   rb   r>   asyncioEventrv   rc   rd   ro   r   rj   r6   r
   r   r7   ru   r.   intrZ   r&   parserrp   )r?   r^   r_   r`   ra   r8   rb   rc   rd   rm   rr   ry   r/   rx   r0   r@   ~   s(    
zTelnetConnection.__init__rK   c                    s   dd fdd}t  }| j| zf j I dH  t j jd* t	  _
  I dH  W d   n1 sx0    Y  W    n
   0 dS )z"
        Run application.
        NrK   c                     s8    j d} | r |  ntd j     d S )Ni   z"Connection closed by client. %r %r)r^   recvfeedr   r3   r_   rT   rl   rM   r/   r0   handle_incoming_data   s
    z>TelnetConnection.run_application.<locals>.handle_incoming_data)inputoutput)r   
add_readerr^   rv   waitr   rc   ro   r)   copy_contextrp   r`   rT   )r?   r   loopr/   rM   r0   run_application   s    

0z TelnetConnection.run_applicationrA   c                 C   s   | j | dS )zF
        Handler for incoming data. (Called by TelnetServer.)
        N)r}   r   rI   r/   r/   r0   r      s    zTelnetConnection.feedc                 C   s<   | j s8d| _ | j  t | j | j  | j  dS )z#
        Closed by client.
        TN)r>   rc   rT   r   remove_readerr^   ru   rM   r/   r/   r0   rT      s    

zTelnetConnection.close)formatted_textr,   c                 C   s0   | j du rdS t|}t| j || jp(t  dS )z*
        Send text to the client.
        N)ro   r   r   rb   r   r?   r   r/   r/   r0   r4      s    
zTelnetConnection.sendc                    s   t    fddS )z\
        Send text to the client.
        This is asynchronous, returns a `Future`.
        c                      s
     S r-   )r4   r/   r   r?   r/   r0   rn      rO   z4TelnetConnection.send_above_prompt.<locals>.<lambda>)r   _run_in_terminalr   r/   r   r0   send_above_prompt   s    z"TelnetConnection.send_above_prompt)funcr,   c                 C   s"   | j r| j t| ntdd S )Nz2Called _run_in_terminal outside `run_application`.)rp   rq   r   RuntimeError)r?   r   r/   r/   r0   r      s    z!TelnetConnection._run_in_terminalc                 C   s4   | j du rdS | j   | j dd | j   dS )zB
        Erase the screen and move the cursor to the top.
        Nr   )ro   erase_screenZcursor_gotorH   rM   r/   r/   r0   r      s
    

zTelnetConnection.erase_screen)T)rU   rV   rW   rX   rY   r	   rZ   r|   r   r   r   r   r   r[   r@   r   r.   r   rT   r   r4   r   r   r   r/   r/   r/   r0   r]   y   s(    
8r]   c                    s   d S r-   r/   r5   r/   r/   r0   _dummy_interact	  s    r   c                	   @   s   e Zd ZdZddedddfeeeege	d f ee
e edddd	Zeeeejd
ddZddddZddddZddddZdS )r*   z'
    Telnet server implementation.
    z	127.0.0.1   zutf-8NT)hostportr`   r8   rb   rd   r,   c                 C   s<   || _ || _|| _|| _|| _|| _g | _t | _d | _	d S r-   )
r   r   r`   r8   rb   rd   _application_tasksrw   connections_listen_socket)r?   r   r   r`   r8   rb   rd   r/   r/   r0   r@     s    
zTelnetServer.__init__)r   r   r,   c                 C   s>   t  t jt j}|t jt jd |||f |d |S )Nr      )rY   AF_INETSOCK_STREAM
setsockopt
SOL_SOCKETSO_REUSEADDRbindlisten)clsr   r   sr/   r/   r0   _create_socket'  s
    
zTelnetServer._create_socketrK   c                 C   s:   |  | j| j| _td| j| j t | j| j dS )zn
        Start the telnet server.
        Don't forget to call `loop.run_forever()` after doing this.
        z.Listening for telnet connections on %s port %rN)	r   r   r   r   r   r3   r   r   _acceptrM   r/   r/   r0   start1  s
    
zTelnetServer.startc              	      sv   | j rt | j  | j   | jD ]}|  q$| jD ]8}z|I d H  W q8 tjyn   t	dt
| Y q80 q8d S )NzTask %s cancelled)r   r   r   rT   r   cancelrz   CancelledErrorr   debugrZ   )r?   tr/   r/   r0   stop=  s    



zTelnetServer.stopc                    sd   j du rdS j  \ tjdg R   dd fdd}t | j dS )z1
        Accept new incoming connection.
        NzNew connection %r %rrK   c                     s`  zJzt  } t jjj| jd}j| tj	dg R   z0|
 I d H  W j| tj	dg R   n j| tj	dg R   0 W d    n1 s0    Y  W n~ ty   t	d Y nd ty   t	d Y nJ ty< } z0tdt|j | dd l}|  W Y d }~n
d }~0 0 W j nj 0 d S )N)r8   rb   rc   rd   zStarting interaction %r %rzStopping interaction %r %rz)Unhandled EOFError in telnet application.z2Unhandled KeyboardInterrupt in telnet application.zGot %sr   )r   r]   r`   r8   rb   rd   r   addr   r3   r   removeEOFErrorKeyboardInterruptBaseExceptionprinttyperU   	traceback	print_excr   )rc   r2   rS   r   r_   r^   r?   taskr/   r0   rq   W  s:    
6 z!TelnetServer._accept.<locals>.run)r   acceptr   r3   r   create_taskr   rF   )r?   rq   r/   r   r0   r   L  s    
&zTelnetServer._accept)rU   rV   rW   rX   r   rZ   r|   r   r]   r   r   r   r[   r@   classmethodrY   r   r   r   r   r/   r/   r/   r0   r*     s*   	)@rX   rz   rY   systypingr   r   r   r   r   r   r   r	   r
   Z"prompt_toolkit.application.currentr   r   Z*prompt_toolkit.application.run_in_terminalr   Zprompt_toolkit.data_structuresr   Zprompt_toolkit.eventloopr   Zprompt_toolkit.formatted_textr   r   Zprompt_toolkit.inputr   r   Zprompt_toolkit.output.vt100r   Zprompt_toolkit.rendererr   Zprompt_toolkit.stylesr   r   logr   protocolr   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   version_infor)   __annotations____all__r|   r.   r1   r6   r7   r]   r   r*   r/   r/   r/   r0   <module>   s6   ,<
* 