
    bc!                         d Z ddlZddlZddlZddlmZ  ej        d          Z G d d          Z G d d          Z	ddgZ
dS )	z@Default classes for Comm and CommManager, for usage in IPython.
    N)import_itemCommc                   v     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 d fd	ZddZd ZddZdd
ZddZ	d Z
d Zd Zd Z x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                     t          t          |           j        di | |r|nt          j                    j        | _        || _        || _        || _	        |r|nd| j        z  
                    d          | _        |	r|	ni | _        |
r|
ni | _        d | _        d | _        d| _        | j        r|                     |||           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__s               .lib/python3.11/site-packages/comm/base_comm.pyr   zBaseComm.__init__   s     	'h&00000")?wwtz||/?&*#SUU)dl*B)J)J7)S)S
(2:***5=;;2!#< 	!II4(GIDDDDD DLLL    c                      t          d          )Nz*publish_msg Comm method is not implemented)NotImplementedError)r    msg_typer   r   r   keyss         r#   publish_msgzBaseComm.publish_msg<   s    !"NOOOr$   c                 2    |                      d           dS )ztrigger close on gcT)deletingN)closer    s    r#   __del__zBaseComm.__del__?   s    

D
!!!!!r$   c                 ,   || j         }t          j                    }|t          d          |                    |            	 |                     d|||| j        | j                   d| _        dS # t          $ r |
                    |             w xY 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_managers        r#   r   zBaseComm.openE   s      	#?D,.. 	QOPPP""4(((	! ,"0     !DLLL 	 	 	((...	s   ,A2 2!BFc                     | j         rdS d| _         || j        }|                     d|||           |s(t          j                                        |            dS dS )z,Close the frontend-side version of this commNT
comm_closer
   )r   r   r)   r   r1   r5   )r    r   r   r   r+   s        r#   r,   zBaseComm.close]   s    < 	F 	$#D	 	 	
 	
 	
  	:!##33D99999	: 	:r$   c                 8    |                      d|||           dS )z8Send a message to the frontend-side version of this commcomm_msgr
   N)r)   )r    r   r   r   s       r#   sendzBaseComm.sendo   s5    	 	 	
 	
 	
 	
 	
r$   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    callbacks     r#   on_closezBaseComm.on_closez   s      (r$   c                     || _         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   r=   s     r#   on_msgzBaseComm.on_msg   s     &r$   c                     t                               d| j        |           | j        r|                     |           dS dS )zHandle a comm_close messagezhandle_close[%s](%s)N)loggerdebugr   r   )r    msgs     r#   handle_closezBaseComm.handle_close   sJ    +T\3??? 	&  %%%%%	& 	&r$   c                    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)rC   rD   r   r   IPythonrH   eventstrigger)r    rE   rH   shells       r#   
handle_msgzBaseComm.handle_msg   s    )4<=== 	5++++++KMME 4$$]333s### 5$$^44444	5 	55 5r$   )
r   NNNNTNNNN)NNN)NNNF)__name__
__module____qualname____doc__r   r)   r.   r   r,   r;   r?   rA   rF   rO   __classcell__)r"   s   @r#   r   r      s         "! "! "! "! "! "!HP P P P" " "   0: : : :$
 
 
 
( ( (& & && & &5 5 5 5 5 5 5r$   r   c                   H    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZdS )CommManagerzDDefault CommManager singleton implementation for Comms in the Kernelc                 "    i | _         i | _        d S )N)commstargetsr-   s    r#   r   zCommManager.__init__   s    
r$   c                 b    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   rY   r    r   fs      r#   register_targetzCommManager.register_target   s3     a 	AA$%[!!!r$   c                 6    | j                             |          S )z5Unregister a callable registered with register_target)rY   popr]   s      r#   unregister_targetzCommManager.unregister_target   s    |,,,r$   c                 (    |j         }|| j        |<   |S )zRegister a new comm)r   rX   )r    r   r   s      r#   r3   zCommManager.register_comm   s    ,"
7r$   c                 D    | j                             |j                  }dS )z,Unregister a comm, and close its counterpartN)rX   ra   r   )r    r   s     r#   r5   zCommManager.unregister_comm   s     z~~dl++r$   c                 B   	 | j         |         S # t          $ r t                              d|           t                              t
          j                  rBt                              dt          | j         	                                                     Y dS Y dS w xY 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)
rX   KeyErrorrC   warningisEnabledForloggingDEBUGrD   listr(   )r    r   s     r#   get_commzCommManager.get_comm   s    	K:g&& 	K 	K 	KNN-w777""7=11 K0$tz7H7H2I2IJJJJJJK K K	Ks    BBBc                    ddl m} |d         }|d         }|d         }| j                            |d          } ||d|          }	|                     |	           |t
                              d	|           n<	  ||	|           dS # t          $ r  t
                              d
|d           Y nw xY w	  |	j                     dS # t          $ r  t
                              dd           Y dS w xY 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   rn   rY   getr3   rC   errorr4   r,   )
r    streamidentrE   rn   ro   r   r   r^   r   s
             r#   r0   zCommManager.comm_open   sb   $$$$$$i.)$m,L[$//{#
 
 

 	4    		LL={KKKK$   <kTX      	DJLLLLL 	 	 	LLE       	s$   <B
 
'B43B48C	 	&C32C3c                     |d         }|d         }|                      |          }|dS 	  |j        |           dS # t          $ r! t                              d|d           Y dS w xY w)zHandler for comm_msg messagesro   r   NzException in comm_msg for %sTrp   )rl   rO   r4   rC   rs   r    rt   ru   rE   ro   r   r   s          r#   r:   zCommManager.comm_msg   s    i.)$}}W%% 	F	QDOC      	Q 	Q 	QLL74LPPPPPP	Qs   = 'A('A(c                 
   |d         }|d         }|                      |          }|dS d| j        |         _        | j        |= 	  |j        |           dS # t          $ r! t
                              d|d           Y dS w xY w)zHandler for comm_close messagesro   r   NTzException in comm_close for %srp   )rl   rX   r   rF   r4   rC   rs   rw   s          r#   r8   zCommManager.comm_close  s    i.)$}}W%% 	F&*
7#Jw	SDc""""" 	S 	S 	SLL97TLRRRRRR	Ss   A 'BBN)rP   rQ   rR   rS   r   r_   rb   r3   r5   rl   r0   r:   r8   r   r$   r#   rV   rV      s        NN  & & &- - -  , , ,
K K K$! ! !FQ Q QS S S S Sr$   rV   )rS   r   ri   r   traitlets.utils.importstringr   	getLoggerrC   r   rV   __all__r   r$   r#   <module>r|      s        4 4 4 4 4 4		6	"	"N5 N5 N5 N5 N5 N5 N5 N5bwS wS wS wS wS wS wS wSt *
%r$   