a
    ÷£Be·  ã                   @   s\   d Z ddlZddlZddlZddlZddlmZ ddlmZ dd„ Z	G dd„ dej
jƒZdS )	z9Tornado websocket handler to serve a terminal interface.
é    N)Úgen)Úrun_on_executorc                 C   s   t | tƒr|  d¡S | S )Nzutf-8)Ú
isinstanceÚbytesÚdecode)Ús© r   úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/terminado/websocket.pyÚ_cast_unicode   s    

r
   c                       s†   e Zd ZdZdd„ Zddd„Zd‡ fdd„	Zd	d
„ Zdd„ Ze	j
dd„ ƒZdd„ Zdd„ Zdeddœdd„Zedd�dd„ ƒZ‡  ZS )Ú
TermSocketz Handler for a terminal websocketc                 C   sN   || _ d| _d| _d | _|j| _t t¡| _	d| _
t t dd¡¡dk| _d S )NÚ )NNZLOG_TERMINAL_OUTPUTÚfalseÚtrue)Úterm_managerÚ	term_nameÚsizeÚterminalZblocking_io_executorÚ_blocking_io_executorÚloggingÚ	getLoggerÚ__name__Ú_loggerÚ_user_commandÚstrÚlowerÚosÚgetenvÚ_enable_output_logging)Úselfr   r   r   r	   Ú
initialize   s    zTermSocket.initializeNc                 C   s   |   |p| jj d¡¡S )z1Deprecated: backward-compat for terminado <= 0.5.ZOrigin)Zcheck_originÚrequestÚheadersÚget)r   Úoriginr   r   r	   Úorigin_check&   s    zTermSocket.origin_checkc                    s    t ƒ  |¡ | j d|¡ t|ƒ}|p(d| _| j |¡| _| jj	 
| ¡ |  di g¡ | j d| j¡ d}| jj ¡ }|s|qŽ| ¡ }||7 }qv|rœ|  |¡ dS )z‚Websocket connection opened.

        Call our terminal manager to get a terminal, and connect to it as a
        client.
        zTermSocket.open: %sÚttyÚsetupzTermSocket.open: Opened %sr   N)ÚsuperÚopenr   Úinfor
   r   r   Zget_terminalr   ÚclientsÚappendÚsend_json_messageZread_bufferÚcopyÚpopleftÚon_pty_read)r   Zurl_componentÚbufferedZpreopen_bufferr   ©Ú	__class__r   r	   r(   *   s     

zTermSocket.openc                 C   s   |   d|g¡ dS )z$Data read from pty; send to frontendÚstdoutN)r,   ©r   Útextr   r   r	   r/   G   s    zTermSocket.on_pty_readc                 C   sL   t  |¡}|  |¡ | jrH|d dkrHt|d tƒrH|  d|d › �¡ d S )Nr   r3   é   zSTDOUT: )ÚjsonÚdumpsZwrite_messager   r   r   Úlog_terminal_output)r   ÚcontentZjson_msgr   r   r	   r,   K   s
    

zTermSocket.send_json_messagec                 c   sœ   t  |¡}|d }| jdus J ‚|dkrx|  |d ¡V  | jr˜|d dkrd|  d| j› �¡ d| _q˜|  j|d 7  _n |dkr˜|dd	… | _| j ¡  dS )
zÇHandle incoming websocket message

        We send JSON arrays, where the first element is a string indicating
        what kind of message this is. Data associated with the message follows.
        r   NÚstdinr6   úzSTDIN: r   Zset_sizeé   )	r7   Úloadsr   Ústdin_to_ptyprocr   r9   r   r   Úresize_to_smallest)r   ÚmessageÚcommandÚmsg_typer   r   r	   Ú
on_messageS   s    
zTermSocket.on_messagec                 C   s:   | j  d¡ | jr*| jj | ¡ | j ¡  | j | ¡ dS )z„Handle websocket closing.

        Disconnect from our terminal, and tell the terminal manager we're
        disconnecting.
        zWebsocket closedN)r   r)   r   r*   Úremover@   r   Zclient_disconnected©r   r   r   r	   Úon_closej   s
    
zTermSocket.on_closec                 C   s    |   ddg¡ |  ¡  d| _dS )z9Terminal closed: tell the frontend, and close the socket.Z
disconnectr6   N)r,   Úcloser   rF   r   r   r	   Úon_pty_diedv   s    zTermSocket.on_pty_diedr   )ÚlogÚreturnc                 C   s   | j  |¡ dS )zg
        Logs the terminal input/output
        :param log: log line to write
        :return:
        N)r   Údebug)r   rJ   r   r   r	   r9   |   s    zTermSocket.log_terminal_outputr   )Úexecutorc                 C   s   | j dur| j j |¡ dS )a   Handles stdin messages sent on the websocket.

        This is a blocking call that should NOT be performed inside the
        server primary event loop thread. Messages must be handled
        asynchronously to prevent blocking on the PTY buffer.
        N)r   ZptyprocÚwriter4   r   r   r	   r?   „   s    
zTermSocket.stdin_to_ptyproc)N)N)r   )r   Ú
__module__Ú__qualname__Ú__doc__r   r$   r(   r/   r,   r   Ú	coroutinerD   rG   rI   r   r9   r   r?   Ú__classcell__r   r   r1   r	   r      s   

r   )rQ   r7   r   r   Ztornado.websocketÚtornador   Ztornado.concurrentr   r
   Z	websocketZWebSocketHandlerr   r   r   r   r	   Ú<module>   s   