ó
Ýà‹]c           @@  sç   d  Z  d d l m Z d d l Z d d l Z d d l 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 d d l m Z d d	 l m Z m Z d
 e f d „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d S(   s%   A kernel manager for multiple kernelsi    (   t   absolute_importN(   t   LoggingConfigurable(   t   import_item(   t   Instancet   Dictt   Listt   Unicodet   Anyt   DottedObjectName(   t   unicode_typei   (   t   NATIVE_KERNEL_NAMEt   KernelSpecManagert   DuplicateKernelErrorc           B@  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyR      s   c         @  s   ‡  f d †  } | S(   sD   decorator for proxying MKM.method(kernel_id) to individual KMs by IDc         @  sG   |  j  | ƒ } t | ˆ  j ƒ } | | | Ž  } ˆ  |  | | | Ž | S(   N(   t
   get_kernelt   getattrR   (   t   selft	   kernel_idt   argst   kwargst   kmt   methodt   r(   t   f(    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   wrapped   s
    (    (   R   R   (    (   R   s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   kernel_method   s    t   MultiKernelManagerc           B@  s  e  Z d  Z e e d e d d ƒZ e e d e ƒZ	 e
 d d e d d ƒZ d „  Z e d d ƒ Z d	 „  Z e d
 ƒ Z d „  Z e d ƒ Z e ƒ  Z d „  Z d „  Z d „  Z d' d „ Z e e e d „ ƒ Z e e d „ ƒ Z e d' d d „ ƒ Z e e d „ ƒ Z d „  Z  e d „ Z! e d „  ƒ Z" e d „  ƒ Z# e e d „ ƒ Z$ e d „  ƒ Z% d „  Z& d „  Z' e d d „ ƒ Z( e d d  „ ƒ Z) e d! „  ƒ Z* e d' d" „ ƒ Z+ e d' d# „ ƒ Z, e d' d$ „ ƒ Z- e d' d% „ ƒ Z. e d' d& „ ƒ Z/ RS((   s&   A class for managing multiple kernels.t   configt   helps'   The name of the default kernel to startt
   allow_nones)   jupyter_client.ioloop.IOLoopKernelManagersƒ   The kernel manager class.  This is configurable to allow
        subclassing of the KernelManager for customized behavior.
        c         C@  s   t  | ƒ |  _ d  S(   N(   R   t   kernel_manager_factory(   R   t   namet   oldt   new(    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   _kernel_manager_class_changed9   s    s)   this is kernel_manager_class after importc         C@  s   t  |  j ƒ S(   N(   R   t   kernel_manager_class(   R   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   _kernel_manager_factory_default=   s    s   zmq.Contextc         C@  s
   t  j ƒ  S(   N(   t   zmqt   Context(   R   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   _context_defaultA   s    t    c         C@  s   t  |  j j ƒ  ƒ S(   s6   Return a list of the kernel ids of the active kernels.(   t   listt   _kernelst   keys(   R   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   list_kernel_idsH   s    c         C@  s   t  |  j ƒ  ƒ S(   s%   Return the number of running kernels.(   t   lenR-   (   R   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   __len__N   s    c         C@  s   | |  j  k S(   N(   R+   (   R   R   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   __contains__R   s    c      
   K@  sÒ   | j  d t t j ƒ  ƒ ƒ } | |  k r= t d | ƒ ‚ n  | d	 k rU |  j } n  i  } |  j rt |  j | d <n  |  j d t	 j
 j |  j d | ƒ d |  d |  j d | |  } | j |   | |  j | <| S(
   sÝ   Start a new kernel.

        The caller can pick a kernel_id by passing one in as a keyword arg,
        otherwise one will be picked using a uuid.

        The kernel ID for the newly started kernel is returned.
        R   s   Kernel already exists: %st   kernel_spec_managert   connection_files   kernel-%s.jsont   parentt   logt   kernel_nameN(   t   popR	   t   uuidt   uuid4R   t   Nonet   default_kernel_nameR1   R   t   ost   patht   joint   connection_dirR4   t   start_kernelR+   (   R   R5   R   R   t   constructor_kwargsR   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyR?   U   s    		c         C@  s%   |  j  j d | ƒ |  j | ƒ d S(   s3  Shutdown a kernel by its kernel uuid.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel to shutdown.
        now : bool
            Should the kernel be shutdown forcibly using a signal.
        restart : bool
            Will the kernel be restarted?
        s   Kernel shutdown: %sN(   R4   t   infot   remove_kernel(   R   R   t   nowt   restart(    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   shutdown_kernelr   s    c         C@  s   d S(   s,   Ask a kernel to shut down by its kernel uuidN(    (   R   R   RD   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   request_shutdown‚   R)   gš™™™™™¹?c         C@  s   |  j  j d | ƒ d S(   sM   Wait for a kernel to finish shutting down, and kill it if it doesn't
        s   Kernel shutdown: %sN(   R4   RA   (   R   R   t   waittimet   pollinterval(    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   finish_shutdown†   s    c         C@  s   d S(   s   Clean up a kernel's resourcesN(    (   R   R   R2   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   cleanupŒ   R)   c         C@  s   |  j  j | ƒ S(   sÉ   remove a kernel from our mapping.

        Mainly so that a kernel can be removed if it is already dead,
        without having to call shutdown_kernel.

        The kernel object is returned.
        (   R+   R6   (   R   R   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyRB      s    c         C@  sf   |  j  ƒ  } x | D] } |  j | ƒ q Wx5 | D]- } |  j | ƒ |  j | ƒ |  j | ƒ q1 Wd S(   s   Shutdown all kernels.N(   R-   RF   RI   RJ   RB   (   R   RC   t   kidst   kid(    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   shutdown_allš   s    c         C@  s   |  j  j d | ƒ d S(   s¢   Interrupt (SIGINT) the kernel by its uuid.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel to interrupt.
        s   Kernel interrupted: %sN(   R4   RA   (   R   R   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   interrupt_kernel¤   s    	c         C@  s   |  j  j d | | f ƒ d S(   s  Sends a signal to the kernel by its uuid.

        Note that since only SIGTERM is supported on Windows, this function
        is only useful on Unix systems.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel to signal.
        s   Signaled Kernel %s with %sN(   R4   RA   (   R   R   t   signum(    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   signal_kernel¯   s    c         C@  s   |  j  j d | ƒ d S(   s­   Restart a kernel by its uuid, keeping the same ports.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel to interrupt.
        s   Kernel restarted: %sN(   R4   RA   (   R   R   RC   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   restart_kernel½   s    	c         C@  s   d S(   sì   Is the kernel alive.

        This calls KernelManager.is_alive() which calls Popen.poll on the
        actual kernel subprocess.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel.
        N(    (   R   R   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   is_aliveÈ   R)   c         C@  s#   | |  k r t  d | ƒ ‚ n  d S(   s   check that a kernel id is valids   Kernel with id not found: %sN(   t   KeyError(   R   R   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   _check_kernel_idÕ   s    c         C@  s   |  j  | ƒ |  j | S(   s¨   Get the single KernelManager object for a kernel by its uuid.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel.
        (   RT   R+   (   R   R   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyR   Ú   s    RD   c         C@  s   d S(   s&   add a callback for the KernelRestarterN(    (   R   R   t   callbackt   event(    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   add_restart_callbackå   R)   c         C@  s   d S(   s)   remove a callback for the KernelRestarterN(    (   R   R   RU   RV   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   remove_restart_callbacké   R)   c         C@  s   d S(   sË  Return a dictionary of connection data for a kernel.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel.

        Returns
        =======
        connection_dict : dict
            A dict of the information needed to connect to a kernel.
            This includes the ip address and the integer port
            numbers of the different channels (stdin_port, iopub_port,
            shell_port, hb_port).
        N(    (   R   R   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   get_connection_infoí   R)   c         C@  s   d S(   s6  Return a zmq Socket connected to the iopub channel.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel
        identity : bytes (optional)
            The zmq identity of the socket

        Returns
        =======
        stream : zmq Socket or ZMQStream
        N(    (   R   R   t   identity(    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   connect_iopubÿ   R)   c         C@  s   d S(   s6  Return a zmq Socket connected to the shell channel.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel
        identity : bytes (optional)
            The zmq identity of the socket

        Returns
        =======
        stream : zmq Socket or ZMQStream
        N(    (   R   R   RZ   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   connect_shell  R)   c         C@  s   d S(   s8  Return a zmq Socket connected to the control channel.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel
        identity : bytes (optional)
            The zmq identity of the socket

        Returns
        =======
        stream : zmq Socket or ZMQStream
        N(    (   R   R   RZ   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   connect_control  R)   c         C@  s   d S(   s6  Return a zmq Socket connected to the stdin channel.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel
        identity : bytes (optional)
            The zmq identity of the socket

        Returns
        =======
        stream : zmq Socket or ZMQStream
        N(    (   R   R   RZ   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   connect_stdin/  R)   c         C@  s   d S(   s3  Return a zmq Socket connected to the hb channel.

        Parameters
        ==========
        kernel_id : uuid
            The id of the kernel
        identity : bytes (optional)
            The zmq identity of the socket

        Returns
        =======
        stream : zmq Socket or ZMQStream
        N(    (   R   R   RZ   (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt
   connect_hb?  R)   N(0   R   R   t   __doc__R   R
   t   TrueR:   R   R   R1   R   R$   R#   R   R   R%   t   contextR(   R>   R   R+   R-   R/   R0   R9   R?   R   t   FalseRE   RF   RI   RJ   RB   RM   RN   RP   RQ   RR   RT   R   RW   RX   RY   R[   R\   R]   R^   R_   (    (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyR   *   sd   										


		(   R`   t
   __future__R    R;   R7   R&   t   traitlets.config.configurableR   t   ipython_genutils.importstringR   t	   traitletsR   R   R   R   R   R   t   ipython_genutils.py3compatR	   t
   kernelspecR
   R   t	   ExceptionR   R   R   (    (    (    s@   lib/python2.7/site-packages/jupyter_client/multikernelmanager.pyt   <module>   s   .	