o
    i!                     @   sZ   d Z ddlZddlZddlZddlm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_itemCommc                       s   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 t| jdi | |r|nt j| _|| _|| _|| _	|r"|nd| j 
d| _|	r/|	ni | _|
r6|
ni | _d | _d | _d| _| jrO| j|||d d S d| _d S )Nzcomm-%sasciiTdatametadatabuffersF )superr   __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__r   I/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/comm/base_comm.pyr      s   
zBaseComm.__init__c                 K   s   t d)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   sr   |du r| j }t }|du rtd||  z| jd|||| j| jd d| _W dS  t	y8   |
|   w )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
   comm_managerr   r   r!   r   E   s(   

zBaseComm.openFc                 C   sJ   | j rdS d| _ |du r| j}| jd|||d |s#t |  dS 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   
   || _ 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      
zBaseComm.on_closec                 C   r4   )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   r5   r   r   r!   on_msg   r8   zBaseComm.on_msgc                 C   s(   t d| j| | jr| | dS 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   s^   t d| j| | jr+ddlm} | }|r|jd | | |r-|jd dS dS dS )zHandle a comm_msg messagezhandle_msg[%s](%s)r   )get_ipythonpre_executepost_executeN)r:   r;   r   r   IPythonr>   eventstrigger)r   r<   r>   shellr   r   r!   
handle_msg   s   
zBaseComm.handle_msg)
r   NNNNTNNNN)NNN)NNNF)__name__
__module____qualname____doc__r   r%   r)   r   r'   r3   r7   r9   r=   rE   __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   rM   r   r   fr   r   r!   register_target   s   

zCommManager.register_targetc                 C   s   | j |S )z5Unregister a callable registered with register_target)rM   poprP   r   r   r!   unregister_target   s   zCommManager.unregister_targetc                 C   s   |j }|| j|< |S )zRegister a new comm)r   rL   )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)rL   rS   r   )r   r   r   r   r!   r/      s   zCommManager.unregister_commc              	   C   sV   z| j | W S  ty*   td| ttjr'tdt| j 	  Y dS Y dS w )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)
rL   KeyErrorr:   warningisEnabledForloggingDEBUGr;   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 r0td	| nz||	| W dS  tyI   tjd
|dd Y nw z|	  W dS  tyb   tjddd Y dS w )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   r\   rM   getr-   r:   errorr.   r'   )
r   streamidentr<   r\   r]   r   r   rQ   r   r   r   r!   r*      s:   


zCommManager.comm_openc                 C   s\   |d }|d }|  |}|du rdS z|| W dS  ty-   tjd|dd Y dS w )zHandler for comm_msg messagesr]   r   NzException in comm_msg for %sTr^   )r[   rE   r.   r:   ra   r   rb   rc   r<   r]   r   r   r   r   r!   r2      s   
zCommManager.comm_msgc                 C   sp   |d }|d }|  |}|du rdS d| j| _| j|= z|| W dS  ty7   tjd|dd Y dS w )zHandler for comm_close messagesr]   r   NTzException in comm_close for %sr^   )r[   rL   r   r=   r.   r:   ra   rd   r   r   r!   r1     s   
zCommManager.comm_closeN)rF   rG   rH   rI   r   rR   rT   r-   r/   r[   r*   r2   r1   r   r   r   r!   rK      s    #rK   )rI   r   rX   r   traitlets.utils.importstringr   	getLoggerr:   r   rK   __all__r   r   r   r!   <module>   s    
 z