
    |tf'                       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j2                  egdf   Zej2                  degdf   Z G d d      Z G d	 d
      Zd
dgZy)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	 	 	 	 	 	 	 ddZ	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 	 	 ddZ	ddZ
dd	Zdd
ZddZ xZS )r   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.
    c                   t        |   di | |r|nt        j                         j                  | _        || _        || _        || _        |r|nd| j
                  z  j                  d      | _
        |	r|	ni | _        |
r|
ni | _        d | _        d | _        d| _        | j                  r| j!                  |||       y d| _        y )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__s               X/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/comm/base_comm.pyr   zBaseComm.__init__$   s     	"6"")wtzz|/?/?&*#U)dll*B)J)J7)S
(2**5;22648<<II4(GID DL    c                    d}t        |      )Nz*publish_msg Comm method is not implemented)NotImplementedError)r!   msg_typer   r   r   keysmsgs          r$   publish_msgzBaseComm.publish_msgH   s     ;!#&&r%   c                    t        j                  t              5  | j                  d       ddd       y# 1 sw Y   yxY w)ztrigger close on gcT)deletingN)
contextlibsuppress	Exceptioncloser!   s    r$   __del__zBaseComm.__del__S   s5      + 	& JJJ%	& 	& 	&s   6?c                ,   || j                   }t        j                         }|d}t        |      |j	                  |        	 | j                  d|||| j                  | j                         d| _        y# t        $ r |j                  |         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   commget_comm_managerRuntimeErrorregister_commr+   r   r   r   r0   unregister_comm)r!   r   r   r   comm_managerr*   s         r$   r    zBaseComm.open\   s    
 <??D,,.BCs##""4(	! ,,"00   !DL 	((.	s   2A7 7Bc                    | j                   ryd| _         || j                  }| j                  d|||       |s$t        j                         j                  |        yy)z,Close the frontend-side version of this commNT
comm_closer   )r   r   r+   r6   r7   r:   )r!   r   r   r   r-   s        r$   r1   zBaseComm.closew   se     <<<##D	 	 	
 !!#33D9 r%   c                .    | j                  d|||       y)z8Send a message to the frontend-side version of this commcomm_msgr   N)r+   )r!   r   r   r   s       r$   sendzBaseComm.send   s#     		 	 	
r%   c                    || _         y)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_close   s      (r%   c                    || _         y)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   rB   s     r$   on_msgzBaseComm.on_msg   s     &r%   c                    t         j                  d| j                  |       | j                  r| j                  |       yy)zHandle a comm_close messagezhandle_close[%s](%s)N)loggerdebugr   r   )r!   r*   s     r$   handle_closezBaseComm.handle_close   s3    +T\\3?  %  r%   c                   t         j                  d| j                  |       | j                  rZddlm}  |       }|r|j                  j                  d       | j                  |       |r|j                  j                  d       yyy)zHandle a comm_msg messagezhandle_msg[%s](%s)r   )get_ipythonpre_executepost_executeN)rH   rI   r   r   IPythonrL   eventstrigger)r!   r*   rL   shells       r$   
handle_msgzBaseComm.handle_msg   sl    )4<<=+ME$$]3s#$$^4  r%   )
r6   NNNNTNNNN)r   strr   	MaybeDictr   rU   r   BuffersTyper   
str | Noner   boolr   rW   r   zbytes | Noner   rU   r   rU   r"   t.AnyreturnNone)NNN)r(   rT   r   rU   r   rU   r   rV   r)   rY   rZ   r[   rZ   r[   )r   rU   r   rU   r   rV   rZ   r[   )NNNF)
r   rU   r   rU   r   rV   r-   rX   rZ   r[   )rC   zCommCallback | NonerZ   r[   )r*   MessageTyperZ   r[   )__name__
__module____qualname____doc__r   r+   r3   r    r1   r@   rD   rF   rJ   rS   __classcell__)r#   s   @r$   r   r      s    ""#"$(" $!%"!"! "! 	"!
 "! "! "! ""! "! "! "! "! 
"!N "#	'	' 	' 		'
 	' 	' 
	'& Z^09KV	: "#:: : 	:
 : 
:2 Z^	
	
09	
KV	
		
(&&5r%   c                  X    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y)CommManagerzDDefault CommManager singleton implementation for Comms in the Kernelc                     i | _         i | _        y )N)commstargetsr2   s    r$   r   zCommManager.__init__   s    *,
68r%   c                    t        |t              rt        |      }t        j                  t
        |      | j                  |<   y)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)
isinstancerT   r   tcastCommTargetCallbackrg   r!   r   fs      r$   register_targetzCommManager.register_target   s1     aAA$%FF+=q$A[!r%   c                8    | j                   j                  |      S )z5Unregister a callable registered with register_target)rg   poprm   s      r$   unregister_targetzCommManager.unregister_target   s    ||,,r%   c                <    |j                   }|| j                  |<   |S )zRegister a new comm)r   rf   )r!   r6   r   s      r$   r9   zCommManager.register_comm   s    ,,"

7r%   c                N    | j                   j                  |j                        }y)z,Unregister a comm, and close its counterpartN)rf   rq   r   )r!   r6   s     r$   r:   zCommManager.unregister_comm   s     zz~~dll+r%   c                    	 | j                   |   S # t        $ rs t        j                  d|       t        j	                  t
        j                        r7t        j                  dt        | j                   j                                      Y y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)
rf   KeyErrorrH   warningisEnabledForloggingDEBUGrI   listr)   )r!   r   s     r$   get_commzCommManager.get_comm   sh    	::g&& 	NN-w7""7==10$tzz7H2IJ	s    A9BBc                   ddl m} |d   }|d   }|d   }| j                  j                  |d      } ||d|      }	| j	                  |	       |t
        j                  d	|       n	  ||	|       y	  |	j                          y# t        $ r t
        j                  d
|d       Y 6w xY w# t        $ r t
        j                  dd       Y y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.)	r6   r~   rg   getr9   rH   errorr0   r1   )
r!   streamidentr*   r~   r   r   r   rn   r6   s
             r$   r5   zCommManager.comm_open   s    $i.)$m,LL[$/#

 	4 9LL={Kc$
	DJJL  cE{]abc  	LLE  	s$   (	B 3B, !B)(B), CCc                    |d   }|d   }| j                  |      }|y	  |j                  |       y# t        $ r t        j	                  d|d       Y yw xY w)zHandler for comm_msg messagesr   r   NzException in comm_msg for %sTr   )r|   rS   r0   rH   r   r!   r   r   r*   r   r   r6   s          r$   r?   zCommManager.comm_msg  sc    i.)$}}W%<	QDOOC  	QLL74LP	Qs   3 !AAc                    |d   }|d   }| j                  |      }|yd| j                  |   _        | j                  |= 	  |j                  |       y# t        $ r t
        j                  d|d       Y yw xY w)zHandler for comm_close messagesr   r   NTzException in comm_close for %sr   )r|   rf   r   rJ   r0   rH   r   r   s          r$   r=   zCommManager.comm_close,  s    i.)$}}W%<&*

7#JJw	SDc" 	SLL97TLR	Ss   A !A87A8Nr\   )r   rT   rn   zCommTargetCallback | strrZ   r[   )r   rT   rn   rl   rZ   rl   )r6   r   rZ   rT   )r6   r   rZ   r[   )r   rT   rZ   zBaseComm | None)r   r   r   rT   r*   r]   rZ   r[   )r^   r_   r`   ra   r   ro   rr   r9   r:   r|   r5   r?   r=   r   r%   r$   rd   rd      s6    N9B-,
&BQSr%   rd   )ra   
__future__r   r.   ry   typingrj   r   traitlets.utils.importstringr   r6   TYPE_CHECKINGzmq.eventloop.zmqstreamr   	getLoggerrH   DictrT   Anyr]   OptionalrU   ListbytesrV   CallableCommCallbackrl   r   rd   __all__r   r%   r$   <module>r      s   
 #     4 ??1			6	"ffS!%%Z JJqvvc155j)*	jj'zz;--.ZZ[ 94 ?@ d5 d5NvS vSr *
%r%   