
]c           @@  sU  d  Z  d d l m Z d d l m Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l m Z d d l m Z m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z d d	 l m Z m Z d d
 l m Z d d l m  Z  d e f d     YZ! e  j" e!  d d d  Z# e d    Z$ d S(   s%   Base class to manage a running kerneli    (   t   absolute_import(   t   contextmanagerN(   t   import_itemi   (   t   is_local_ipt	   local_ips(   t   Anyt   Floatt   Instancet   Unicodet   Listt   Boolt   Typet   DottedObjectName(   t   launch_kernelt
   kernelspec(   t   ConnectionFileMixin(   t   KernelManagerABCt   KernelManagerc           B@  s  e  Z d  Z e e j  Z d   Z e d  Z	 e
 d d  Z d   Z d   Z e   Z e e j  Z d   Z d   Z e d	 d
 e d d Z e e j  Z d   Z d* Z e d    Z e  e   d
 e d d Z! d   Z" e d    Z# e   Z$ e   Z% e   Z& e' e d
 e d d Z( d   Z) d   Z* d   Z+ d d  Z, d d  Z- d   Z. d* d  Z/ d   Z0 d   Z1 d   Z2 d   Z3 e4 d  Z5 d* d  d!  Z6 e d"  Z7 e4 e4 d#  Z8 e4 e4 d$  Z9 e d%    Z: d&   Z; d'   Z< d(   Z= d)   Z> RS(+   sg   Manages a single kernel in a subprocess on this host.

    This version starts kernels with Popen.
    c         C@  s
   t  j   S(   N(   t   zmqt   Context(   t   self(    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   _context_default)   s    s,   jupyter_client.blocking.BlockingKernelClientt   klasss   jupyter_client.KernelClientc         C@  s   t  |  j  S(   N(   R   t   client_class(   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   _client_factory_default/   s    c         C@  s   t  t |   |  _ d  S(   N(   R   t   strt   client_factory(   R   t   namet   oldt   new(    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   _client_class_changed2   s    c         C@  s   t  j d |  j  S(   Nt   data_dir(   R   t   KernelSpecManagerR   (   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   _kernel_spec_manager_default;   s    c         C@  s   d  |  _ d  S(   N(   t   Nonet   _kernel_spec(   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   _kernel_spec_manager_changed>   s    g      @t   configt   helpsE   Time to wait for a kernel to terminate before killing it, in seconds.c         C@  s(   d  |  _ | d k r$ t j |  _ n  d  S(   Nt   python(   R"   R#   R   t   NATIVE_KERNEL_NAMEt   kernel_name(   R   R   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   _kernel_name_changedH   s    	c         C@  s@   |  j  d  k r9 |  j d k	 r9 |  j j |  j  |  _  n  |  j  S(   Nt    (   R#   R"   R)   t   kernel_spec_managert   get_kernel_spec(   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   kernel_specO   s    s  DEPRECATED: Use kernel_name instead.

        The Popen Command to launch the kernel.
        Override this if you have a custom kernel.
        If kernel_cmd is specified in a configuration file,
        Jupyter does not pass any arguments to the kernel,
        because it cannot make any assumptions about the
        arguments that the kernel understands. In particular,
        this means that the kernel does not receive the
        option --debug if it given on the Jupyter command line.
        c         C@  s   t  j d  d  S(   NsM   Setting kernel_cmd is deprecated, use kernel_spec to start different kernels.(   t   warningst   warn(   R   R   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   _kernel_cmd_changedc   s    c         C@  s   |  j  d d d h k S(   NR'   t   python2t   python3(   R)   (   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt	   ipykernelg   s    s,   Should we autorestart the kernel if it dies.c         C@  s   |  j    |  j   d  S(   N(   t   _close_control_sockett   cleanup_connection_file(   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   __del__u   s    
c         C@  s   d  S(   N(    (   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   start_restarter}   s    c         C@  s   d  S(   N(    (   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   stop_restarter   s    t   restartc         C@  s*   |  j  d k r d S|  j  j | |  d S(   s;   register a callback to be called when a kernel is restartedN(   t
   _restarterR"   t   add_callback(   R   t   callbackt   event(    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   add_restart_callback   s    c         C@  s*   |  j  d k r d S|  j  j | |  d S(   s=   unregister a callback to be called when a kernel is restartedN(   R;   R"   t   remove_callback(   R   R=   R>   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   remove_restart_callback   s    c         K@  sX   i  } | j  |  j d t   | j  t d |  j d |    | j  |  |  j |   S(   s3   Create a client configured to connect to our kernelt   sessiont   connection_filet   parent(   t   updatet   get_connection_infot   Truet   dictRC   R   (   R   t   kwargst   kw(    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   client   s    	
c         @  s  | p	 g  } |  j  r% |  j  | } n |  j j | } | rz | d d d t j d d t j d  h k rz t j | d <n  t d |  j d t j    |  j r |  j j	   d <n    j
 |  j  t j d	  }   f d
   } g  | D] } | j | |  ^ q S(   s/   replace templated args (e.g. {connection_file})i    R'   s   python%is   python%i.%ii   RC   t   prefixt   resource_dirs   \{([A-Za-z0-9_]+)\}c         @  s     j  |  j d  |  j    S(   s9   Get the key out of ns if it's there, otherwise no change.i   (   t   gett   group(   t   match(   t   ns(    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   from_ns   s    (   t
   kernel_cmdR.   t   argvt   syst   version_infot
   executableRH   RC   RL   RM   RE   t   _launch_argst   ret   compilet   sub(   R   t   extra_argumentst   cmdt   patRR   t   arg(    (   RQ   s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   format_kernel_cmd   s     		c         K@  s   t  | |  S(   sm   actually launch the kernel

        override in a subclass to launch kernel subprocesses differently
        (   R   (   R   RS   RJ   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   _launch_kernel   s    c         C@  s4   |  j  d  k r0 |  j d  |  _  d |  j  _ n  d  S(   Nt   controlid   (   t   _control_socketR"   t   _create_connected_sockett   linger(   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   _connect_control_socket   s    c         C@  s-   |  j  d  k r d  S|  j  j   d  |  _  d  S(   N(   Rc   R"   t   close(   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyR5      s    c         K@  s  |  j  d k r> t |  j  r> t d |  j t   f   n  |  j   | j   |  _ | j d g   } |  j	 d |  } | j d t
 j  j   } | j d d  |  j s | j |  j j p i   n  |  j j d |  |  j | d | | |  _ |  j   |  j   d S(   sz  Starts a kernel on this host in a separate process.

        If random ports (port=0) are being used, this method must be called
        before the channels are created.

        Parameters
        ----------
        `**kw` : optional
             keyword arguments that are passed down to build the kernel_cmd
             and launching the kernel (e.g. Popen kwargs).
        t   tcps   Can only launch a kernel on a local interface. This one is not: %s.Make sure that the '*_address' attributes are configured properly. Currently valid addresses are: %sR\   t   envt   PYTHONEXECUTABLEs   Starting kernel: %sN(   t	   transportR   t   ipt   RuntimeErrorR   t   write_connection_filet   copyRX   t   popR`   t   ost   environR"   RS   RE   R.   Ri   t   logt   debugRa   t   kernelR8   Rf   (   R   RJ   R\   RS   Ri   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   start_kernel   s     
	
c         C@  sK   t  d |  } |  j j d d | } |  j   |  j j |  j |  d S(   s4   Send a shutdown request via control channel
        R:   t   shutdown_requestt   contentN(   RH   RB   t   msgRf   t   sendRc   (   R   R:   Rx   Ry   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   request_shutdown  s    
g?c         C@  s   | d k r! t |  j d  } n  xa t t | |   D]# } |  j   rZ t j |  q8 Pq8 W|  j r |  j	 j
 d  |  j   n  d S(   s   Wait for kernel shutdown, then kill process if it doesn't shutdown.

        This does not send shutdown requests - use :meth:`request_shutdown`
        first.
        i    s,   Kernel is taking too long to finish, killingN(   R"   t   maxt   shutdown_wait_timet   ranget   intt   is_alivet   timet   sleept
   has_kernelRs   Rt   t   _kill_kernel(   R   t   waittimet   pollintervalt   i(    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   finish_shutdown  s    	c         C@  s+   | r |  j    n  |  j   |  j   d S(   s/   Clean up resources when the kernel is shut downN(   R6   t   cleanup_ipc_filesR5   (   R   RC   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   cleanup#  s    
c         C@  sL   |  j    | r |  j   n |  j d |  |  j   |  j d |  d S(   sX  Attempts to stop the kernel process cleanly.

        This attempts to shutdown the kernels cleanly by:

        1. Sending it a shutdown message over the shell channel.
        2. If that fails, the kernel is shutdown forcibly by sending it
           a signal.

        Parameters
        ----------
        now : bool
            Should the kernel be forcible killed *now*. This skips the
            first, nice shutdown attempt.
        restart: bool
            Will this kernel be restarted after it is shutdown. When this
            is True, connection files will not be cleaned up.
        R:   RC   N(   R9   R   R{   R   R   (   R   t   nowR:   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   shutdown_kernel+  s    

c         K@  sk   |  j  d k r t d   nI |  j d | d t  | rG |  j   n  |  j  j |  |  j |  j    d S(   sy  Restarts a kernel with the arguments that were used to launch it.

        Parameters
        ----------
        now : bool, optional
            If True, the kernel is forcefully restarted *immediately*, without
            having a chance to do any cleanup action.  Otherwise the kernel is
            given 1s to clean up before a forceful restart is issued.

            In all cases the kernel is restarted, the only difference is whether
            it is given a chance to perform a clean shutdown or not.

        newports : bool, optional
            If the old kernel was launched with random ports, this flag decides
            whether the same ports and connection file will be used again.
            If False, the same ports and connection file are used. This is
            the default. If True, new random port numbers are chosen and a
            new connection file is written. It is still possible that the newly
            chosen random port numbers happen to be the same as the old ones.

        `**kw` : optional
            Any options specified here will overwrite those used to launch the
            kernel.
        s>   Cannot restart the kernel. No previous call to 'start_kernel'.R   R:   N(   RX   R"   Rm   R   RG   t   cleanup_random_portsRE   Rv   (   R   R   t   newportsRJ   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   restart_kernelK  s    c         C@  s   |  j  d k	 S(   s/   Has a kernel been started that we are managing.N(   Ru   R"   (   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyR   r  s    c         C@  s   |  j  r y3 t t d  r. |  j t j  n |  j j   Wn_ t k
 r } t j	 d k ru | j
 d k r   q q d d l m } | j | k r   q n X|  j j   d |  _ n t d   d S(   sr   Kill the running kernel.

        This is a private method, callers should use shutdown_kernel(now=True).
        t   SIGKILLt   win32i   i    (   t   ESRCHs)   Cannot kill kernel. No kernel is running!N(   R   t   hasattrt   signalt   signal_kernelR   Ru   t   killt   OSErrorRU   t   platformt   winerrort   errnoR   t   waitR"   Rm   (   R   t   eR   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyR   w  s    		
c         C@  s   |  j  r |  j j } | d k rf t j d k rS d d l m } | |  j j  q |  j	 t
 j  q | d k r |  j j d d i  } |  j   |  j j |  j |  q n t d   d	 S(
   s   Interrupts the kernel by sending it a signal.

        Unlike ``signal_kernel``, this operation is well supported on all
        platforms.
        R   R   i   (   t   send_interruptt   messaget   interrupt_requestRx   s.   Cannot interrupt kernel. No kernel is running!N(   R   R.   t   interrupt_modeRU   R   t   win_interruptR   Ru   t   win32_interrupt_eventR   R   t   SIGINTRB   Ry   Rf   Rz   Rc   Rm   (   R   R   R   Ry   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   interrupt_kernel  s    	
c         C@  s   |  j  r~ t t d  rk t t d  rk y- t j |  j j  } t j | |  d SWqk t k
 rg qk Xn  |  j j |  n t	 d   d S(   s  Sends a signal to the process group of the kernel (this
        usually includes the kernel and any subprocesses spawned by
        the kernel).

        Note that since only SIGTERM is supported on Windows, this function is
        only useful on Unix systems.
        t   getpgidt   killpgNs+   Cannot signal kernel. No kernel is running!(
   R   R   Rq   R   Ru   t   pidR   R   t   send_signalRm   (   R   t   signumt   pgid(    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyR     s    	c         C@  s1   |  j  r) |  j j   d k r" t St Sn t Sd S(   s$   Is the kernel process still running?N(   R   Ru   t   pollR"   RG   t   False(   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyR     s
    	N(?   t   __name__t
   __module__t   __doc__R   R   R   t   contextR   R   R   R   R   R   R   R   Ru   R   R    R,   R!   R$   R   RG   R}   R   R(   R)   R*   R"   R#   t   propertyR.   R	   RS   R1   R4   RX   Rc   R;   R
   t   autorestartR7   R8   R9   R?   RA   RK   R`   Ra   Rf   R5   Rv   R   R{   R   R   R   R   R   R   R   R   R   (    (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyR   !   s`   																	
	#					,	 '	!		i<   R'   c         K@  s{   t  d |  } | j |   | j   } | j   y | j d |   Wn( t k
 rp | j   | j     n X| | f S(   s5   Start a new kernel, and return its Manager and ClientR)   t   timeout(   R   Rv   RK   t   start_channelst   wait_for_readyRm   t   stop_channelsR   (   t   startup_timeoutR)   RI   t   kmt   kc(    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   start_new_kernel  s    


c          k@  s=   t  |    \ } } z	 | VWd | j   | j d t  Xd S(   s   Context manager to create a kernel in a subprocess.

    The kernel is shut down when the context exits.

    Returns
    -------
    kernel_client: connected KernelClient instance
    NR   (   R   R   R   RG   (   RI   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt
   run_kernel  s
    
	
(%   R   t
   __future__R    t
   contextlibR   Rq   RY   R   RU   R   R/   R   t   ipython_genutils.importstringR   t   localinterfacesR   R   t	   traitletsR   R   R   R   R	   R
   R   R   t   jupyter_clientR   R   t   connectR   t
   managerabcR   R   t   registerR   R   (    (    (    s5   lib/python2.7/site-packages/jupyter_client/manager.pyt   <module>   s(   : 