
    Mh'                       d Z ddlm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
Z
ej        rddlmZ  ej        d          Zej        eej        f         Zej        ej        eej        f                  Zej        ej        e                  Zej        egdf         Zej        degdf         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.
    )annotationsN)import_item)	ZMQStreamCommBaseCommc                       e Zd ZdZ	 	 	 	 	 	 	 	 	 	 d+d, fdZ	 	 	 d-d.dZd/dZ	 d-d0dZ	 	 	 	 d1d2d!Z	 d-d0d"Z	d3d%Z
d3d&Zd4d)Zd4d*Z xZS )5r   zClass 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.
    commNTtarget_namestrdata	MaybeDictmetadatabuffersBuffersTypecomm_id
str | Noneprimarybooltarget_moduletopicbytes | None
_open_data_close_datakwargst.AnyreturnNonec                    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r   r   r   F )super__init__uuiduuid4hexr   r   r
   r   encoder   r   r   _msg_callback_close_callback_closedopen)selfr
   r   r   r   r   r   r   r   r   r   r   	__class__s               N/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/comm/base_comm.pyr#   zBaseComm.__init__$   s     	""6"""")?wwtz||/?&*#SUU)dl*B)J)J7)S)S
(2:***5=;;22648< 	!II4(GIDDDDD DLLL    msg_typekeysc                $    d}t          |          )Nz*publish_msg Comm method is not implemented)NotImplementedError)r,   r0   r   r   r   r1   msgs          r.   publish_msgzBaseComm.publish_msgH   s     ;!#&&&r/   c                    t          j        t                    5  |                     d           ddd           dS # 1 swxY w Y   dS )ztrigger close on gcT)deletingN)
contextlibsuppress	Exceptioncloser,   s    r.   __del__zBaseComm.__del__S   s     ++ 	& 	& JJJ%%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&s   >AAc                0   || j         }t          j                    }|d}t          |          |                    |            	 |                     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_commr5   r
   r   r*   r:   unregister_comm)r,   r   r   r   comm_managerr4   s         r.   r+   zBaseComm.open\   s    
 <?D,..BCs###""4(((	! ,"0     !DLLL 	 	 	((...	s   ,A4 4!BFr7   c                    | 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   r5   r	   r@   rC   )r,   r   r   r   r7   s        r.   r;   zBaseComm.closew   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)r5   )r,   r   r   r   s       r.   sendzBaseComm.send   s7     		 	 	
 	
 	
 	
 	
r/   callbackCommCallback | Nonec                    || _         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,   rJ   s     r.   on_closezBaseComm.on_close   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(   rM   s     r.   on_msgzBaseComm.on_msg   s     &r/   r4   MessageTypec                    t                               d| j        |           | j        r|                     |           dS dS )zHandle a comm_close messagezhandle_close[%s](%s)N)loggerdebugr   r)   )r,   r4   s     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)rS   rT   r   r(   IPythonrW   eventstrigger)r,   r4   rW   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)r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )NNN)r0   r   r   r   r   r   r   r   r1   r   r   r   r   r   )r   r   r   r   r   r   r   r   )NNNF)
r   r   r   r   r   r   r7   r   r   r   )rJ   rK   r   r   )r4   rQ   r   r   )__name__
__module____qualname____doc__r#   r5   r=   r+   r;   rI   rN   rP   rU   r^   __classcell__)r-   s   @r.   r   r      s\         ""#"$(" $!%"! "! "! "! "! "! "!N "#	' 	' 	' 	' 	'& & & & Z^    : "#: : : : :2 Z^	
 	
 	
 	
 	
( ( ( (& & & && & & &5 5 5 5 5 5 5 5r/   c                  Z    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 Kernelr   r   c                "    i | _         i | _        d S )N)commstargetsr<   s    r.   r#   zCommManager.__init__   s    *,
68r/   r
   r   fCommTargetCallback | strc                    t          |t                    rt          |          }t          j        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)
isinstancer   r   tcastCommTargetCallbackri   r,   r
   rj   s      r.   register_targetzCommManager.register_target   s@     a 	AA$%F+=q$A$A[!!!r/   rp   c                6    | j                             |          S )z5Unregister a callable registered with register_target)ri   poprq   s      r.   unregister_targetzCommManager.unregister_target   s    |,,,r/   r	   r   c                (    |j         }|| j        |<   |S )zRegister a new comm)r   rh   )r,   r	   r   s      r.   rB   zCommManager.register_comm   s    ,"
7r/   c                D    | j                             |j                  }dS )z,Unregister a comm, and close its counterpartN)rh   rt   r   )r,   r	   s     r.   rC   zCommManager.unregister_comm   s     z~~dl++r/   r   BaseComm | Nonec                <   	 | j         |         S # t          $ r t                              d|           t                              t
          j                  r?t                              dt          | j         	                                                     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)
rh   KeyErrorrS   warningisEnabledForloggingDEBUGrT   listr1   )r,   r   s     r.   get_commzCommManager.get_comm   s    	:g&& 	 	 	NN-w777""7=11 K0$tz7H7H2I2IJJJ44	s    BBBstreamr   identr4   rQ   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	   r   ri   getrB   rS   errorr:   r;   )
r,   r   r   r4   r   r   r   r
   rj   r	   s
             r.   r?   zCommManager.comm_open   sW   $$$$$$i.)$m,L[$//{#
 
 

 	4   9LL={KKKKc$ c c cE{]abbbbbc	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 messagesr   r   NzException in comm_msg for %sTr   )r   r^   r:   rS   r   r,   r   r   r4   r   r   r	   s          r.   rH   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 messagesr   r   NTzException in comm_close for %sr   )r   rh   r*   rU   r:   rS   r   r   s          r.   rF   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Nr_   )r
   r   rj   rk   r   r   )r
   r   rj   rp   r   rp   )r	   r   r   r   )r	   r   r   r   )r   r   r   rx   )r   r   r   r   r4   rQ   r   r   )r`   ra   rb   rc   r#   rr   ru   rB   rC   r   r?   rH   rF   r!   r/   r.   rf   rf      s        NN9 9 9 9B B B B- - - -   , , , ,
   &   BQ Q Q QS S S S S Sr/   rf   )rc   
__future__r   r8   r}   typingrn   r$   traitlets.utils.importstringr   r	   TYPE_CHECKINGzmq.eventloop.zmqstreamr   	getLoggerrS   Dictr   AnyrQ   Optionalr   Listbytesr   CallableCommCallbackrp   r   rf   __all__r!   r/   r.   <module>r      sw   
 # " " " " "           4 4 4 4 4 4 ? 2111111		6	"	"fS!%Z Jqvc15j)*	j'z;--.Z[ 94 ?@ d5 d5 d5 d5 d5 d5 d5 d5NvS vS vS vS vS vS vS vSr *
%r/   