B
    U[                 @   sz   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZmZ ddlmZ G d	d
 d
eZd
gZdS )zBase class to manage comms    N)LoggingConfigurable)import_item)string_types)InstanceUnicodeDictAnydefault   )Commc               @   sd   e Zd ZdZedZe Ze 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 )CommManagerzManager for Comms in the Kernelzipykernel.kernelbase.Kernelc             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)
isinstancer   r   targets)selftarget_namef r   5lib/python3.7/site-packages/ipykernel/comm/manager.pyregister_target   s    

zCommManager.register_targetc             C   s   | j |S )z5Unregister a callable registered with register_target)r   pop)r   r   r   r   r   r   unregister_target*   s    zCommManager.unregister_targetc             C   s   |j }| j|_|| j|< |S )zRegister a new comm)comm_idkernelcomms)r   commr   r   r   r   register_comm.   s    
zCommManager.register_commc             C   s   | j |j}dS )z,Unregister a comm, and close its counterpartN)r   r   r   )r   r   r   r   r   unregister_comm5   s    zCommManager.unregister_commc          	   C   sX   y
| j | S  tk
rR   | jd| | jtjrN| jdt| j 	  Y nX dS )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)
r   KeyErrorlogZwarningZisEnabledForloggingDEBUGdebuglistkeys)r   r   r   r   r   get_comm:   s    
zCommManager.get_commc       	      C   s   |d }|d }|d }| j |d}t|d|d}| | |dkrV| jd| n6y||| dS  tk
r   | jjd|d	d
 Y nX y|  W n   | jjdd	d
 Y nX dS )zHandler for comm_open messagescontentr   r   NF)r   Zprimaryr   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   getr   r   r   error	Exceptionclose)	r   streamidentmsgr%   r   r   r   r   r   r   r   	comm_openK   s(    

zCommManager.comm_openc             C   s`   |d }|d }|  |}|dkr&dS y|| W n& tk
rZ   | jjd|dd Y nX dS )zHandler for comm_msg messagesr%   r   NzException in comm_msg for %sT)r&   )r$   Z
handle_msgr)   r   r(   )r   r+   r,   r-   r%   r   r   r   r   r   comm_msgf   s    
zCommManager.comm_msgc             C   sh   |d }|d }|  |}|dkr&dS | j|= y|| W n& tk
rb   | jjd|dd Y nX dS )zHandler for comm_close messagesr%   r   NzException in comm_close for %sT)r&   )r$   r   Zhandle_closer)   r   r(   )r   r+   r,   r-   r%   r   r   r   r   r   
comm_closes   s    
zCommManager.comm_closeN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r$   r.   r/   r0   r   r   r   r   r      s   r   )r4   sysr   Ztraitlets.configr   Zipython_genutils.importstringr   Zipython_genutils.py3compatr   Z	traitletsr   r   r   r   r	   r   r   r   __all__r   r   r   r   <module>   s   p