U
    e                      @   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 )BaseCommzClass 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 | |r|nt j| _|| _|| _|| _|r<|nd| j 	d| _
|	rV|	ni | _|
rd|
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/ankuromar296_gmail_com/.local/lib/python3.8/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   sz   |dkr| j }t }|dkr&td||  z$| jd|||| j| jd d| _W n  t	k
rt   |
|   Y nX 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k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 )zRegister 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 )zRegister 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   sT   z| j | W S  tk
rN   td| ttjrJtdt| j 	  Y nX dS )zGet 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kr`td	| n6z||	| W dS  tk
r   tjd
|dd Y nX z|	  W n" tk
r   tjddd Y nX 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kr&dS z|| W n$ tk
rX   tjd|dd Y nX 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   sr   |d }|d }|  |}|dkr&dS d| j| _| j|= z|| W n$ tk
rl   tjd|dd Y nX 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