ó
Ýà‹]c           @  sA  d  Z  d d l m Z d d l m Z d d l m Z y d d l m Z Wn! e k
 rm d d l	 m Z n Xd 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 d
 d l m Z y e j Z Wn e k
 rû e j Z n Xy e Wn e k
 re Z n Xd „  Z d e f d „  ƒ  YZ d S(   se   Implements a fully blocking kernel client.

Useful for test suites and blocking terminal interfaces.
iÿÿÿÿ(   t   print_function(   t   partial(   t   getpass(   t   EmptyN(   t   Type(   t	   HBChannel(   t   KernelClienti   (   t   ZMQSocketChannelc           s…   ‡  f d †  } ˆ  j  s | Sˆ  j  j d d ƒ \ } } | j ƒ  g } d | k rb | j d ƒ n  | j d ƒ d j | ƒ | _  | S(   Nc           sS   | j  d t ƒ } | j  d d  ƒ } ˆ  |  | | Ž } | s@ | S|  j | d | ƒS(   Nt   replyt   timeout(   t   popt   Falset   Nonet   _recv_reply(   t   selft   argst   kwargsR   R	   t   msg_id(   t   meth(    s=   lib/python2.7/site-packages/jupyter_client/blocking/client.pyt   wrapped(   s    s   Returns
i   t
   Parameterss/   
        Parameters
        ----------
        sŠ  
        reply: bool (default: False)
            Whether to wait for and return reply
        timeout: float or None (default: None)
            Timeout to use when waiting for a reply

        Returns
        -------
        msg_id: str
            The msg_id of the request sent, if reply=False (default)
        reply: dict
            The reply message for this request, if reply=True
    s   
(   t   __doc__t   splitt   stript   appendt   join(   R   R   t   basedoct   _t   parts(    (   R   s=   lib/python2.7/site-packages/jupyter_client/blocking/client.pyt   reqrep'   s    		
t   BlockingKernelClientc        	   B  sû   e  Z d  Z d d „ Z e e ƒ Z e e ƒ Z e e ƒ Z	 e e
 ƒ Z d d „ Z e e j ƒ Z e e j ƒ Z e e j ƒ Z e e j ƒ Z e e j ƒ Z e e j ƒ Z e e j ƒ Z d „  Z d „  Z d „  Z e e d d e d d d d „ Z RS(   sÊ   A KernelClient with blocking APIs
    
    ``get_[channel]_msg()`` methods wait for and return messages on channels,
    raising :exc:`queue.Empty` if no message arrives within ``timeout`` seconds.
    c         C  s{  | d k r t d ƒ } n t j ƒ  | } d d l m } t |  j | ƒ s• xE |  j ƒ  s‘ t j ƒ  | k r t d | ƒ ‚ n  t j	 d ƒ qP Wn  xž t
 r5y |  j j d t
 d d ƒ } Wn t k
 rÐ n" X| d	 d
 k rò |  j | ƒ Pn  |  j ƒ  st d ƒ ‚ n  t j ƒ  | k r˜ t d | ƒ ‚ q˜ q˜ Wx> t
 rvy |  j j d t
 d d ƒ } Wq9t k
 rrPq9Xq9Wd S(   sH  Waits for a response when a client is blocked
        
        - Sets future time for timeout
        - Blocks on shell channel until a message is received
        - Exit if the kernel has died
        - If client times out before receiving a message from the kernel, send RuntimeError
        - Flush the IOPub channel
        t   infi   (   t   KernelManagers?   Kernel didn't respond to heartbeats in %d seconds and timed outgš™™™™™É?t   blockR	   i   t   msg_typet   kernel_info_replys*   Kernel died before replying to kernel_infos#   Kernel didn't respond in %d secondsN(   R   t   floatt   timet   managerR    t
   isinstancet   parentt   is_alivet   RuntimeErrort   sleept   Truet   shell_channelt   get_msgR   t   _handle_kernel_info_replyt   iopub_channel(   R   R	   t   abs_timeoutR    t   msg(    (    s=   lib/python2.7/site-packages/jupyter_client/blocking/client.pyt   wait_for_readyT   s4    			c         C  s¨   | d k	 r t ƒ  | } n  x… t r£ | d k	 rJ t d | t ƒ  ƒ } n  y |  j d | ƒ } Wn t k
 r t d ƒ ‚ n X| d j d ƒ | k rŸ q n  | SWd S(   s0   Receive and return the reply for a given requesti    R	   s   Timeout waiting for replyt   parent_headerR   N(   R   t	   monotonicR,   t   maxt   get_shell_msgR   t   TimeoutErrort   get(   R   R   R	   t   deadlineR   (    (    s=   lib/python2.7/site-packages/jupyter_client/blocking/client.pyR   Œ   s    	c         C  sÄ   | d } | j  d t ƒ r% t } n t j d k  r= t } n t } y | | d ƒ } Wn8 t k
 rp d } n" t k
 r‘ t j	 j
 d ƒ d SX|  j j ƒ  p­ |  j j ƒ  sÀ |  j | ƒ n  d S(	   s   Handle an input requestt   contentt   passwordi   t   prompts   s   
N(   i   (   R9   R   R   t   syst   version_infot	   raw_inputt   inputt   EOFErrort   KeyboardInterruptt   stdoutt   writet   stdin_channelt	   msg_readyR-   (   R   R2   R;   R=   t   raw_data(    (    s=   lib/python2.7/site-packages/jupyter_client/blocking/client.pyt   _stdin_hook_default¦   s    
			c         C  s­   | d d } | d } | d k rK t  t | d ƒ } | j | d ƒ n^ | d k rz t j j | d	 j d
 d ƒ ƒ n/ | d k r© t d j | d ƒ d t j ƒn  d S(   s/   Default hook for redisplaying plain-text outputt   headerR"   R;   t   streamt   namet   textt   display_datat   execute_resultt   datas
   text/plaint    t   errors   
t	   tracebackt   fileN(   RN   RO   (   t   getattrR>   RE   RD   R9   t   printR   t   stderr(   R   R2   R"   R;   RK   (    (    s=   lib/python2.7/site-packages/jupyter_client/blocking/client.pyt   _output_hook_default¾   s    
#c         C  sK   | d d } | d	 k r: | j  | | | d d | ƒn |  j | ƒ d S(
   s]   Output hook when running inside an IPython kernel

        adds rich output support.
        RJ   R"   RN   RO   RR   R;   R(   N(   RN   RO   RR   (   t   sendRX   (   R   t   sessiont   socketR4   R2   R"   (    (    s=   lib/python2.7/site-packages/jupyter_client/blocking/client.pyt   _output_hook_kernelÊ   s     c
         C  sý  |  j  j ƒ  s t d ƒ ‚ n  | d k r6 |  j } n  | r[ |  j j ƒ  r[ t d ƒ ‚ n  |  j | d | d | d | d | d | ƒ}
 |	 d k r  |  j }	 n  | d k rd t j	 k rd	 d
 l
 m } | ƒ  } t | d t ƒ } | rt |  j | j j | j j | j j ƒ } qqn  | d k r7|  j } n  | d k	 rSt ƒ  | } n d } t j ƒ  } |  j  j } | j | t j ƒ | r¬|  j j } | j | t j ƒ n d } xt rÄ| d k	 rêt d | t ƒ  ƒ } d | } n  t | j | ƒ ƒ } | st d ƒ ‚ n  | | k rE|  j j  d d ƒ } |	 | ƒ qµn  | | k rWqµn  |  j  j  d d ƒ } | d j! d ƒ |
 k r‹qµn  | | ƒ | d d d k rµ| d d d k rµPqµqµW| d k	 rêt d | t ƒ  ƒ } n  |  j" |
 d | ƒS(   s!  Execute code in the kernel interactively

        Output will be redisplayed, and stdin prompts will be relayed as well.
        If an IPython kernel is detected, rich output will be displayed.

        You can pass a custom output_hook callable that will be called
        with every IOPub message that is produced instead of the default redisplay.

        .. versionadded:: 5.0

        Parameters
        ----------
        code : str
            A string of code in the kernel's language.

        silent : bool, optional (default False)
            If set, the kernel will execute the code as quietly possible, and
            will force store_history to be False.

        store_history : bool, optional (default True)
            If set, the kernel will store command history.  This is forced
            to be False if silent is True.

        user_expressions : dict, optional
            A dict mapping names to expressions to be evaluated in the user's
            dict. The expression values are returned as strings formatted using
            :func:`repr`.

        allow_stdin : bool, optional (default self.allow_stdin)
            Flag for whether the kernel can send stdin requests to frontends.

            Some frontends (e.g. the Notebook) do not support stdin requests.
            If raw_input is called from code executed from such a frontend, a
            StdinNotImplementedError will be raised.

        stop_on_error: bool, optional (default True)
            Flag whether to abort the execution queue, if an exception is encountered.

        timeout: float or None (default: None)
            Timeout to use when waiting for a reply

        output_hook: callable(msg)
            Function to be called with output messages.
            If not specified, output will be redisplayed.

        stdin_hook: callable(msg)
            Function to be called with stdin_request messages.
            If not specified, input/getpass will be called.

        Returns
        -------
        reply: dict
            The reply message for this request
        s/   IOPub channel must be running to receive outputs,   stdin channel must be running to allow inputt   silentt   store_historyt   user_expressionst   allow_stdint   stop_on_errort   IPythoniÿÿÿÿ(   t   get_ipythont   kerneli    g     @@s   Timeout waiting for outputR	   R4   R   RJ   R"   t   statusR;   t   execution_statet   idleN(#   R0   R)   R*   R   R`   RF   t   executeRI   R>   t   modulesRb   Rc   RU   R   R   R\   t   display_pubRZ   t
   pub_socketR4   RX   R5   t   zmqt   PollerR[   t   registert   POLLINR,   R6   t   dictt   pollR8   R.   R9   R   (   R   t   codeR]   R^   R_   R`   Ra   R	   t   output_hookt
   stdin_hookR   Rc   t   ipt	   in_kernelR:   t
   timeout_mst   pollert   iopub_sockett   stdin_sockett   eventst   reqR2   (    (    s=   lib/python2.7/site-packages/jupyter_client/blocking/client.pyt   execute_interactiveÕ   sx    :					

N(   t   __name__t
   __module__R   R   R3   R   R   t   shell_channel_classt   iopub_channel_classt   stdin_channel_classR   t   hb_channel_classR   R   R   Rh   t   historyt   completet   inspectt   kernel_infot	   comm_infot   shutdownRI   RX   R\   R   R,   R}   (    (    (    s=   lib/python2.7/site-packages/jupyter_client/blocking/client.pyR   M   s(   2				(   R   t
   __future__R    t	   functoolsR   R   t   queueR   t   ImportErrort   QueueR>   R%   Rl   t	   traitletsR   t   jupyter_client.channelsR   t   jupyter_client.clientR   t   channelsR   R5   t   AttributeErrorR8   t	   NameErrorR*   R   R   (    (    (    s=   lib/python2.7/site-packages/jupyter_client/blocking/client.pyt   <module>   s0   
	&