ó
_y]c           @   sÑ   d  Z  d d l Z d d l m 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 d e d	 e e f i  ƒ f d
 „  ƒ  YZ d e d	 e e f i  ƒ f d „  ƒ  YZ d g Z d S(   s;  
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.
iÿÿÿÿN(   t   LoggingConfigurable(   t   import_item(   t   string_types(   t   QtCore(   t   MetaQObjectHasTraitst   SuperQObjectt   CommManagert   NewBasec           B   s¤   e  Z d  Z d „  Z d „  Z e j e ƒ d „  ƒ Z d d d d d „ Z
 d „  Z d „  Z d „  Z e j e ƒ d „  ƒ Z d	 „  Z d
 „  Z d „  Z d „  Z RS(   s+   
    Manager for Comms in the Frontend
    c         O   sE   t  t |  ƒ j | | Ž  i  |  _ i  |  _ | rA |  j | ƒ n  d  S(   N(   t   superR   t   __init__t   commst   targetst   init_kernel_client(   t   selft   kernel_clientt   argst   kwargs(    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR	      s
    		c         C   s#   | |  _  | j j j |  j ƒ d S(   sC   
        connect the kernel, and register message handlers
        N(   R   t   iopub_channelt   message_receivedt   connectt	   _dispatch(   R   R   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR   $   s    	c         C   sC   | d d } d d d g } | | k r? t  |  | ƒ | ƒ n  d S(   s   Dispatch messagest   headert   msg_typet	   comm_opent   comm_msgt
   comm_closeN(   t   getattr(   R   t   msgR   t   handled_msg_types(    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR   +   s    c         C   sa   t  | |  j | ƒ } |  j | ƒ y | j | | | ƒ Wn! t k
 r\ |  j | ƒ ‚  n X| S(   sÉ   
        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
        (   t   CommR   t   register_commt   opent	   Exceptiont   unregister_comm(   R   t   target_namet   datat   metadatat   comm_idt   bufferst   comm(    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyt   new_comm3   s    c         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   R   (   R   R"   t   f(    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyt   register_targetD   s    c         C   s   |  j  j | ƒ S(   s5   Unregister a callable registered with register_target(   R   t   pop(   R   R"   R*   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyt   unregister_targetT   s    c         C   s9   | j  } |  j | _ | |  j | <| j j |  j ƒ | S(   s   Register a new comm(   R%   R   R
   t   sig_is_closingR   R!   (   R   R'   R%   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR   X   s
    	c         C   s*   | j  j |  j ƒ |  j j | j ƒ d S(   s-   Unregister a comm, and close its counterpart.N(   R.   t
   disconnectR!   R
   R,   R%   (   R   R'   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR!   `   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%   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyt   get_commg   s    c         C   sõ   | d } | d } | d } |  j  j | d ƒ } t | |  j | ƒ } |  j | ƒ | d k rw |  j j d | ƒ nB y | | | ƒ d SWn* t k
 r¸ |  j j d | d t	 ƒn Xy | j
 ƒ  Wn' t k
 rð |  j j d d t	 ƒn Xd S(	   s   Handler for comm_open messagest   contentR%   R"   s"   No such comm target registered: %sNs&   Exception opening comm with target: %st   exc_infosa   Could not close comm during `comm_open` failure clean-up.  The comm may not have been opened yet.(   R   t   gett   NoneR   R   R   R1   t   errorR    t   Truet   close(   R   R   R:   R%   R"   R*   R'   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR   x   s(    


	c         C   s‚   | d } | d } |  j  | ƒ } | d k r3 d S|  j | ƒ y | j | ƒ Wn* t k
 r} |  j j d | d t ƒn Xd S(   s   Handler for comm_close messagesR:   R%   Ns   Exception in comm_close for %sR;   (   R9   R=   R!   t   handle_closeR    R1   R>   R?   (   R   R   R:   R%   R'   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR   •   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;   (   R9   R=   t
   handle_msgR    R1   R>   R?   (   R   R   R:   R%   R'   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR   ¥   s    

N(   t   __name__t
   __module__t   __doc__R	   R   R   t   Slott   objectR   R=   R(   R+   R-   R   R!   R9   R   R   R   (    (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR      s   								R   c           B   s’   e  Z d  Z e j e ƒ Z d
 d
 d
 d „ Z d „  Z	 d
 d
 d
 d „ Z
 d
 d
 d
 d „ Z d
 d
 d
 d „ Z d „  Z d „  Z d „  Z d	 „  Z RS(   s   
    Comm base class
    c         C   sw   t  t |  ƒ j d | ƒ | |  _ | |  _ | d k rI t j ƒ  j } n  | |  _	 | |  _
 | |  _ |  j j |  _ d S(   s;   
        Create a new comm. Must call open to use.
        R"   N(   R   R   R	   R"   R   R=   t   uuidt   uuid1t   hexR%   t   _msg_callbackt   _close_callbackt   shell_channelt   _send_channel(   R   R"   R   R%   t   msg_callbackt   close_callback(    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR	   º   s    					c         C   s‚   | d k r i  } n  | d k r* i  } n  |  j | d <| | d <|  j j j | | d | ƒ} | rr | | d <n  |  j j | ƒ S(   s6   
        Send a message on the shell channel.
        R%   R#   R$   R&   N(   R=   R%   R   t   sessionR   RN   t   send(   R   R   R:   R#   R$   R&   R   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyt	   _send_msgÉ   s    		
c         C   s#   |  j  d i |  j d 6| | | ƒ S(   s)   Open the kernel-side version of this commR   R"   (   RS   R"   (   R   R#   R$   R&   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR   Û   s    c         C   s   |  j  d i  | | | ƒ S(   s6   Send a message to the kernel-side version of this commR   (   RS   (   R   R#   R$   R&   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyRR   á   s    c         C   s)   |  j  j |  ƒ |  j d i  | | | ƒ S(   s*   Close the kernel-side version of this commR   (   R.   t   emitRS   (   R   R#   R$   R&   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR@   æ   s    c         C   s   | |  _  d S(   s©   Register 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(   RK   (   R   t   callback(    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyt   on_msgî   s    c         C   s   | |  _  d S(   s©   Register 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(   RL   (   R   RU   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyt   on_close÷   s    c         C   s3   |  j  j d |  j | ƒ |  j r/ |  j | ƒ Sd S(   s   Handle a comm_msg messages   handle_msg[%s](%s)N(   R1   R6   R%   RK   (   R   R   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyRB     s    	c         C   s3   |  j  j d |  j | ƒ |  j r/ |  j | ƒ Sd S(   s   Handle a comm_close messages   handle_close[%s](%s)N(   R1   R6   R%   RL   (   R   R   (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyRA     s    	N(   RC   RD   RE   R   t   SignalRG   R.   R=   R	   RS   R   RR   R@   RV   RW   RB   RA   (    (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyR   ³   s   				
	(   RE   R4   t   traitlets.configR    t   ipython_genutils.importstringR   t   ipython_genutils.py3compatR   RH   t   qtconsole.qtR   t   qtconsole.utilR   R   R   R   t   __all__(    (    (    s.   lib/python2.7/site-packages/qtconsole/comms.pyt   <module>	   s   "›"Z