
-[c           @  s=  d  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	 m
 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 d d l m Z d d l m Z m Z m 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( m) Z) d d l* m+ Z+ d d l, m- Z- d d l. m/ Z/ d d l0 m1 Z1 d d l2 m3 Z3 d d l4 m5 Z5 d d l6 m7 Z7 d d l8 m9 Z9 m: Z: d d l; m< Z< m= Z= m> Z> d d l? m@ Z@ eA e  ZB eB jC i d d 6d d 6d d 6d d 6d d  6d! d" 6d# d$ 6d% d& 6 eA e  ZD eD jC i i i eE d' 6d( 6d) f d* 6i i eE d+ 6d( 6d, f d- 6i i d. d/ 6d( 6d0 f d/ 6 eB jC e  eD jC e  eB jC e>  eD jC e=  d1 ZF d( e e e1 f d2     YZG eG jH ZI d3   ZJ eK d4 k r9eJ   n  d S(5   s%   An Application for launching a kerneli(   t   print_functionN(   t   ioloop(   t	   ZMQStream(   t   BaseIPythonApplicationt
   base_flagst   base_aliasest   catch_config_error(   t
   ProfileDir(   t   InteractiveShellAppt   shell_flagst   shell_aliases(   t   io(   t   filefindt   ensure_dir_exists(	   t   Anyt   Instancet   Dictt   Unicodet   Integert   Boolt   DottedObjectNamet   Typet   default(   t   import_item(   t   jupyter_runtime_dir(   t   write_connection_file(   t   ConnectionFileMixini   (   t   IOPubThread(   t	   Heartbeat(   t   IPythonKernel(   t   ParentPollerUnixt   ParentPollerWindows(   t   Sessiont   session_flagst   session_aliases(   t   ZMQInteractiveShells   IPKernelApp.ipt   ips   IPKernelApp.hb_portt   hbs   IPKernelApp.shell_portt   shells   IPKernelApp.iopub_portt   iopubs   IPKernelApp.stdin_portt   stdins   IPKernelApp.control_portt   controls   IPKernelApp.connection_filet   fs   IPKernelApp.transportt	   transportt	   no_stdoutt   IPKernelApps"   redirect stdout to the null devices	   no-stdoutt	   no_stderrs"   redirect stderr to the null devices	   no-stderrt   autot   pylabs^   Pre-load matplotlib and numpy for interactive use with
        the default matplotlib backend.s#  NOTE: When using the `ipython kernel` entry point, Ctrl-C will not work.

To exit, you will have to explicitly quit this process, by either sending
"quit" from a client, or using Ctrl-\ in UNIX-like environments.

To read more about this, see https://github.com/ipython/ipython/issues/2049

c           B  s  e  Z d  Z e e  Z e e  Z e e	 e
 e g Z e d d d d d j d e  Z e   Z e   Z e e d e Z e   Z i d0 d
 6Z e   Z e d  d    Z e d    Z e e  d d j d e  Z! e e  d d j d e  Z" e e d d j d e  Z# e$ d d d j d e  Z% e$ d d d j d e  Z& e' e( e) j* j+ d  prd  d d j d e  Z, e' e( e) j* j+ d  pd  d d j d e  Z- d   Z. d   Z/ d   Z0 d   Z1 d   Z2 d   Z3 d    Z4 d!   Z5 d"   Z6 d#   Z7 d$   Z8 d%   Z9 d&   Z: d'   Z; d(   Z< d)   Z= d*   Z> d+   Z? d,   Z@ d-   ZA eB d1 d.   ZD d/   ZE RS(2   s   ipython-kernels    ipykernel.ipkernel.IPythonKernelt   klasss   ipykernel.kernelbase.Kernelt   helps   The Kernel subclass to be used.

    This should allow easy re-use of the IPKernelApp entry point
    to configure and launch kernels other than IPython's own.
    t   configt
   allow_nones0   ipykernel.kernelspec.InstallIPythonKernelSpecApps   Install the IPython kernelt   installt   connection_dirc         C  s   t    S(   N(   R   (   t   self(    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   _default_connection_dir   s    c         C  sB   t  j j |  j  |  j k r7 t  j j |  j |  j  S|  j Sd  S(   N(   t   ost   patht   basenamet   connection_filet   joinR6   (   R7   (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   abs_connection_file   s    s"   redirect stdout to the null devices"   redirect stderr to the null devices(   Only send stdout/stderr to output streams   ipykernel.iostream.OutStreams*   The importstring for the OutStream factorys$   ipykernel.displayhook.ZMQDisplayHooks,   The importstring for the DisplayHook factoryt   JPY_PARENT_PIDi    s   kill this process if its parent dies.  On Windows, the argument
        specifies the HANDLE of the parent process, otherwise it is simply boolean.
        t   JPY_INTERRUPT_EVENTsY   ONLY USED ON WINDOWS
        Interrupt this process when the parent is signaled.
        c         C  s   |  j  t _  d  S(   N(   t
   excepthookt   sys(   R7   (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   init_crash_handler   s    c         C  s    t  j | | | d t j d  S(   Nt   file(   t	   tracebackt   print_exceptionRB   t
   __stderr__(   R7   t   etypet   evaluet   tb(    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyRA      s    c         C  sj   t  j d k r? |  j s! |  j rf t |  j |  j  |  _ qf n' |  j rf |  j d k rf t   |  _ n  d  S(   Nt   win32i   (   RB   t   platformt	   interruptt   parent_handleR   t   pollerR   (   R7   (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   init_poller   s
    c         C  s   d |  j  |  j f } |  j  d k r` | d k rC | j |  } q | j d |  j | f  n |  j  d k r | d k r d } d |  j | f } xI t j j |  r | d } d |  j | f } q Wn d |  j | f } | j d |  n  | S(	   Ns   %s://%st   tcpi    s   tcp://%s:%it   ipci   s   %s-%is   ipc://%s(   R+   R$   t   bind_to_random_portt   bindR9   R:   t   exists(   R7   t   st   portt   ifaceR:   (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   _bind_socket   s    
c         C  su   |  j  } |  j j d |  t | d |  j d |  j j d |  j d |  j d |  j	 d |  j
 d |  j d	 |  j d
 S(   s"   write connection info to JSON files   Writing connection file: %sR$   t   keyR+   t
   shell_portt
   stdin_portt   hb_portt
   iopub_portt   control_portN(   R>   t   logt   debugR   R$   t   sessionRZ   R+   R[   R\   R]   R^   R_   (   R7   t   cf(    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyR      s
    	'c         C  sU   |  j  } |  j j d |  y t j |  Wn t t f k
 rF n X|  j   d  S(   Ns   Cleaning up connection file: %s(   R>   R`   Ra   R9   t   removet   IOErrort   OSErrort   cleanup_ipc_files(   R7   Rc   (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   cleanup_connection_file   s    	c         C  s   |  j  s d t j   |  _  n  y" t |  j  d |  j g  |  _  WnT t k
 r |  j j d |  j   t t j	 j
 |  j  d  t j |  j  d  SXy |  j   Wn: t k
 r |  j j d |  j  d t |  j d  n Xd  S(   Ns   kernel-%s.jsont   .s   Connection file not found: %si  s"   Failed to load connection file: %rt   exc_infoi   (   R<   R9   t   getpidR   R6   Re   R`   Ra   R   R:   t   dirnameR>   t   atexitt   registerRh   t   load_connection_filet	   Exceptiont   errort   Truet   exit(   R7   (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   init_connection_file   s    	"c         C  sd  |  j  j d t j    t j j   } | j t j  |  _	 d |  j	 _
 |  j |  j	 |  j  |  _ |  j  j d |  j  | j t j  |  _ d |  j _
 |  j |  j |  j  |  _ |  j  j d |  j  | j t j  |  _ d |  j _
 |  j |  j |  j  |  _ |  j  j d |  j  t t d  rSd |  j	 _ |  j _ |  j _ n  |  j |  d  S(   Ns   Starting the kernel at pid: %ii  s    shell ROUTER Channel on port: %is    stdin ROUTER Channel on port: %is"   control ROUTER Channel on port: %it   ROUTER_HANDOVERi   (   R`   t   infoR9   Rk   t   zmqt   Contextt   instancet   sockett   ROUTERt   shell_sockett   lingerRY   R[   Ra   t   stdin_socketR\   t   control_socketR_   t   hasattrt   router_handovert
   init_iopub(   R7   t   context(    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   init_sockets   s"    #c         C  s   | j  t j  |  _ d |  j _ |  j |  j |  j  |  _ |  j j d |  j  |  j	   t
 |  j d t |  _ |  j j   |  j j |  _ d  S(   Ni  s   iopub PUB Channel on port: %it   pipe(   Rz   Rw   t   PUBt   iopub_socketR}   RY   R^   R`   Ra   t   configure_tornado_loggerR   Rr   t   iopub_threadt   startt   background_socket(   R7   R   (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyR     s    
c         C  sg   t  j   } t | |  j |  j |  j f  |  _ |  j j |  _ |  j j	 d |  j  |  j j
   d S(   s   start the heart beatings!   Heartbeat REP Channel on port: %iN(   Rw   Rx   R   R+   R$   R]   t	   heartbeatRW   R`   Ra   R   (   R7   t   hb_ctx(    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   init_heartbeat  s
    $c         C  s  t  j j |  j  } | |  j k sB t  j j |  j  |  j k rK | } n	 |  j } d d | g } x | D] } |  j j |  qk W|  j r |  j	 t
 j k  r t j t  x | D] } t j |  q Wn  t d |  j d |  j d |  j d |  j d |  j  |  _ d S(	   s(   display connection info, and store portss.   To connect another client to this kernel, use:s       --existing %sR&   R'   R(   R%   R)   N(   R9   R:   R;   R<   Rl   R6   R`   Rv   RN   t	   log_levelt   loggingt   CRITICALR   t	   raw_printt   _ctrl_c_messaget   dictR[   R^   R\   R]   R_   t   ports(   R7   R;   t   tailt   linest   line(    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   log_connection_info  s     		c         C  sc   |  j  s |  j r_ t t j d  } |  j  r@ | t _ t _ n  |  j r_ | t _ t _	 q_ n  d S(   s/   redirects stdout/stderr to devnull if necessaryt   wN(
   R,   R.   t   openR9   t   devnullRB   t   stdoutt
   __stdout__t   stderrRG   (   R7   t	   blackhole(    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   init_blackhole9  s    		c         C  s$  |  j  r t t |  j    } t j d k	 r= t j j   n  |  j rL d n t j } |  j rd d n t j	 } | |  j
 |  j d d | t _ t j d k	 r t j j   n  | |  j
 |  j d d | t _ n  |  j rt t |  j   } | |  j
 |  j  |  _ |  j t _ n  |  j   d S(   s.   Redirect input streams and set a display hook.u   stdoutt   echou   stderrN(   t   outstream_classR   t   strRB   R   t   Nonet   flusht   quietR   RG   Rb   R   R   t   displayhook_classR   t   displayhookt   patch_io(   R7   t   outstream_factoryt   e_stdoutt   e_stderrt   displayhook_factory(    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   init_ioB  s&    		c           s   y d d l  } Wn t k
 r# nj X| j   t j t   f d  } | | _ t | d  r | j  t j t t  f d  } | | _ n  d S(   sA   Patch important libraries that can't handle sys.stdout forwardingiNc           s     d |  d | |  S(   NRD   t   all_threads(    (   RD   R   t   kwargs(   t   faulthandler_enable(    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   enableh  s    Rn   c      	     s     |  d | d | d | | S(   NRD   R   t   chain(    (   t   signumRD   R   R   R   (   t   faulthandler_register(    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyRn   o  s    (	   t   faulthandlert   ImportErrorR   RB   RG   Rr   R   Rn   t   False(   R7   R   R   Rn   (    (   R   R   s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyR   [  s    			c         C  s   t  j  t  j t  j  d  S(   N(   t   signalt   SIGINTt   SIG_IGN(   R7   (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   init_signalt  s    c           s   t  |  j  } t  |  j  } |  j j } | d |  d |  j d | | g d |  j d |  j d |  j d |  j	 d |  j
 d	 |  j  	    j d
   |  j j   D    |  _   f d   |  j _ d S(   s   Create the Kernel object itselft   parentRb   t   shell_streamsR   R   R~   R`   t   profile_dirt   user_nsc         S  s#   i  |  ] \ } } | | d   q S(   t   _port(    (   t   .0t   nameRW   (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pys
   <dictcomp>  s   	 c             s     j  S(   N(   t   execution_count(    (   t   kernel(    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   <lambda>  s    N(   R   R|   R   t   kernel_classRy   Rb   R   R   R~   R`   R   R   t   record_portsR   t   itemsR   R   t   get_execution_count(   R7   t   shell_streamt   control_streamt   kernel_factory(    (   R   s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   init_kernelw  s    						c           sn   t  j j d  s" d t  j d <n  |  j     j } z)   f d   } |   _ t j |   Wd |   _ Xd S(   s=   Enable GUI event loop integration, taking pylab into account.t
   MPLBACKENDs'   module://ipykernel.pylab.backend_inlinec           s6   t  d d t j t    j j |  d t j d  S(   Ns-   GUI event loop or pylab initialization failedRD   (   t   printRB   R   t   InteractiveTBt   stb2text(   RH   RI   t   stb(   R&   (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   print_tb  s    	
N(   R9   t   environt   getR&   t   _showtracebackR   t   init_gui_pylab(   R7   R   R   (    (   R&   s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyR     s    			c         C  s;   t  |  j d d   |  _ |  j r7 |  j j j |   n  d  S(   NR&   (   t   getattrR   R   R&   t   configurablest   append(   R7   (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt
   init_shell  s    	c         C  sl   t  t |   j   |  j j } d | j k rh y | j d  Wqh t k
 rd } |  j j	 d  qh Xn  d  S(   Nt
   ipywidgetssA   ipywidgets package not installed.  Widgets will not be available.(
   t   superR-   t   init_extensionsR&   t   extension_managert   loadedt   load_extensionR   R`   Ra   (   R7   t   extension_mant   e(    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyR     s    c         C  sK   t  j d  } t  j   } t  j t  j  } | j |  | j |  d S(   sF   Configure the tornado logging.Logger.

            Must set up the tornado logger or else tornado will call
            basicConfig for the root logger which makes the root logger
            go to the real sys.stderr instead of the capture streams.
            This function mimics the setup of logging.basicConfig.
        t   tornadoN(   R   t	   getLoggert   StreamHandlert	   Formattert   BASIC_FORMATt   setFormattert
   addHandler(   R7   t   loggert   handlert	   formatter(    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyR     s
    c         C  s   t  t |   j |  |  j d  k	 r) d  St j   |  j   |  j   |  j	   |  j
   |  j   |  j   |  j   |  j   |  j   |  j   |  j   |  j   |  j r |  j   |  j   |  j   n  t j j   t j j   d  S(   N(   R   R-   t
   initializet   subappR   t
   zmq_ioloopR5   R   Rt   RP   R   R   R   R   R   R   R   t	   init_pathR   R&   R   R   t	   init_codeRB   R   R   R   (   R7   t   argv(    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyR     s,    












	

c         C  s   |  j  d  k	 r |  j  j   S|  j d  k	 r; |  j j   n  |  j j   t j j   |  _ y |  j j   Wn t	 k
 r~ n Xd  S(   N(
   R   R   R   RO   R   R   t   IOLoopt   currentt   io_loopt   KeyboardInterrupt(   R7   (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyR     s    (   s0   ipykernel.kernelspec.InstallIPythonKernelSpecApps   Install the IPython kernelN(F   t   __name__t
   __module__R   R   t   kernel_aliasest   aliasest   kernel_flagst   flagsR   R#   R   R    t   classesR   t   tagRr   R   R   R   RO   R   R   R   R   t   subcommandsR   R6   R   R8   t   propertyR>   R   R   R,   R.   R   R   R   R   R   t   intR9   R   R   RN   RM   RC   RA   RP   RY   R   Rh   Rt   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyR-   c   sf   				 
			!!			
			
		 		
											c          C  s$   t  j   }  |  j   |  j   d S(   s!   Run an IPKernel as an applicationN(   R-   Ry   R   R   (   t   app(    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   main  s    
t   __main__(L   t   __doc__t
   __future__R    Rm   R9   RB   R   RE   R   R   R   Rw   t   zmq.eventloopR   t   zmq.eventloop.zmqstreamR   t   IPython.core.applicationR   R   R   R   t   IPython.core.profiledirR   t   IPython.core.shellappR   R	   R
   t   IPython.utilsR   t   ipython_genutils.pathR   R   t	   traitletsR   R   R   R   R   R   R   R   R   t   ipython_genutils.importstringR   t   jupyter_core.pathsR   t   jupyter_clientR   t   jupyter_client.connectR   t   iostreamR   R   R   t   ipkernelR   t   parentpollerR   R   t   jupyter_client.sessionR    R!   R"   t   zmqshellR#   R   R   t   updateR   Rr   R   R-   t   launch_instancet   launch_new_instanceR  R   (    (    (    s2   lib/python2.7/site-packages/ipykernel/kernelapp.pyt   <module>   sr   "@		


	 		