ó
Û¤[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 m	 Z	 m
 Z
 m Z m Z m Z m Z m Z d e f d „  ƒ  YZ d g Z d S(	   s   Base class for a CommiÿÿÿÿN(   t   LoggingConfigurable(   t   Kernel(   t
   json_clean(   t   Instancet   Unicodet   Bytest   Boolt   Dictt   Anyt   defaultt   Commc           B   sm  e  Z d  Z e d d e ƒZ e d ƒ d „  ƒ Z e ƒ  Z	 e d ƒ d „  ƒ Z
 e e d d ƒZ e d	 ƒ Z e d d e d d
 ƒZ e ƒ  Z e d ƒ d „  ƒ Z e d d ƒ Z e d d ƒ Z e ƒ  Z e ƒ  Z e e ƒ Z d d d d d „ 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(   s7   Class for communicating between a Frontend and a Kernels   ipykernel.kernelbase.Kernelt
   allow_nonet   kernelc         C   s   t  j ƒ  r t  j ƒ  Sd  S(   N(   R   t   initializedt   instance(   t   self(    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyt   _default_kernel   s    t   comm_idc         C   s   t  j ƒ  j S(   N(   t   uuidt   uuid4t   hex(   R   (    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyt   _default_comm_id   s    t   helps#   Am I the primary or secondary Comm?t   comms8   requirejs module from
        which to load comm target.t   topicc         C   s   d |  j  j d ƒ S(   Ns   comm-%st   ascii(   R   t   encode(   R   (    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyt   _default_topic&   s    s.   data dict, if any, to be included in comm_opens/   data dict, if any, to be included in comm_closet    c         K   sj   | r | | d <n  t  t |  ƒ j |   |  j rf |  j rZ |  j d | d | d | ƒ qf t |  _ n  d  S(   Nt   target_namet   datat   metadatat   buffers(   t   superR
   t   __init__R   t   primaryt   opent   Falset   _closed(   R   R   R   R   R    t   kwargs(    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyR"   2   s    		c         K   s›   | d k r i  n | } | d k r* i  n | } t t d | d |  j |  ƒ } |  j j j |  j j | | d t | ƒ d |  j j d |  j	 d | ƒd S(   s*   Helper for sending a comm message on IOPubR   R   R   t   parentt   identR    N(
   t   NoneR   t   dictR   R   t   sessiont   sendt   iopub_sockett   _parent_headerR   (   R   t   msg_typeR   R   R    t   keyst   content(    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyt   _publish_msg=   s    !	c         C   s   |  j  ƒ  d S(   s   trigger close on gcN(   t   close(   R   (    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyt   __del__J   s    c         C   s±   | d	 k r |  j } n  t |  j d d	 ƒ } | d	 k rH t d ƒ ‚ n  | j |  ƒ y> |  j d d | d | d | d |  j d |  j ƒt	 |  _
 Wn | j |  ƒ ‚  n Xd	 S(
   s+   Open the frontend-side version of this commt   comm_managersS   Comms cannot be opened without a kernel and a comm_manager attached to that kernel.t	   comm_openR   R   R    R   t   target_moduleN(   R*   t
   _open_datat   getattrR   t   RuntimeErrort   register_commR3   R   R8   R%   R&   t   unregister_comm(   R   R   R   R    R6   (    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyR$   P   s    	
c         C   sq   |  j  r d St |  _  |  j s# d S| d k r; |  j } n  |  j d d | d | d | ƒ|  j j j |  ƒ d S(   s,   Close the frontend-side version of this commNt
   comm_closeR   R   R    (   R&   t   TrueR   R*   t   _close_dataR3   R6   R=   (   R   R   R   R    (    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyR4   e   s    			c         C   s#   |  j  d d | d | d | ƒd S(   s8   Send a message to the frontend-side version of this commt   comm_msgR   R   R    N(   R3   (   R   R   R   R    (    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyR-   v   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(   t   _close_callback(   R   t   callback(    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyt   on_close~   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(   t   _msg_callback(   R   RC   (    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyt   on_msg‡   s    c         C   s6   |  j  j d |  j | ƒ |  j r2 |  j | ƒ n  d S(   s   Handle a comm_close messages   handle_close[%s](%s)N(   t   logt   debugR   RB   (   R   t   msg(    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyt   handle_close’   s    	c         C   st   |  j  j d |  j | ƒ |  j rp |  j j } | rG | j j d ƒ n  |  j | ƒ | rp | j j d ƒ qp n  d S(   s   Handle a comm_msg messages   handle_msg[%s](%s)t   pre_executet   post_executeN(   RG   RH   R   RE   R   t   shellt   eventst   trigger(   R   RI   RM   (    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyt
   handle_msg˜   s    	N($   t   __name__t
   __module__t   __doc__R   R?   R   R	   R   R   R   R   R   R#   R   R*   R8   R   R   R   R   R9   R@   R   RE   RB   R&   R"   R3   R5   R$   R4   R-   RD   RF   RJ   RP   (    (    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyR
      s4   										(   RS   R   t   traitlets.configR    t   ipykernel.kernelbaseR   t   ipykernel.jsonutilR   t	   traitletsR   R   R   R   R   R   R	   R
   t   __all__(    (    (    s2   lib/python2.7/site-packages/ipykernel/comm/comm.pyt   <module>   s   4•