ó
Ýà‹]c           @   s.  d  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
 d d l 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 d
 l m Z m Z m Z m Z d d l m Z d d l  m! Z! e j" Z" d d l# m$ Z$ i  Z% e% j& e ƒ i i i d d 6d 6d f d 6Z' e' j& e d d d d ƒ ƒ e% j& e' ƒ i  Z( e( j& e ƒ e) d d d d d d d d d d d  d! d d" d# d$ d% d& d' d( ƒ 
Z* e( j& e* ƒ e e e! g Z+ d e" f d) „  ƒ  YZ, d* e, f d+ „  ƒ  YZ- d S(,   s)   A minimal application base mixin for all ZMQ based IPython frontends.

This is not a complete console app, as subprocess will not be able to receive
input, there is no real readline support, among other limitations. This is a
refactoring of what used to be the IPython/qt/console/qtconsoleapp.py
iÿÿÿÿN(   t   boolean_flag(   t   filefind(   t   Dictt   Listt   Unicodet   CUnicodet   CBoolt   Any(   t
   base_flagst   base_aliasesi   (   t   BlockingKernelClient(   t   KernelRestarter(   t   KernelManagert   tunnel_to_kernelt   find_connection_filet   connect(   t   NoSuchKernel(   t   Session(   t	   localhosts   kernel*.jsont   existingt   JupyterConsoleAppsJ   Connect to an existing kernel. If no argument specified, guess most recents   confirm-exits   JupyterConsoleApp.confirm_exitsò   Set to display confirmation dialog on exit. You can always use 'exit' or
       'quit', to force a direct exit without any confirmation. This can also
       be set in the config file by setting
       `c.JupyterConsoleApp.confirm_exit`.
    sù   Don't prompt the user when exiting. This will terminate the kernel
       if it is owned by the frontend, and leave it alive if it is external.
       This can also be set in the config file by setting
       `c.JupyterConsoleApp.confirm_exit`.
    t   ips   JupyterConsoleApp.ipt	   transports   JupyterConsoleApp.transportt   hbs   JupyterConsoleApp.hb_portt   shells   JupyterConsoleApp.shell_portt   iopubs   JupyterConsoleApp.iopub_portt   stdins   JupyterConsoleApp.stdin_ports   JupyterConsoleApp.existingt   fs!   JupyterConsoleApp.connection_filet   kernels   JupyterConsoleApp.kernel_namet   sshs   JupyterConsoleApp.sshserverc           B   s  e  Z d  Z d Z e Z e e ƒ Z e e ƒ Z e Z	 e
 Z e e ƒ  ƒ Z e d d e d d ƒZ e d d e d d ƒZ d „  Z e d d e d d ƒZ e d	 d e d d
 ƒZ e e d e d d ƒZ d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z RS(   s   jupyter-console-mixins@  
        The Jupyter Console Mixin.
        
        This class contains the common portions of console client (QtConsole,
        ZMQ-based terminal console, etc).  It is not a full console, in that
        launched terminal subprocesses will not be able to accept input.
        
        The Console using this mixing supports various extra features beyond
        the single-process Terminal IPython shell, such as connecting to
        existing kernel, via:
        
            jupyter console <appname> --existing
        
        as well as tunnel via SSH
        
    t    t   configt   helps/   The SSH server to use to connect to the kernel.s<   Path to the ssh key to use for logging in to the ssh server.c         C   s   d t  j ƒ  S(   Ns   kernel-%i.json(   t   ost   getpid(   t   self(    (    s8   lib/python2.7/site-packages/jupyter_client/consoleapp.pyt   _connection_file_default}   s    s$   Connect to an already running kernelt   pythons(   The name of the default kernel to start.s’   
        Set to display confirmation dialog on exit. You can always use 'exit' or 'quit',
        to force a direct exit without any confirmation.c         C   s   |  j  |  _ d S(   sˆ   build argv to be passed to kernel subprocess
        
        Override in subclasses if any args should be passed to the kernel
        N(   t
   extra_argst   kernel_argv(   R#   t   argv(    (    s8   lib/python2.7/site-packages/jupyter_client/consoleapp.pyt   build_kernel_argvŒ   s    c         C   s™  |  j  r y t |  j  d |  j g ƒ } Wn4 t k
 r^ |  j j d |  j  ƒ |  j d ƒ n X|  j j d | ƒ | |  _ n~ y t |  j |  j g ƒ } Wn_ t k
 rü |  j t	 j
 j |  j ƒ k rç t	 j
 j |  j |  j ƒ } n	 |  j } | |  _ n Xy" t |  j d |  j g ƒ |  _ Wn( t k
 rI|  j j d |  j ƒ d SXy |  j ƒ  Wn: t k
 r”|  j j d |  j d t ƒ|  j d ƒ n Xd S(	   s£  find the connection file, and load the info if found.
        
        The current working directory and the current profile's security
        directory will be searched for the file if it is not given by
        absolute path.
        
        When attempting to connect to an existing kernel and the `--existing`
        argument does not match an existing file, it will be interpreted as a
        fileglob, and the matching file in the current profile's security dir
        with the latest access time will be used.
        
        After this method is called, self.connection_file contains the *full path*
        to the connection file, never just its name.
        t   .s1   Could not find existing kernel connection file %si   s!   Connecting to existing kernel: %ss   Connection File not found: %sNs"   Failed to load connection file: %rt   exc_info(   R   R   t   runtime_dirt	   Exceptiont   logt   criticalt   exitt   debugt   connection_fileR!   t   patht   basenamet   joinR   t   IOErrort   load_connection_filet   errort   True(   R#   t   cf(    (    s8   lib/python2.7/site-packages/jupyter_client/consoleapp.pyt   init_connection_file“   s2    		"c         C   sË  |  j  s |  j r" |  j r" d S|  j ƒ  |  j } |  j } | d k rm |  j j d | ƒ t j	 d ƒ n  |  j r• |  j r• | |  _ t
 ƒ  } n  t d | d |  j d |  j d |  j d	 |  j ƒ } |  j j d
 | |  j f ƒ t
 ƒ  |  _ y t | |  j |  j ƒ } Wn* |  j j d d t ƒ|  j	 d ƒ n X| \ |  _ |  _ |  _ |  _ |  j } t j j | ƒ \ } } | d | |  _ |  j ƒ  |  j j d ƒ |  j j d t j j |  j ƒ ƒ d S(   s   set up ssh tunnels, if needed.Nt   tcps1   Can only use ssh tunnels with TCP sockets, not %siÿÿÿÿR   t
   shell_portt
   iopub_portt
   stdin_portt   hb_ports#   Forwarding connections to %s via %ss   Could not setup tunnelsR+   i   s   -sshs/   To connect another client via this tunnel, use:s   --existing %s(   R   t	   sshservert   sshkeyR7   R   R   R.   R8   t   sysR0   R   t   dictR=   R>   R?   R@   t   infoR   R9   R2   R!   R3   t   splitextt   write_connection_fileR4   (   R#   R   R   RE   t   newportsR:   t   roott   ext(    (    s8   lib/python2.7/site-packages/jupyter_client/consoleapp.pyt   init_sshÆ   s<    
							
c         C   sp   d } xc | sk t  t j ƒ  ƒ j d ƒ d } t j j |  j d | ƒ } t j j | ƒ sb | n d } q	 W| S(   NR   t   -iÿÿÿÿs   kernel-%s.json(	   t   strt   uuidt   uuid4t   splitR!   R3   R5   R,   t   exists(   R#   R:   t   ident(    (    s8   lib/python2.7/site-packages/jupyter_client/consoleapp.pyt   _new_connection_fileô   s    	"c         C   sŸ  |  j  r d  |  _ d  St j t j t j ƒ ys |  j d |  j d |  j d |  j	 d |  j
 d |  j d |  j d |  j d |  j d	 |  j d
 |  d |  j ƒ |  _ Wn4 t k
 rÕ |  j j d |  j ƒ |  j d ƒ n X|  j |  j _ i  } |  j j r|  j | d <n  |  j j |   t j |  j j ƒ |  j rC|  j j ƒ  n  |  j } | j
 |  _
 | j |  _ | j |  _ | j |  _ | j |  _ t j |  j j ƒ d  S(   NR   t   sessionR   R=   R>   R?   R@   R2   t   kernel_namet   parentt   data_dirs   Could not find kernel %si   t   extra_arguments(    R   t   Nonet   kernel_managert   signalt   SIGINTt   SIG_DFLt   kernel_manager_classR   RT   R   R=   R>   R?   R@   R2   RU   RW   R   R.   R/   R0   t   kernel_client_classt   client_factoryt	   ipykernelR'   t   start_kernelt   atexitt   registert   cleanup_ipc_filesRA   RG   t   cleanup_connection_file(   R#   t   kwargst   km(    (    s8   lib/python2.7/site-packages/jupyter_client/consoleapp.pyt   init_kernel_manager  sF    														c         C   s’   |  j  d  k	 r$ |  j  j ƒ  |  _ n] |  j d |  j d |  j d |  j d |  j d |  j	 d |  j
 d |  j d |  j d	 |  ƒ 	|  _ |  j j ƒ  d  S(
   NRT   R   R   R=   R>   R?   R@   R2   RV   (   RZ   RY   t   clientt   kernel_clientR_   RT   R   R   R=   R>   R?   R@   R2   t   start_channels(   R#   (    (    s8   lib/python2.7/site-packages/jupyter_client/consoleapp.pyt   init_kernel_client1  s    									c         C   s9   |  j  r d S|  j ƒ  |  j ƒ  |  j ƒ  |  j ƒ  d S(   su   
        Classes which mix this class in should call:
               JupyterConsoleApp.initialize(self,argv)
        N(   t   _dispatchingR;   RK   Ri   Rm   (   R#   R(   (    (    s8   lib/python2.7/site-packages/jupyter_client/consoleapp.pyt
   initializeE  s    	


N(    t   __name__t
   __module__t   namet   descriptiont   classesR   t   flagst   aliasesR   R^   R
   R_   R   R   R'   R9   RA   RB   R$   R   R   RU   R   t   confirm_exitRY   R)   R;   RK   RS   Ri   Rm   Ro   (    (    (    s8   lib/python2.7/site-packages/jupyter_client/consoleapp.pyR   Z   s4   							3	.		0	t   IPythonConsoleAppc           B   s   e  Z d  „  Z RS(   c         O   s*   t  j d ƒ t t |  ƒ j | | Ž  d  S(   Ns6   IPythonConsoleApp is deprecated. Use JupyterConsoleApp(   t   warningst   warnt   superRx   t   __init__(   R#   t   argsRg   (    (    s8   lib/python2.7/site-packages/jupyter_client/consoleapp.pyR|   R  s    (   Rp   Rq   R|   (    (    (    s8   lib/python2.7/site-packages/jupyter_client/consoleapp.pyRx   Q  s   (.   t   __doc__Rc   R!   R[   RC   RN   Ry   t   traitlets.config.applicationR    t   ipython_genutils.pathR   t	   traitletsR   R   R   R   R   R   t   jupyter_core.applicationR   R	   t   blockingR
   t	   restarterR   R   R   R   R   R   t
   kernelspecR   RT   R   t   ConnectionFileMixint   localinterfacesR   Ru   t   updatet	   app_flagsRv   RD   t   app_aliasesRt   R   Rx   (    (    (    s8   lib/python2.7/site-packages/jupyter_client/consoleapp.pyt   <module>   sX   ."		
	÷