ó
ßüÚ\c           @  sa  d  Z  d d l m Z d d l Z d d l Z y d d l Z Wn e k
 r] d d l Z n Xd d l m	 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 d d	 l m Z d d
 l m Z d d l m Z m Z d d l m Z d Z d „  Z d d „ Z d d „ Z i  e _  d d „ Z! d d d „ Z" d d d d „ Z# d S(   sº   Utilities for integrating with IPython

These functions should probably reside in Jupyter and IPython repositories,
after which we can import them instead of having our own definitions.
iÿÿÿÿ(   t   print_functionN(   t   Popen(   t   Thread(   t   uuid4(   t   TimeoutError(   t   IOLoop(   t   Event(   t   get_ipython(   t   BlockingKernelClientt   write_connection_file(   t   jupyter_runtime_diri
   c         C  s¥  | j  | ƒ } t |  d t ƒ } | rN |  j j } |  j j } |  j j } n  xPt r y | j d t	 ƒ } Wn  t
 j k
 r t d ƒ ‚ n X| d j d ƒ | k r¯ qQ n  | d d }	 | d }
 |	 d	 k rê |
 d
 d k rPqqQ |	 d k rt t |
 d ƒ } | j |
 d ƒ qQ |	 d k rQ | rK| j | |	 |
 d | ƒq|	 d k rzt d j |
 d ƒ d t j ƒqt j j |
 d j d d ƒ ƒ qQ qQ Wd S(   si   Run a cell on a KernelClient

    Any output from the cell will be redisplayed in the local session.
    t   kernelt   timeouts"   Timeout waiting for IPython outputt   parent_headert   msg_idt   headert   msg_typet   contentt   statust   execution_statet   idlet   streamt   namet   textt   display_datat   execute_resultt   errort   parents   
t	   tracebackt   filet   datas
   text/plaint    N(   R   R   R   (   t   executet   getattrt   Falset   display_pubt
   pub_sockett   sessionR   t   Truet   get_iopub_msgt   OUTPUT_TIMEOUTt   queuet   EmptyR   t   gett   syst   writet   sendt   printt   joint   stderrt   stdout(   t   ipt   kct   cellR   t	   in_kernelt   socketR%   R   t   msgR   R   R   (    (    s9   lib/python2.7/site-packages/distributed/_ipython_utils.pyt   run_cell_remote"   s8    	
##t   workerc           sš   t  ƒ  ‰  t |  ƒ } | j d ƒ } t |    ‰ | ˆ j _ ˆ j ƒ  d ‡  ‡ f d † } ˆ | _ ˆ  j	 | d d d | ƒˆ  j	 | d d d | ƒd S(   s–   Register a %worker magic, given connection_info.

    Both a line and cell magic are registered,
    which run the given cell in a remote kernel.
    t   keyc           s)   | d k r |  } n  t ˆ  ˆ | ƒ d S(   s/   Run the current cell on a remote IPython kernelN(   t   NoneR9   (   t   lineR5   (   R3   R4   (    s9   lib/python2.7/site-packages/distributed/_ipython_utils.pyt   remoteY   s    	t
   magic_kindR=   t
   magic_nameR5   N(
   R   t   dictt   popR   R%   R;   t   start_channelsR<   t   clientt   register_magic_function(   t   connection_infoR@   t   infoR;   R>   (    (   R3   R4   s9   lib/python2.7/site-packages/distributed/_ipython_utils.pyt   register_worker_magicL   s    	
	c   	      C  s/  t  ƒ  } |  j d d ƒ } | d } | | j k rC t | ƒ ‚ n  t | j | ƒ } | sŠ t | ƒ d k r} t d ƒ ‚ n  | d } n  d j t	 t
 t | j ƒ  ƒ ƒ ƒ } | j d ƒ } | t j k rÜ t j | } n? t |   } | | j _ | j ƒ  | j d d ƒ | t j | <t | | | ƒ d S(	   st  A magic for running code on a specified remote worker

    The connection_info dict of the worker will be looked up
    as the first positional arg to the magic.
    The rest of the line (or the entire cell for a %%cell magic)
    will be passed to the remote kernel.

    Usage:

        info = e.start_ipython(worker)[worker]
        %remote info print(worker.data)
    i   i    s   I need some code to run!t   ,R;   R   i
   N(   R   t   splitR<   t   user_nst	   NameErrorRA   t   lent
   ValueErrorR0   t   mapt   strt   sortedt   itemsRB   t   remote_magict   _clientsR   R%   R;   RC   t   wait_for_readyR9   (	   R=   R5   R3   t
   split_linet	   info_nameRF   R;   t   session_keyR4   (    (    s9   lib/python2.7/site-packages/distributed/_ipython_utils.pyRS   e   s(    	
$
R>   c         C  sO   t  ƒ  } | d k r d S| j t d d d |  ƒ| j t d d d |  ƒd S(   sT   Define the parameterized %remote magic

    See remote_magic above for details.
    NR?   R=   R@   R5   (   R   R<   RE   RS   (   R@   R3   (    (    s9   lib/python2.7/site-packages/distributed/_ipython_utils.pyt   register_remote_magic”   s
    	c           s˜   t  ƒ  } | d k r$ t ƒ  j } n  t j j | | d ƒ ‰  t ˆ  |   d d d ˆ  g } | rr | j | ƒ n  t	 | ƒ t
 j ‡  f d †  ƒ } d S(   s¿   Open a QtConsole connected to a worker who has the given future

    - identify worker with who_has
    - start IPython kernel on the worker
    - start qtconsole connected to the kernel
    s   .jsont   jupytert	   qtconsoles
   --existingc             s)   y t  j ˆ  ƒ Wn t k
 r$ n Xd S(   s)   Cleanup our connection file when we exit.N(   t   ost   removet   OSError(    (   t   path(    s9   lib/python2.7/site-packages/distributed/_ipython_utils.pyt   _cleanup_connection_file²   s    N(   R
   R<   R   t   hexR\   R_   R0   R	   t   extendR   t   atexitt   register(   RF   R   t
   extra_argst   runtime_dirt   cmdR`   (    (   R_   s9   lib/python2.7/site-packages/distributed/_ipython_utils.pyt   connect_qtconsole    s    	
c   	        sA  d d l  m } | ƒ  d k	 r. t d ƒ ‚ n  d d l m } d d l m } t j	 ƒ  } t j
 ƒ  | ƒ  ‰ ˆ j ƒ  | j	 d | ƒ ‰  d ˆ  j j _ |  rª |  ˆ  _ n  d „  ‰ ˆ ˆ  _ ˆ ˆ  _ t ƒ  ‰ ‡  ‡ ‡ ‡ ‡ f d	 †  } t d
 | ƒ } t | _ | j ƒ  ˆ j d d ƒ s)t d ƒ ‚ t j
 ƒ  | j ƒ  ˆ  S(   sU  Start an IPython kernel in a thread

    Parameters
    ----------

    ip: str
        The IP address to listen on (likely the parent object's ip).
    ns: dict
        Any names that should be injected into the IPython namespace.
    log: logger instance
        Hook up IPython's logging to an existing logger instead of the default.
    iÿÿÿÿ(   R   s+   Cannot start IPython, it's already running.(   t	   ZMQIOLoop(   t   IPKernelAppt   logs   :memory:c           S  s   d  S(   N(   R<   (    (    (    s9   lib/python2.7/site-packages/distributed/_ipython_utils.pyt   noopà   s    c             s{   ˆ  j  g  ƒ ˆ ˆ  j _ ˆ ˆ  j _ ˆ  j j ƒ  t j ƒ  ˆ  j _ ˆ rc ˆ  j j j	 j
 ˆ ƒ n  ˆ j ƒ  ˆ j ƒ  d  S(   N(   t
   initializeR   t   pre_handler_hookt   post_handler_hookt   startR   t   instancet   loopt   shellRK   t   updatet   set(    (   t   appt   evtRl   t   nst   zmq_loop(    s9   lib/python2.7/site-packages/distributed/_ipython_utils.pyt   _startë   s    
t   targetR   i   s4   IPython didn't start in a reasonable amount of time.N(   t   IPythonR   R<   t   RuntimeErrort   zmq.eventloop.ioloopRi   t   ipykernel.kernelappRj   R   Rq   t   clear_instancet   installt   configt   HistoryManagert	   hist_fileR3   t   init_signalt   log_connection_infoR   R   R&   t   daemonRp   t   waitt   AssertionError(	   R3   Rx   Rk   R   Ri   Rj   t	   save_instRz   t   zmq_loop_thread(    (   Rv   Rw   Rl   Rx   Ry   s9   lib/python2.7/site-packages/distributed/_ipython_utils.pyt   start_ipython»   s2    
	
					


($   t   __doc__t
   __future__R    Rc   R\   R)   t   ImportErrort   Queuet
   subprocessR   R,   t	   threadingR   t   uuidR   t   tornado.genR   t   tornado.ioloopR   R   R|   R   t   jupyter_clientR   R	   t   jupyter_core.pathsR
   R(   R9   RH   R<   RS   RT   RY   Rh   RŒ   (    (    (    s9   lib/python2.7/site-packages/distributed/_ipython_utils.pyt   <module>   s2   	*,	