a
    ÷£Beä   ã                   @   sZ   d Z ddlZddlZddlmZ ddlZe d¡ZG dd„ dƒZG dd„ dƒZ	ddgZ
dS )	z@Default classes for Comm and CommManager, for usage in IPython.
é    N)Úimport_itemZCommc                
       sr   e Zd ZdZd‡ fdd„	Zddd„Zd	d
„ Zddd„Zddd„Zddd„Z	dd„ Z
dd„ Zdd„ Zdd„ Z‡  ZS )ÚBaseCommz¶Class for communicating between a Frontend and a Kernel

    Must be subclassed with a publish_msg method implementation which
    sends comm messages through the iopub channel.
    ÚcommNTc                    s¢   t ƒ jf i |¤Ž |r|nt ¡ j| _|| _|| _|| _|r@|nd| j  	d¡| _
|	rZ|	ni | _|
rh|
ni | _d | _d | _d| _| jr˜| j|||d� nd| _d S )Nzcomm-%sÚasciiT©ÚdataÚmetadataÚbuffersF)ÚsuperÚ__init__ÚuuidÚuuid4ÚhexÚcomm_idÚprimaryÚtarget_nameÚtarget_moduleÚencodeÚtopicÚ
_open_dataÚ_close_dataÚ_msg_callbackÚ_close_callbackÚ_closedÚopen)Úselfr   r   r   r	   r   r   r   r   r   r   Úkwargs©Ú	__class__© úQ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/comm/base_comm.pyr      s    zBaseComm.__init__c                 K   s   t dƒ‚d S )Nz*publish_msg Comm method is not implemented)ÚNotImplementedError)r   Úmsg_typer   r   r	   Úkeysr   r   r    Úpublish_msg<   s    zBaseComm.publish_msgc                 C   s   | j dd� dS )ztrigger close on gcT)ÚdeletingN)Úclose©r   r   r   r    Ú__del__?   s    zBaseComm.__del__c                 C   sx   |du r| j }t ¡ }|du r&tdƒ‚| | ¡ z$| jd|||| j| jd� d| _W n t	yr   | 
| ¡ ‚ Y n0 dS )z+Open the frontend-side version of this commNz.Comms cannot be opened without a comm_manager.Ú	comm_open)r   r   r	   r   r   F)r   r   Úget_comm_managerÚRuntimeErrorÚregister_commr$   r   r   r   Ú	ExceptionÚunregister_comm)r   r   r   r	   Zcomm_managerr   r   r    r   E   s&    
ú

zBaseComm.openFc                 C   sF   | j r
dS d| _ |du r| j}| jd|||d� |sBt ¡  | ¡ dS )z,Close the frontend-side version of this commNTÚ
comm_closer   )r   r   r$   r   r*   r.   )r   r   r   r	   r%   r   r   r    r&   ]   s    üzBaseComm.closec                 C   s   | j d|||d� dS )z8Send a message to the frontend-side version of this commÚcomm_msgr   N)r$   )r   r   r   r	   r   r   r    Úsendo   s    üzBaseComm.sendc                 C   s
   || _ dS )z©Register a callback for comm_close

        Will be called with the `data` of the close message.

        Call `on_close(None)` to disable an existing callback.
        N)r   ©r   Úcallbackr   r   r    Úon_closez   s    zBaseComm.on_closec                 C   s
   || _ dS )z©Register a callback for comm_msg

        Will be called with the `data` of any comm_msg messages.

        Call `on_msg(None)` to disable an existing callback.
        N)r   r2   r   r   r    Úon_msgƒ   s    zBaseComm.on_msgc                 C   s$   t  d| j|¡ | jr |  |¡ dS )zHandle a comm_close messagezhandle_close[%s](%s)N)ÚloggerÚdebugr   r   )r   Úmsgr   r   r    Úhandle_closeŽ   s    zBaseComm.handle_closec                 C   sV   t  d| j|¡ | jrRddlm} |ƒ }|r8|j d¡ |  |¡ |rR|j d¡ dS )zHandle a comm_msg messagezhandle_msg[%s](%s)r   )Úget_ipythonZpre_executeZpost_executeN)r6   r7   r   r   ZIPythonr:   ÚeventsÚtrigger)r   r8   r:   Úshellr   r   r    Ú
handle_msg”   s    
zBaseComm.handle_msg)
r   NNNNTNNNN)NNN)NNN)NNNF)NNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r(   r   r&   r1   r4   r5   r9   r>   Ú__classcell__r   r   r   r    r      s*             õ$



	r   c                   @   sX   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )ÚCommManagerzDDefault CommManager singleton implementation for Comms in the Kernelc                 C   s   i | _ i | _d S )N)ÚcommsÚtargetsr'   r   r   r    r   §   s    zCommManager.__init__c                 C   s    t |tƒrt|ƒ}|| j|< dS )a  Register a callable f for a given target name

        f will be called with two arguments when a comm_open message is received with `target`:

        - the Comm instance
        - the `comm_open` message itself.

        f can be a Python callable or an import string for one.
        N)Ú
isinstanceÚstrr   rF   ©r   r   Úfr   r   r    Úregister_target«   s    

zCommManager.register_targetc                 C   s   | j  |¡S )z5Unregister a callable registered with register_target)rF   ÚpoprI   r   r   r    Úunregister_targetº   s    zCommManager.unregister_targetc                 C   s   |j }|| j|< |S )zRegister a new comm)r   rE   )r   r   r   r   r   r    r,   ¾   s    
zCommManager.register_commc                 C   s   | j  |j¡}dS )z,Unregister a comm, and close its counterpartN)rE   rL   r   )r   r   r   r   r    r.   Ä   s    zCommManager.unregister_commc              	   C   sR   z| j | W S  tyL   t d|¡ t tj¡rHt dt| j  	¡ ƒ¡ Y n0 dS )z½Get a comm with a particular id

        Returns the comm if found, otherwise None.

        This will not raise an error,
        it will log messages if the comm cannot be found.
        zNo such comm: %szCurrent comms: %sN)
rE   ÚKeyErrorr6   ÚwarningÚisEnabledForÚloggingÚDEBUGr7   Úlistr#   )r   r   r   r   r    Úget_commÉ   s    zCommManager.get_commc           
      C   sÆ   ddl m} |d }|d }|d }| j |d¡}||d|d�}	|  |	¡ |du r`t d	|¡ n4z||	|ƒ W dS  ty’   tjd
|dd� Y n0 z|	 ¡  W n  tyÀ   tjddd� Y n0 dS )zHandler for comm_open messagesr   )Úcreate_commÚcontentr   r   NF)r   r   r   z"No such comm target registered: %sz&Exception opening comm with target: %sT©Úexc_infozqCould not close comm during `comm_open` failure
                clean-up.  The comm may not have been opened yet.)	r   rU   rF   Úgetr,   r6   Úerrorr-   r&   )
r   ÚstreamÚidentr8   rU   rV   r   r   rJ   r   r   r   r    r)   Û   s2    ý

ýzCommManager.comm_openc                 C   s\   |d }|d }|   |¡}|du r&dS z| |¡ W n" tyV   tjd|dd� Y n0 dS )zHandler for comm_msg messagesrV   r   NzException in comm_msg for %sTrW   )rT   r>   r-   r6   rZ   ©r   r[   r\   r8   rV   r   r   r   r   r    r0   ü   s    
zCommManager.comm_msgc                 C   sp   |d }|d }|   |¡}|du r&dS d| j| _| j|= z| |¡ W n" tyj   tjd|dd� Y n0 dS )zHandler for comm_close messagesrV   r   NTzException in comm_close for %srW   )rT   rE   r   r9   r-   r6   rZ   r]   r   r   r    r/   	  s    
zCommManager.comm_closeN)r?   r@   rA   rB   r   rK   rM   r,   r.   rT   r)   r0   r/   r   r   r   r    rD   ¢   s   !rD   )rB   rQ   r   Ztraitlets.utils.importstringr   r   Ú	getLoggerr6   r   rD   Ú__all__r   r   r   r    Ú<module>   s   
 x