
    
8d"              	           d Z ddlZddlmZ ddlmZ ddlZddlmZ ddl	m
Z
mZ  G d d e
d	eefi                     Z G d
 d e
d	eefi                     ZdgZdS )a;  
Based on
https://github.com/jupyter/notebook/blob/master/notebook/static/services/kernels/comm.js
https://github.com/ipython/ipykernel/blob/master/ipykernel/comm/manager.py
https://github.com/ipython/ipykernel/blob/master/ipykernel/comm/comm.py


Which are distributed under the terms of the Modified BSD License.
    N)LoggingConfigurable)import_item)QtCore)MetaQObjectHasTraitsSuperQObjectc                        e Zd ZdZ fdZd Z ej        e          d             Z		 	 ddZ
d Zd Zd	 Z ej        e          d
             ZddZd Zd Zd Z xZS )CommManagerz+
    Manager for Comms in the Frontend
    c                      t                      j        |i | i | _        i | _        |r|                     |           d S d S )N)super__init__commstargetsinit_kernel_client)selfkernel_clientargskwargs	__class__s       /lib/python3.11/site-packages/qtconsole/comms.pyr   zCommManager.__init__   sY    $)&)))
 	3##M22222	3 	3    c                 \    || _         |j        j                            | j                   dS )zC
        connect the kernel, and register message handlers
        N)r   iopub_channelmessage_receivedconnect	_dispatch)r   r   s     r   r   zCommManager.init_kernel_client#   s.     +#4<<T^LLLLLr   c                 h    |d         d         }g d}||v r t          | |          |           dS dS )zDispatch messagesheadermsg_type)	comm_opencomm_msg
comm_closeN)getattr)r   msgr   handled_msg_typess       r   r   zCommManager._dispatch*   sR     x=,CCC(((#GD(##C((((( )(r   Nc                     t          || j        |          }|                     |           	 |                    |||           n$# t          $ r |                     |            w xY w|S )z
        Create a new Comm, register it, and open its Kernel-side counterpart
        Mimics the auto-registration in `Comm.__init__` in the Jupyter Comm.

        argument comm_id is optional
        )Commr   register_commopen	Exceptionunregister_comm)r   target_namedatametadatacomm_idbufferscomms          r   new_commzCommManager.new_comm2   s     K!3W==4   	IIdHg.... 	 	 	  &&&	 s   A !A&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   r   r   r+   fs      r   register_targetzCommManager.register_targetC   s3     a 	AA$%[!!!r   c                 6    | j                             |          S )z5Unregister a callable registered with register_target)r   popr5   s      r   unregister_targetzCommManager.unregister_targetS   s    |,,,r   c                 ~    |j         }| j        |_        || j        |<   |j                            | j                   |S )zRegister a new comm)r.   r   r   sig_is_closingr   r*   )r   r0   r.   s      r   r'   zCommManager.register_commW   s?    ,!/"
7##D$8999r   c                     |j                             | j                   | j                            |j                   dS )z-Unregister a comm, and close its counterpart.N)r<   
disconnectr*   r   r9   r.   )r   r0   s     r   r*   zCommManager.unregister_comm_   s:     	&&t';<<<
t|$$$$$r   Fc                 L   	 | j         |         S # t          $ r |rY dS | j                            d|           | j                            t
          j                  rB| j                            dt          | j         	                                                     Y dS Y dS w xY w)a  Get 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.
        If the comm is closing, it might already have closed,
        so this is ignored.
        NzNo such comm: %szCurrent comms: %s)
r   KeyErrorlogwarningisEnabledForloggingDEBUGdebuglistkeys)r   r.   closings      r   get_commzCommManager.get_commf   s    	M:g&& 	M 	M 	M H/999x$$W]33 M2D9J9J4K4KLLLLLLM M M	Ms    B#A>B#"B#c                    |d         }|d         }|d         }| j                             |d          }t          || j        |          }|                     |           || j                            d|           n<	  |||           dS # t          $ r  | j                            d|d           Y nw xY w	 |                                 dS # t          $ r  | j                            d	d           Y dS w xY w)
zHandler for comm_open messagescontentr.   r+   Nz"No such comm target registered: %sz&Exception opening comm with target: %sTexc_infozaCould not close comm during `comm_open` failure clean-up.  The comm may not have been opened yet.)	r   getr&   r   r'   rA   errorr)   close)r   r#   rL   r.   r+   r6   r0   s          r   r   zCommManager.comm_open{   sF   i.)$m,L[$//K!3W==4   9HNN?MMMM;$ ; ; ;G*T  ; ; ; ; ;;
	JJLLLLL 	 	 	HNNF       	s$   >B 'B65B6:C &C:9C:c                    |d         }|d         }|                      |d          }|dS |                     |           	 |                    |           dS # t          $ r! | j                            d|d           Y dS w xY w)zHandler for comm_close messagesrL   r.   T)rI   NzException in comm_close for %srM   )rJ   r*   handle_closer)   rA   rP   r   r#   rL   r.   r0   s        r   r!   zCommManager.comm_close   s    i.)$}}Wd}33<FT"""	*c""""" 	* 	* 	*HNN;W$(  * * * * * *	*s   A 'BBc                     |d         }|d         }|                      |          }|dS 	 |                    |           dS # t          $ r! | j                            d|d           Y dS w xY w)zHandler for comm_msg messagesrL   r.   NzException in comm_msg for %sTrM   )rJ   
handle_msgr)   rA   rP   rT   s        r   r    zCommManager.comm_msg   s    i.)$}}W%%<F	*OOC      	* 	* 	*HNN97$(  * * * * * *	*s   A 'A-,A-)NNNN)F)__name__
__module____qualname____doc__r   r   r   Slotobjectr   r1   r7   r:   r'   r*   rJ   r   r!   r    __classcell__r   s   @r   r	   r	      s2        3 3 3 3 3M M M V[) ) ) 9='+   "& & & - - -   V[% % %M M M M*  :* * * * * * * * * *r   r	   NewBasec                   ~     e Zd ZdZ ej        e          Z	 	 d fd	Zd Z	ddZ
ddZddZd Zd	 Zd
 Zd Z xZS )r&   z
    Comm base class
    Nc                     t                                          |           || _        || _        |t	          j                    j        }|| _        || _        || _	        | j        j
        | _        dS )z;
        Create a new comm. Must call open to use.
        )r+   N)r   r   r+   r   uuiduuid1hexr.   _msg_callback_close_callbackshell_channel_send_channel)r   r+   r   r.   msg_callbackclose_callbackr   s         r   r   zComm.__init__   sn    
 	[111&*?jll&G)-!/=r   c                     |i }|i }| j         |d<   ||d<   | j        j                            |||          }|r||d<   | j                            |          S )z6
        Send a message on the shell channel.
        Nr.   r,   )r-   r/   )r.   r   sessionr#   rh   send)r   r   rL   r,   r-   r/   r#   s          r   	_send_msgzComm._send_msg   s|     <D?G!\	 (,,g - 2 2 	%$C	N!&&s+++r   c                 B    |                      dd| j        i|||          S )z)Open the kernel-side version of this commr   r+   )rn   r+   r   r,   r-   r/   s       r   r(   z	Comm.open   s,    ~~-)9:(G% % 	%r   c                 4    |                      di |||          S )z6Send a message to the kernel-side version of this commr    )rn   rp   s       r   rm   z	Comm.send   s#    ~~D(G5 5 	5r   c                 h    | j                             |            |                     di |||          S )z*Close the kernel-side version of this commr!   )r<   emitrn   rp   s       r   rQ   z
Comm.close   s:      &&&~~"dHg7 7 	7r   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)re   r   callbacks     r   on_msgzComm.on_msg   s     &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)rf   ru   s     r   on_closezComm.on_close   s      (r   c                     | j                             d| j        |           | j        r|                     |          S dS )zHandle a comm_msg messagezhandle_msg[%s](%s)N)rA   rF   r.   re   r   r#   s     r   rV   zComm.handle_msg  sF    +T\3??? 	+%%c***	+ 	+r   c                     | j                             d| j        |           | j        r|                     |          S dS )zHandle a comm_close messagezhandle_close[%s](%s)N)rA   rF   r.   rf   r{   s     r   rS   zComm.handle_close
  sF    -t|SAAA 	-'',,,	- 	-r   )NNN)rW   rX   rY   rZ   r   Signalr\   r<   r   rn   r(   rm   rQ   rw   ry   rV   rS   r]   r^   s   @r   r&   r&      s          #V]6**N;?37> > > > > >, , ,$% % % %5 5 5 5
7 7 7 7& & &( ( (+ + +- - - - - - -r   r&   )rZ   rD   traitlets.configr   ipython_genutils.importstringr   rb   qtpyr   qtconsole.utilr   r   r	   r&   __all__ r   r   <module>r      s     0 0 0 0 0 0 5 5 5 5 5 5        = = = = = = = =]* ]* ]* ]* ]*&&'6< < ]* ]* ]*@X- X- X- X- X-'6< < X- X- X-v /r   