ó
Ýà‹]c           @@  sÊ   d  Z  d d l m Z d d l m Z d d l m Z m Z d d l Z d d l	 m
 Z
 m Z m Z d d l m Z m Z d d	 l m Z d d
 l m Z d „  Z d e f d „  ƒ  YZ e j e ƒ d S(   s:   Base class to manage the interaction with a running kerneli    (   t   absolute_import(   t   major_protocol_version(   t   string_typest	   iteritemsN(   t   Anyt   Instancet   Typei   (   t
   ChannelABCt   HBChannelABC(   t   KernelClientABC(   t   ConnectionFileMixinc         C@  se   x^ t  |  ƒ D]P \ } } t | t ƒ s; t d | ƒ ‚ n  t | t ƒ s t d | ƒ ‚ q q Wd S(   s]   Validate that the input is a dict with string keys and values.

    Raises ValueError if not.s   key %r in dict must be a strings!   value %r in dict must be a stringN(   R   t
   isinstanceR   t
   ValueError(   t   dctt   kt   v(    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   validate_string_dict   s
    t   KernelClientc           B@  s”  e  Z d  Z e e j ƒ Z d „  Z e e	 ƒ Z
 e e	 ƒ Z e e	 ƒ Z e e ƒ Z e ƒ  Z e ƒ  Z e ƒ  Z e ƒ  Z e Z d „  Z d „  Z d „  Z e e e e d „ Z d „  Z e d „  ƒ Z d Z e d „  ƒ Z e d	 „  ƒ Z  e d
 „  ƒ Z! e d „  ƒ Z" d „  Z# e$ e d d e d „ Z% d d „ Z& d d d „ Z' e e$ d d „ Z( d „  Z) d d „ Z* d „  Z+ e$ d „ Z, d „  Z- d „  Z. RS(   s™  Communicates with a single kernel on any host via zmq channels.

    There are four channels associated with each kernel:

    * shell: for request/reply calls to the kernel.
    * iopub: for the kernel to publish results to frontends.
    * hb: for monitoring the kernel's heartbeat.
    * stdin: for frontends to reply to raw_input calls in the kernel.

    The messages that can be sent on these channels are exposed as methods of the
    client (KernelClient.execute, complete, history, etc.). These methods only
    send the message, they don't wait for a reply. To get results, use e.g.
    :meth:`get_shell_msg` to fetch messages from the shell channel.
    c         C@  s
   t  j ƒ  S(   N(   t   zmqt   Context(   t   self(    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   _context_default5   s    c         O@  s   |  j  j | | Ž  S(   s$   Get a message from the shell channel(   t   shell_channelt   get_msg(   R   t   argst   kwargs(    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   get_shell_msgK   s    c         O@  s   |  j  j | | Ž  S(   s$   Get a message from the iopub channel(   t   iopub_channelR   (   R   R   R   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   get_iopub_msgO   s    c         O@  s   |  j  j | | Ž  S(   s$   Get a message from the stdin channel(   t   stdin_channelR   (   R   R   R   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   get_stdin_msgS   s    c         C@  sx   | r  |  j  j ƒ  |  j ƒ  n  | r6 |  j j ƒ  n  | rU |  j j ƒ  t |  _ n	 t |  _ | rt |  j j ƒ  n  d S(   s}  Starts the channels for this kernel.

        This will create the channels if they do not exist and then start
        them (their activity runs in a thread). If port numbers of 0 are
        being used (random ports) then you must first call
        :meth:`start_kernel`. If the channels have been stopped and you
        call this, :class:`RuntimeError` will be raised.
        N(	   R   t   startt   kernel_infoR   R   t   Truet   allow_stdint   Falset
   hb_channel(   R   t   shellt   iopubt   stdint   hb(    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   start_channels[   s    		c         C@  s€   |  j  j ƒ  r |  j  j ƒ  n  |  j j ƒ  r> |  j j ƒ  n  |  j j ƒ  r] |  j j ƒ  n  |  j j ƒ  r| |  j j ƒ  n  d S(   sw   Stops all the running channels for this kernel.

        This stops their event loops and joins their threads.
        N(   R   t   is_alivet   stopR   R   R$   (   R   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   stop_channelsq   s    c         C@  s:   |  j  j ƒ  p9 |  j j ƒ  p9 |  j j ƒ  p9 |  j j ƒ  S(   s,   Are any of the channels created and running?(   R   R*   R   R   R$   (   R   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   channels_running   s    c         C@  sq   |  j  d k rj |  j d ƒ } |  j j d | ƒ |  j d |  j j ƒ } |  j | |  j |  j	 ƒ |  _  n  |  j  S(   s-   Get the shell channel object for this kernel.R%   s   connecting shell channel to %st   identityN(
   t   _shell_channelt   Nonet	   _make_urlt   logt   debugt   connect_shellt   sessiont   bsessiont   shell_channel_classt   ioloop(   R   t   urlt   socket(    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyR   ‡   s    c         C@  se   |  j  d k r^ |  j d ƒ } |  j j d | ƒ |  j ƒ  } |  j | |  j |  j ƒ |  _  n  |  j  S(   s-   Get the iopub channel object for this kernel.R&   s   connecting iopub channel to %sN(	   t   _iopub_channelR0   R1   R2   R3   t   connect_iopubt   iopub_channel_classR5   R8   (   R   R9   R:   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyR   “   s    c         C@  sq   |  j  d k rj |  j d ƒ } |  j j d | ƒ |  j d |  j j ƒ } |  j | |  j |  j	 ƒ |  _  n  |  j  S(   s-   Get the stdin channel object for this kernel.R'   s   connecting stdin channel to %sR.   N(
   t   _stdin_channelR0   R1   R2   R3   t   connect_stdinR5   R6   t   stdin_channel_classR8   (   R   R9   R:   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyR   Ÿ   s    c         C@  sY   |  j  d k rR |  j d ƒ } |  j j d | ƒ |  j |  j |  j | ƒ |  _  n  |  j  S(   s*   Get the hb channel object for this kernel.R(   s"   connecting heartbeat channel to %sN(   t   _hb_channelR0   R1   R2   R3   t   hb_channel_classt   contextR5   (   R   R9   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyR$   «   s    c         C@  sS   d d l  m } t |  j | ƒ r/ |  j j ƒ  S|  j d k	 rK |  j j ƒ  St Sd S(   s$   Is the kernel process still running?i   (   t   KernelManagerN(	   t   managerRD   R   t   parentR*   RA   R0   t
   is_beatingR!   (   R   RD   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyR*   ¶   s    c   	      C@  s·   | d k r i  } n  | d k r- |  j } n  t | t ƒ sO t d | ƒ ‚ n  t | ƒ t d | d | d | d | d | d | ƒ } |  j j d | ƒ } |  j	 j
 | ƒ | d	 d
 S(   s  Execute code in the kernel.

        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.

        Returns
        -------
        The msg_id of the message sent.
        s   code %r must be a stringt   codet   silentt   store_historyt   user_expressionsR"   t   stop_on_errort   execute_requestt   headert   msg_idN(   R0   R"   R   R   R   R   t   dictR5   t   msgR   t   send(	   R   RH   RI   RJ   RK   R"   RL   t   contentRQ   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   executeÈ   s    $	
c         C@  sa   | d k r t | ƒ } n  t d | d | ƒ } |  j j d | ƒ } |  j j | ƒ | d d S(   sà  Tab complete text in the kernel's namespace.

        Parameters
        ----------
        code : str
            The context in which completion is requested.
            Can be anything between a variable name and an entire cell.
        cursor_pos : int, optional
            The position of the cursor in the block of code where the completion was requested.
            Default: ``len(code)``

        Returns
        -------
        The msg_id of the message sent.
        RH   t
   cursor_post   complete_requestRN   RO   N(   R0   t   lenRP   R5   RQ   R   RR   (   R   RH   RU   RS   RQ   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   complete  s    i    c         C@  sg   | d k r t | ƒ } n  t d | d | d | ƒ } |  j j d | ƒ } |  j j | ƒ | d d S(   sœ  Get metadata information about an object in the kernel's namespace.

        It is up to the kernel to determine the appropriate object to inspect.

        Parameters
        ----------
        code : str
            The context in which info is requested.
            Can be anything between a variable name and an entire cell.
        cursor_pos : int, optional
            The position of the cursor in the block of code where the info was requested.
            Default: ``len(code)``
        detail_level : int, optional
            The level of detail for the introspection (0-2)

        Returns
        -------
        The msg_id of the message sent.
        RH   RU   t   detail_levelt   inspect_requestRN   RO   N(   R0   RW   RP   R5   RQ   R   RR   (   R   RH   RU   RY   RS   RQ   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   inspect  s    	t   rangec         K@  s~   | d k r/ | j  d d ƒ | j  d d ƒ n  t d | d | d | |  } |  j j d | ƒ } |  j j | ƒ | d	 d
 S(   sà  Get entries from the kernel's history list.

        Parameters
        ----------
        raw : bool
            If True, return the raw input.
        output : bool
            If True, then return the output as well.
        hist_access_type : str
            'range' (fill in session, start and stop params), 'tail' (fill in n)
             or 'search' (fill in pattern param).

        session : int
            For a range request, the session from which to get lines. Session
            numbers are positive integers; negative ones count back from the
            current session.
        start : int
            The first line number of a history range.
        stop : int
            The final (excluded) line number of a history range.

        n : int
            The number of lines of history to get for a tail request.

        pattern : str
            The glob-syntax pattern for a search request.

        Returns
        -------
        The ID of the message sent.
        R\   R5   i    R   t   rawt   outputt   hist_access_typet   history_requestRN   RO   (   t
   setdefaultRP   R5   RQ   R   RR   (   R   R]   R^   R_   R   RS   RQ   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   history5  s     	c         C@  s.   |  j  j d ƒ } |  j j | ƒ | d d S(   sd   Request kernel info

        Returns
        -------
        The msg_id of the message sent
        t   kernel_info_requestRN   RO   (   R5   RQ   R   RR   (   R   RQ   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyR    ^  s    c         C@  sU   | d k r i  } n t d | ƒ } |  j j d | ƒ } |  j j | ƒ | d d S(   sj   Request comm info
        
        Returns
        -------
        The msg_id of the message sent
        t   target_namet   comm_info_requestRN   RO   N(   R0   RP   R5   RQ   R   RR   (   R   Rd   RS   RQ   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt	   comm_infoi  s    	c         C@  s@   t  | d d j d ƒ d ƒ } | t k r< | |  j _ n  d S(   s~   handle kernel info reply

        sets protocol adaptation version. This might
        be run from a separate thread.
        RS   t   protocol_versiont   .i    N(   t   intt   splitR   R5   t   adapt_version(   R   RQ   Rk   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   _handle_kernel_info_replyx  s    !c         C@  s8   |  j  j d i | d 6ƒ } |  j j | ƒ | d d S(   så  Request an immediate kernel shutdown.

        Upon receipt of the (empty) reply, client code can safely assume that
        the kernel has shut down and it's safe to forcefully terminate it if
        it's still alive.

        The kernel will send the reply via a function registered with Python's
        atexit module, ensuring it's truly done as the kernel is done with all
        normal operation.

        Returns
        -------
        The msg_id of the message sent
        t   shutdown_requestt   restartRN   RO   (   R5   RQ   R   RR   (   R   Rn   RQ   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   shutdown‚  s    c         C@  s8   |  j  j d i | d 6ƒ } |  j j | ƒ | d d S(   sB   Ask the kernel whether some code is complete and ready to execute.t   is_complete_requestRH   RN   RO   (   R5   RQ   R   RR   (   R   RH   RQ   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   is_complete—  s    c         C@  s8   t  d | ƒ } |  j j d | ƒ } |  j j | ƒ d S(   s³   Send a string of raw input to the kernel.

        This should only be called in response to the kernel sending an
        ``input_request`` message on the stdin channel.
        t   valuet   input_replyN(   RP   R5   RQ   R   RR   (   R   t   stringRS   RQ   (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   input  s    N(/   t   __name__t
   __module__t   __doc__R   R   R   RC   R   R   R   R7   R=   R@   R   RB   R   R/   R;   R>   RA   R!   R"   R   R   R   R)   R,   t   propertyR-   R0   R8   R   R   R   R$   R*   R#   RT   RX   R[   Rb   R    Rf   Rl   Ro   Rq   Ru   (    (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyR   #   sF   										8)		
	(   Rx   t
   __future__R    t   jupyter_client.channelsR   t   ipython_genutils.py3compatR   R   R   t	   traitletsR   R   R   t   channelsabcR   R   t	   clientabcR	   t   connectR
   R   R   t   register(    (    (    s4   lib/python2.7/site-packages/jupyter_client/client.pyt   <module>   s   	ÿ †