ó
Ýà‹]c           @   sÿ   d  Z  d d l Z d d l Z d d l m Z d „  Z e j d e j ƒ Z e j d e j ƒ Z	 e j d e j
 e j Bƒ Z d „  Z d	 e f d
 „  ƒ  YZ d „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d d „ Z i e ƒ  d 6e ƒ  d 6Z d S(   s'   Adapters for Jupyter msg spec versions.iÿÿÿÿN(   t   protocol_version_infoc         C   sT   |  s
 d Sx= |  j  t ƒ D], } t | ƒ } | | k rE | | 8} q Pq W| | f S(   s¡   Turn a multiline code block and cursor position into a single line
    and new cursor position.

    For adapting ``complete_`` and ``object_info_request``.
    t    i    (   R   i    (   t
   splitlinest   Truet   len(   t   codet
   cursor_post   linet   n(    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   code_to_line   s    s   \([^\(\)]+\)s	   \([^\(]*$s   [a-z_][0-9a-z._]*c         C   s   t  |  | ƒ \ } } | } t j d | ƒ } x( | | k rW | } t j d | ƒ } q0 Wt j d | ƒ } t j | ƒ } | r‡ | d Sd Sd S(   sx   Reimplement token-finding logic from IPython 2.x javascript
    
    for adapting object_info_request from v5 to v4
    u    R   iÿÿÿÿN(   R	   t   _match_brackett   subt   _end_brackett   _identifiert   findall(   R   R   R   t   _t   oldlinet   matches(    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   extract_oname_v4    s    t   Adapterc           B   sA   e  Z d  Z i  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   sa   Base class for adapting messages

    Override message_type(msg) methods to create adapters.
    c         C   s   | S(   N(    (   t   selft   msg(    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   update_header?   s    c         C   s   | S(   N(    (   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   update_metadataB   s    c         C   sC   | d } | d } | |  j  k r? |  j  | | d <| d <n  | S(   Nt   headert   msg_type(   t   msg_type_map(   R   R   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   update_msg_typeE   s
    

c         C   s   | S(   se   This will be called *instead of* the regular handler

        on any reply with status != ok
        (    (   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   handle_reply_status_errorL   s    c         C   s–   |  j  | ƒ } |  j | ƒ } |  j | ƒ } | d } t |  | d d  ƒ } | d  k r] | S| d j d d  ƒ d d h k rŒ |  j | ƒ S| | ƒ S(   NR   R   t   contentt   statust   errort   aborted(   R   R   R   t   getattrt   Nonet   getR   (   R   R   R   t   handler(    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   __call__S   s    
"(	   t   __name__t
   __module__t   __doc__R   R   R   R   R   R%   (    (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyR   7   s   				c         C   sO   g  } xB |  j  d ƒ D]1 } y | j t | ƒ ƒ Wq t k
 rF q Xq W| S(   sR   convert a version string to a list of ints

    non-int segments are excluded
    t   .(   t   splitt   appendt   intt
   ValueError(   t   versiont   vt   part(    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   _version_str_to_listb   s    t   V5toV4c           B   s    e  Z d  Z d Z i d d 6d d 6d d 6d d	 6d
 d 6Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s   Adapt msg protocol v5 to v4s   4.1t   pyoutt   execute_resultt   pyint   execute_inputt   pyerrR   t   object_info_requestt   inspect_requestt   object_info_replyt   inspect_replyc         C   s,   | d j  d d  ƒ | d j  d d  ƒ | S(   NR   R.   t   parent_header(   t   popR"   (   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyR   |   s    c         C   sê   i  } | d } x1 d D]) } | | k r t  | | ƒ | | <q q W| j d d ƒ d k r d | k r t  | d ƒ | d <n  | j d	 i  ƒ } | j d
 d ƒ } | j d | ƒ d | k rÜ | j d t  | d ƒ ƒ n  | | d <| S(   NR   t   language_versiont   protocol_versiont   implementationR   t   ipythont   implementation_versiont   ipython_versiont   language_infot   namet   languageR.   (   R>   R?   (   R1   R#   t
   setdefault(   R   R   t   v4cR   t   keyRD   RF   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   kernel_info_replyƒ   s    

c         C   s   | d } | j  d g  ƒ | S(   NR   t   user_variables(   RG   (   R   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   execute_request”   s    
c         C   s   | d } | j  d i  ƒ | S(   NR   RK   (   RG   (   R   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   execute_reply™   s    
c         C   sm   | d } | d } | d } t  | | ƒ \ } } i  } | d <d | d <| | d <d  | d <| | d <| S(   NR   R   R   R   t   textR   t   block(   R	   R"   (   R   R   R   R   R   R   t   new_content(    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   complete_requestŸ   s    






c         C   s\   | d } | j  d ƒ } | j  d ƒ } | | } | d d |  | d <| j  d d  ƒ | S(   NR   t   cursor_startt
   cursor_endR   i    t   matched_textt   metadata(   R=   R"   (   R   R   R   RR   RS   t	   match_len(    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   complete_reply¬   s    

c         C   sf   | d } | d } | d } t  | | ƒ \ } } i  } | d <t | | ƒ | d <| d | d <| S(   NR   R   R   t   onamet   detail_level(   R	   R   (   R   R   R   R   R   R   R   RP   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyR8   µ   s    


c         C   s   i t  d 6d d 6| d <| S(   s1   inspect_reply can't be easily backward compatiblet   foundt   unknownRX   R   (   t   False(   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyR:   À   s    c         C   s!   | d } | j  d ƒ | d <| S(   NR   RN   t   data(   R=   (   R   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   streamÇ   s    
c         C   sf   | d } | j  d d ƒ | d } d | k rb y t j | d ƒ | d <Wqb t k
 r^ qb Xn  | S(   NR   t   sourcet   displayR]   s   application/json(   RG   t   jsont   dumpst	   Exception(   R   R   R   R]   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   display_dataÌ   s    

c         C   s   | d j  d d  ƒ | S(   NR   t   password(   R=   R"   (   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   input_requestÚ   s    (   R&   R'   R(   R.   R   R   RJ   RL   RM   RQ   RW   R8   R:   R^   Rd   Rf   (    (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyR2   o   s&   
											t   V4toV5c           B   s   e  Z d  Z d Z d „  e j j ƒ  Dƒ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z RS(   s   Convert msg spec V4 to V5s   5.0c         C   s   i  |  ] \ } } | | “ q S(    (    (   t   .0t   kR/   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pys
   <dictcomp>ä   s   	 c         C   s3   |  j  | d d <| d r/ |  j  | d d <n  | S(   NR   R.   R<   (   R.   (   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyR   æ   s    
c         C   s  | d } x= d D]5 } | | k r d j  t t | | ƒ ƒ | | <q q W| j d d ƒ | d j d ƒ r™ d | k r™ d | d	 <| j d ƒ | d
 <n  | j d ƒ } | j d i  ƒ } | j d | ƒ d | k r
d j  t t | j d ƒ ƒ ƒ } | j d | ƒ n  d | d <| S(   NR   R?   RC   R)   s   4.1RF   t   pythonRA   R@   RB   RD   RE   R>   R.   R   t   banner(   R?   RC   (   t   joint   mapt   strRG   t
   startswithR=   (   R   R   R   RI   RF   RD   R>   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyRJ   î   s     
'
!
c         C   sM   | d } | j  d g  ƒ } | j d i  ƒ } x | D] } | | | <q5 W| S(   NR   RK   t   user_expressions(   R=   RG   (   R   R   R   RK   Rp   R/   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyRL     s    
c         C   s¼   | d } | j  d i  ƒ } | j d i  ƒ } | rD | j | ƒ n  xq | j d g  ƒ D]] } | j d d  ƒ d k rW d | k rW d | k rš i  | d <n  | j d ƒ | d d	 <qW qW W| S(
   NR   Rp   RK   t   payloadR_   t   pageRN   R]   s
   text/plain(   RG   R=   t   updateR#   R"   (   R   R   R   Rp   RK   Rq   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyRM     s    
$c         C   s8   | d } i  } | d <| d | d <| d | d <| S(   NR   R   R   R   (    (   R   R   t   old_contentRP   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyRQ     s
    
c         C   sq   | d } i d d 6} | d <| d | d <| d rO t  | d ƒ | d <n
 d  | d <d  | d <i  | d <| S(	   NR   t   okR   R   RT   RR   RS   RU   (   R   R"   (   R   R   R   RP   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyRW   $  s    




c         C   sN   | d } | d } i  } | d <| | d <t  | ƒ | d <| d | d <| S(   NR   RX   R   R   RY   (   R   (   R   R   R   RE   RP   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyR9   5  s    


c         C   sõ   | d } i d d 6} | d <| d } | d <i  | d <} i  | d <| rñ g  } x5 d D]- } | j  | t ƒ r\ | j | | ƒ Pq\ q\ Wx5 d D]- } | j  | t ƒ r” | j | | ƒ Pq” q” W| sÛ | j d ƒ n  d j | ƒ | d <n  | S(   s1   inspect_reply can't be easily backward compatibleR   Ru   R   RZ   R]   RU   t   call_deft   init_definitiont
   definitiont   call_docstringt   init_docstringt	   docstrings   <empty docstring>s   
s
   text/plain(   Rv   Rw   Rx   (   Ry   Rz   R{   (   R#   R\   R+   Rl   (   R   R   R   RP   RZ   R]   t   linesRI   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyR;   ?  s&    

c         C   s!   | d } | j  d ƒ | d <| S(   NR   R]   RN   (   R=   (   R   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyR^   W  s    
c         C   sf   | d } | j  d d  ƒ | d } d | k rb y t j | d ƒ | d <Wqb t k
 r^ qb Xn  | S(   NR   R_   R]   s   application/json(   R=   R"   Ra   t   loadsRc   (   R   R   R   R]   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyRd   \  s    

c         C   s   | d j  d t ƒ | S(   NR   Re   (   RG   R\   (   R   R   (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyRf   j  s    (   R&   R'   R(   R.   R2   R   t   itemsR   RJ   RL   RM   RQ   RW   R9   R;   R^   Rd   Rf   (    (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyRg   ß   s   							
			i    c         C   sš   d d l  m } |  d } d | k r6 | ƒ  | d <n  d | k rb t | d j d ƒ d ƒ } n d } t j | | f d	 ƒ } | d	 k r |  S| |  ƒ S(
   sS  Adapt a single message to a target version

    Parameters
    ----------

    msg : dict
        A Jupyter message.
    to_version : int, optional
        The target major version.
        If unspecified, adapt to the current version.

    Returns
    -------

    msg : dict
        A Jupyter message appropriate in the new version.
    i   (   t   utcnowR   t   dateR.   R)   i    i   N(   t   sessionR   R,   R*   t   adaptersR#   R"   (   R   t
   to_versionR   R   t   from_versiont   adapter(    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   adaptp  s    
 i   i   (   i   i   (   i   i   (   R(   t   reRa   t   jupyter_clientR    R	   t   compilet   UNICODER
   R   t   IR   R   t   objectR   R1   R2   Rg   R†   R‚   (    (    (    s5   lib/python2.7/site-packages/jupyter_client/adapter.pyt   <module>   s    		+	p‘"
