ó
Û¤[c           @   s©   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 d	 e f d
 „  ƒ  YZ d	 g Z d S(   s   Base class to manage commsiÿÿÿÿN(   t   LoggingConfigurable(   t   import_item(   t   string_types(   t   Instancet   Unicodet   Dictt   Anyt   defaulti   (   t   Commt   CommManagerc           B   st   e  Z d  Z e d ƒ Z e ƒ  Z e ƒ  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d	 „  Z RS(
   s   Manager for Comms in the Kernels   ipykernel.kernelbase.Kernelc         C   s/   t  | t ƒ r t | ƒ } n  | |  j | <d S(   s  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(   t
   isinstanceR   R   t   targets(   t   selft   target_namet   f(    (    s5   lib/python2.7/site-packages/ipykernel/comm/manager.pyt   register_target   s    
c         C   s   |  j  j | ƒ S(   s5   Unregister a callable registered with register_target(   R   t   pop(   R   R   R   (    (    s5   lib/python2.7/site-packages/ipykernel/comm/manager.pyt   unregister_target*   s    c         C   s&   | j  } |  j | _ | |  j | <| S(   s   Register a new comm(   t   comm_idt   kernelt   comms(   R   t   commR   (    (    s5   lib/python2.7/site-packages/ipykernel/comm/manager.pyt   register_comm.   s    	c         C   s   |  j  j | j ƒ } d S(   s,   Unregister a comm, and close its counterpartN(   R   R   R   (   R   R   (    (    s5   lib/python2.7/site-packages/ipykernel/comm/manager.pyt   unregister_comm5   s    c         C   st   y |  j  | SWn^ t k
 ro |  j j d | ƒ |  j j t j ƒ rp |  j j d t |  j  j	 ƒ  ƒ ƒ qp n Xd S(   s½   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.
        s   No such comm: %ss   Current comms: %sN(
   R   t   KeyErrort   logt   warningt   isEnabledFort   loggingt   DEBUGt   debugt   listt   keys(   R   R   (    (    s5   lib/python2.7/site-packages/ipykernel/comm/manager.pyt   get_comm:   s    c   	      C   sñ   | d } | d } | d } |  j  j | d ƒ } t d | d t d | ƒ } |  j | ƒ | d k r} |  j j d | ƒ nB y | | | ƒ d SWn* t k
 r¾ |  j j d | d t	 ƒn Xy | j
 ƒ  Wn |  j j d	 d t	 ƒn Xd S(
   s   Handler for comm_open messagest   contentR   R   t   primarys"   No such comm target registered: %sNs&   Exception opening comm with target: %st   exc_infosq   Could not close comm during `comm_open` failure
                clean-up.  The comm may not have been opened yet.(   R   t   gett   NoneR   t   FalseR   R   t   errort	   Exceptiont   Truet   close(	   R   t   streamt   identt   msgR"   R   R   R   R   (    (    s5   lib/python2.7/site-packages/ipykernel/comm/manager.pyt	   comm_openK   s(    


		c         C   su   | d } | d } |  j  | ƒ } | d k r3 d Sy | j | ƒ Wn* t k
 rp |  j j d | d t ƒn Xd S(   s   Handler for comm_msg messagesR"   R   Ns   Exception in comm_msg for %sR$   (   R!   R&   t
   handle_msgR)   R   R(   R*   (   R   R,   R-   R.   R"   R   R   (    (    s5   lib/python2.7/site-packages/ipykernel/comm/manager.pyt   comm_msgf   s    

c         C   s   | d } | d } |  j  | ƒ } | d k r3 d S|  j | =y | j | ƒ Wn* t k
 rz |  j j d | d t ƒn Xd S(   s   Handler for comm_close messagesR"   R   Ns   Exception in comm_close for %sR$   (   R!   R&   R   t   handle_closeR)   R   R(   R*   (   R   R,   R-   R.   R"   R   R   (    (    s5   lib/python2.7/site-packages/ipykernel/comm/manager.pyt
   comm_closes   s    


(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R   R   R   R!   R/   R1   R3   (    (    (    s5   lib/python2.7/site-packages/ipykernel/comm/manager.pyR	      s   									(   R6   t   sysR   t   traitlets.configR    t   ipython_genutils.importstringR   t   ipython_genutils.py3compatR   t	   traitletsR   R   R   R   R   R   R   R	   t   __all__(    (    (    s5   lib/python2.7/site-packages/ipykernel/comm/manager.pyt   <module>   s   (p