
_y]c           @   s  d  Z  d d l m Z d d l Z d d l Z d d l m Z d d l Z d d l	 Z	 d d l
 m Z d d l m Z d d l m Z m Z d d l m Z m Z d d	 l m 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" d Z# d Z$ d Z% d Z& d Z' d Z( e j) j* d  rSd Z+ n d Z+ d e  f d     YZ, d e, f d     YZ- d e- f d     YZ, d S(   s   A FrontendWidget that emulates a repl for a Jupyter kernel.

This supports the additional functionality provided by Jupyter kernel.
i(   t
   namedtupleN(   t   Popen(   t   dedent(   t   warn(   t   QtCoret   QtGui(   t   IPythonLexert   IPython3Lexer(   t   get_lexer_by_name(   t   ClassNotFound(   t   __version__(   t   Boolt   Unicodet   observet   defaulti   (   t   FrontendWidget(   t   styless/   In [<span class="in-prompt-number">%i</span>]: s0   Out[<span class="out-prompt-number">%i</span>]: s   
t    s&   ipykernel.zmqshell.ZMQInteractiveShellt   wint   notepadt   IPythonWidgetc           B   s   e  Z d  Z RS(   s4   Dummy class for config inheritance. Destroyed below.(   t   __name__t
   __module__t   __doc__(    (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR   4   s   t   JupyterWidgetc           B   sH  e  Z d  Z e e  Z e j e e  Z	 e
 e d e d d Z e
 d e d d  Z e
 d e d d  Z e
 d e d d  Z e
 e d e Z e
 e d e Z e
 e d e Z e
 e d e Z e
 e d e Z e d d d	 d
 g  Z d Z d Z d Z d Z  e Z! e Z" d   Z# d   Z$ d   Z% d   Z& d   Z' d   Z( d   Z) d   Z* d   Z+ d   Z, d   Z- d   Z. d. d  Z0 d   Z1 d   Z2 d d  Z3 d. d   Z4 e d!  Z5 e d"  Z6 e d#  Z7 d$   Z8 d%   Z9 d&   Z: d'   Z; e< d(  d. d)   Z= e< d*  d. d+   Z> e? d,  d-    Z@ RS(/   s&   A FrontendWidget for a Jupyter kernel.t   configt   helpsN  
        A command for invoking a GUI text editor. If the string contains a
        {filename} format specifier, it will be used. Otherwise, the filename
        will be appended to the end the command. To use a terminal text editor,
        the command should launch a new terminal, e.g.
        ``"gnome-terminal -- vim"``.
        s  
        The editor command to use when a specific line number is requested. The
        string should contain two format specifiers: {line} and {filename}. If
        this parameter is not specified, the line number option to the %edit
        magic will be ignored.
        s  
        A CSS stylesheet. The stylesheet can contain classes for:
            1. Qt: QPlainTextEdit, QFrame, QWidget, etc
            2. Pygments: .c, .k, .o, etc. (see PygmentsHighlighter)
            3. QtConsole: .error, .in-prompt, .out-prompt, etc
        s   
        If not empty, use this Pygments style for syntax highlighting.
        Otherwise, the style sheet is queried for Pygments style
        information.
        t   _PromptBlockt   blockt   lengtht   numbert
   edit_magict   ask_exitt   set_next_inputt   pagec         O   s   t  t |   j | |   i |  j |  j 6|  j |  j 6|  j |  j 6|  j	 |  j
 6|  _ d  |  _ d  |  _ |  j r |  j   |  j   n
 |  j   d  |  _ d  S(   N(   t   superR   t   __init__t   _handle_payload_editt   _payload_source_editt   _handle_payload_exitt   _payload_source_exitt   _handle_payload_paget   _payload_source_paget   _handle_payload_next_inputt   _payload_source_next_inputt   _payload_handlerst   Nonet   _previous_prompt_objt   _keep_kernel_on_exitt   style_sheett   _style_sheet_changedt   _syntax_style_changedt   set_default_stylet   language_name(   t   selft   argst   kw(    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR$   u   s    			

c   
      C   su  |  j  j d | j d d   |  j   } |  j j d  } | rq| j | d d k rq| j |  j   k rq| j |  j	 k rq| d } | d } | d } | d	 } t
 | d
  } t
 | |  } |  j   } | | k  r | j t j j d | | n, | | k r+| j t j j d | | n  |  j j |  | | }	 | j t j j d |	 |  j | |  n  d S(   s9   Support Jupyter's improved completion machinery.
        s   complete: %st   contentR   t   completet   parent_headert   msg_idt   matchest   cursor_startt
   cursor_endi    t   nN(   t   logt   debugt   gett   _get_cursort   _request_infot   idt   post   _get_input_buffer_cursor_post   codet   input_buffert   maxt   movePositionR   t   QTextCursort   Leftt   Rightt   _controlt   setTextCursort   _complete_with_items(
   R6   t   rept   cursort   infoR9   R=   t   startt   endt
   cursor_post   offset(    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt   _handle_complete_reply   s.    




c         C   s   | d j  d  } |  j d j  |  } | r | j d k r | d } | d d k re |  j   n | d d	 } |  j |  |  j d j |  n t t |   j |  d
 S(   s!   Support prompt requests.
        R;   R<   t   executet   promptR9   t   statust   abortedt   execution_counti   N(   RC   RE   t   kindt   _show_interpreter_promptt   popR#   R   t   _handle_execute_reply(   R6   t   msgR<   RU   R9   R   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyRc      s    
c         C   s,  | d } d | k r |  j  j d |  | j d d  d k r |  j r |  j  j d  t |  _ t j d  |  j j d	 d
 d d  n	 t	 |  _ d St	 |  _ | d } |  j  j
 d t |   g  } d } xE | D]= \ } } } | j   } | | k r | j |  | } q q W|  j |  d S(   s_    Handle history tail replies, which are only supported
            by Jupyter kernels.
        R9   t   historys   History request failed: %rR]   R   R^   s    Retrying aborted history requestg      ?t   hist_access_typet   tailR@   i  Ns&   Received history reply with %i entriesu    (   RA   t   errorRC   t   _retrying_history_requestt   Truet   timet   sleept   kernel_clientRe   t   FalseRB   t   lent   rstript   appendt   _set_history(   R6   Rd   R9   t   history_itemst   itemst	   last_cellt   _t   cell(    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt   _handle_history_reply   s,    

			
c         C   s   | j  d d  } |  j | d t } |  j |  j d t } | j d  xp t | d j   j d   D]O \ } } | d k r |  j	 | |  n |  j	 | |  |  j
 | | d  ql W|  j | d  d S(   s.   Insert function for input from other frontendsR_   i    t   remotes   
RI   i   N(   RC   t   _make_in_promptRj   t   _make_continuation_promptt   _promptt
   insertTextt	   enumeratet   stript   splitt   _insert_htmlt   _insert_plain_textt   _update_prompt(   R6   RT   R9   R@   R\   t   cont_promptt   it   line(    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt   _insert_other_input   s    ,c         C   sR   |  j  j d | j d d   |  j |  rN |  j |  j | d d t n  d S(   s   Handle an execute_input messages   execute_input: %sR9   R   t   before_promptN(   RA   RB   RC   t   include_outputt   _append_customR   Rj   (   R6   Rd   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt   _handle_execute_input   s    c         C   s4  |  j  j d | j d d   |  j |  r0|  j   | d } | j d d  } | d } d | k r0|  j |  j d t |  j |  j	 | d	 |  j
 |  d t | d } d
 | k r |  j j d
  r |  j d
 d t n  |  j | |  j d t |  j
 |  s-|  j d
 d t q-q0n  d S(   s    Handle an execute_result messages   execute_result: %sR9   R   R_   i    t   datas
   text/plainR   Ry   s   
N(   RA   RB   RC   R   t   flush_clearoutputt   _append_plain_textt
   output_sepRj   t   _append_htmlt   _make_out_promptt	   from_heret   endswitht   output_sep2(   R6   Rd   R9   t   prompt_numberR   t   text(    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt   _handle_execute_result   s"    



c         C   su   |  j  |  rq |  j   | d d } | d d } d | k r^ | d } |  j | t  n  |  j d t  n  d S(   s2   The base handler for the ``display_data`` message.R9   R   t   metadatas
   text/plainu   
N(   R   R   R   Rj   (   R6   Rd   R   R   R   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt   _handle_display_data  s    

c         C   s   | d } | d d |  _  | d j d d  } yO | d k rL t   } n' | d k rd t   } n t |  j   } | |  j _ Wn t k
 r n X| j d d  |  _ |  j	 r t
 |  _	 t t |   j   n  d	 S(
   s   Handle kernel info replies.R9   t   language_infot   namet   pygments_lexerR   t   ipython3t   ipython2t   bannerN(   R5   RC   R   R   R   t   _highlightert   _lexerR	   t   kernel_bannert	   _startingRn   R#   R   t   _started_channels(   R6   RS   R9   R   t   lexer(    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt   _handle_kernel_info_reply$  s     
		c         C   s3   t  |  _ |  j j   |  j j d d d d  d S(   s   Make a history requestRf   Rg   R@   i  N(   Rj   R   Rm   t   kernel_infoRe   (   R6   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR   =  s    	c         C   s   |  j  j d | j d d   | d } d j | d  d } t r | j d d  } | j d d  } | d	 } d
 | } | j | |  } |  j |  n |  j | d |  j |  d S(   s   Handle an execute_error messages   execute_error: %sR9   R   s   
t	   tracebackt    s   &nbsp;s   <br/>t   enames   <span class="error">%s</span>R   N(	   RA   RB   RC   t   joinRn   t   replaceR   R   R   (   R6   Rd   R9   R   R   t   ename_styled(    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt   _process_execute_errorH  s    


c         C   s8   |  j  j | d  } | d k r& t S| |  t Sd S(   s@    Reimplemented to dispatch payloads to handler methods.
        t   sourceN(   R-   RC   R.   Rn   Rj   (   R6   t   itemt   handler(    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt   _process_execute_payloada  s
    
c         C   s   | d k rK |  j j d d t } |  j | d  } | |  j d | <d S|  j |  _ |  j |  j	 |  d t |  j
 j   j   } t |  j  } |  j | | |  |  _ |  j |  j |  j  d t d S(   s2    Reimplemented for IPython-style prompts.
        R   t   silentR\   R[   Nt   html(   R.   Rm   R[   Rj   t   _ExecutionRequestRE   t	   input_sept   _prompt_sept   _show_promptRz   RP   t   documentt	   lastBlockRo   R|   R   R/   t   _set_continuation_promptR{   (   R6   R   R<   RU   R   R   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyRa   l  s    c         C   s   |  j  j } | j   r | j   r t j |  } | j t j j t j j |  j  j	  |  j
 |  } |  j | |  |  _ |  j j | j    |  j j | j   d  n  d S(   s1   Replace the last displayed prompt with a new one.i   N(   R/   R   t   isValidR   R   RM   RL   RO   t
   KeepAnchorR   Rz   t    _insert_html_fetching_plain_textR|   R   t   rehighlightBlockt   _prompt_cursort   setPositiont   position(   R6   t   new_prompt_numberR   RT   R\   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR     s    	c         C   s   | d } | d d k r; |  j  r2 |  j  j } qE d } n
 | d } |  j  ry |  j  j | k ry |  j |  d |  _  n  |  j | d  d S(   s2    Reimplemented for IPython-style prompts.
        R9   R]   R^   i    R_   i   N(   R/   R   R   R.   Ra   (   R6   Rd   R9   t   previous_prompt_number(    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt"   _show_interpreter_prompt_for_reply  s    
		
	t   lightbgc         C   s   | j    } | d k r3 t j |  _ t j |  _ n^ | d k rZ t j |  _ t j |  _ n7 | d k r t j |  _ t j	 |  _ n t
 d |   d S(   s    Sets the widget style to the class defaults.

        Parameters
        ----------
        colors : str, optional (default lightbg)
            Whether to use the default light background or dark
            background or B&W style.
        R   t   linuxt   nocolors   No such color scheme: %sN(   t   lowerR   t   default_light_style_sheetR1   t   default_light_syntax_stylet   syntax_stylet   default_dark_style_sheett   default_dark_syntax_stylet   default_bw_style_sheett   default_bw_syntax_stylet   KeyError(   R6   t   colors(    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR4     s    	c         C   s!  |  j  r |  j j | |  n |  j s8 |  j d  n y d | } | rr |  j rr |  j j d | d |  } nJ y |  j j   } Wn& t k
 r |  j j d |  } n X| d | 7} Wn t k
 r |  j d  n@ Xy t | d t	 Wn( t
 k
 rd } |  j | |  n Xd	 S(
   s    Opens a Python script for editing.

        Parameters
        ----------
        filename : str
            A path to a local system file.

        line : int, optional
            A line of interest in the file.
        s{   No default editor available.
Specify a GUI text editor in the `JupyterWidget.editor` configurable to enable the %edit magics   "%s"t   filenameR   R   s   Invalid editor command.
t   shells)   Opening editor with command "%s" failed.
N(   t   custom_editt   custom_edit_requestedt   emitt   editorR   t   editor_linet   formatR   R   Rj   t   OSError(   R6   R   R   t   commandRd   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt   _edit  s*    		
c         C   sb   y |  j  | } Wn0 t k
 rC d d l m } | |  j   } n X| rZ |  j | } n  d | S(   s;    Given a prompt number, returns an HTML In prompt.
        i(   t   escapes!   <span class="in-prompt">%s</span>(   t	   in_promptt	   TypeErrort   xml.sax.saxutilsR   t   other_output_prefix(   R6   R   Ry   t   bodyR   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyRz     s    c         C   sd   d } t  | j d   t  |  } | rN | t  |  j j d  d  7} n  d | | } d | S(   sf    Given a plain text version of an In prompt, returns an HTML
            continuation prompt.
        s   ...: s   
is   &nbsp;s!   <span class="in-prompt">%s</span>(   Ro   t   lstripR   t   rsplit(   R6   R\   Ry   t	   end_charst   space_countR   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR{     s    #c         C   sb   y |  j  | } Wn0 t k
 rC d d l m } | |  j   } n X| rZ |  j | } n  d | S(   s<    Given a prompt number, returns an HTML Out prompt.
        i(   R   s"   <span class="out-prompt">%s</span>(   t
   out_promptR   R   R   R   (   R6   R   Ry   R   R   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR     s    c         C   s   |  j  | d | d  d  S(   NR   t   line_number(   R   (   R6   R   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR%     s    c         C   s!   | d |  _  |  j j |   d  S(   Nt
   keepkernel(   R0   t   exit_requestedR   (   R6   R   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR'   "  s    c         C   s   | d |  _  d  S(   NR   (   RJ   (   R6   R   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR+   &  s    c         C   sZ   | d } d | k r? |  j  d k r? |  j | d d t n |  j | d d t d  S(   NR   s	   text/htmlt   richR   s
   text/plain(   R`   t   _pageRj   Rn   (   R6   R   R   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR)   )  s    
R1   c         C   sj   |  j  |  j  |  j d k	 r; |  j j   j |  j  n  |  j d k	 rf |  j j   j |  j  n  d S(   s9    Set the style sheets of the underlying widgets.
        N(   t   setStyleSheetR1   RP   R.   R   t   setDefaultStyleSheett   _page_control(   R6   t   changed(    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR2   5  s
    R   c         C   s\   |  j  d k r d S|  j rE |  j  j |  j  |  j j |  j  n |  j  j |  j  d S(   s3    Set the style for the syntax highlighter.
        N(   R   R.   R   t	   set_stylet   _ansi_processort   set_background_colort   set_style_sheetR1   (   R6   R   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR3   @  s    	R   c         C   s   d j  d t  S(   Ns   Jupyter QtConsole {version}
t   version(   R   R
   (   R6   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt   _banner_defaultO  s    N(A   R   R   R   R   Rn   R   R   t   Signalt   objectR   R   t   default_editorRj   R   R   R1   R   t   default_in_promptR   t   default_out_promptR   t   default_input_sepR   t   default_output_sepR   t   default_output_sep2R   R    R   R&   R(   R,   R*   Ri   R   R$   RZ   Rc   Rx   R   R   R   R   R   R   R   R   R.   Ra   R   R   R4   R   Rz   R{   R   R%   R'   R+   R)   R   R2   R3   R   R   (    (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR   8   sh   						"		!										'					
	c           B   s   e  Z d  Z d   Z RS(   s$   Deprecated class; use JupyterWidget.c         O   s*   t  d t  t t |   j | |   d  S(   Ns.   IPythonWidget is deprecated; use JupyterWidget(   R   t   DeprecationWarningR#   R   R$   (   R6   t   aR8   (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR$   X  s    (   R   R   R   R$   (    (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyR   V  s   (.   R   t   collectionsR    t   os.patht   ost   ret
   subprocessR   t   sysRk   t   textwrapR   t   warningsR   t   qtconsole.qtR   R   t   IPython.lib.lexersR   R   t   pygments.lexersR   t   pygments.utilR	   t	   qtconsoleR
   t	   traitletsR   R   R   R   t   frontend_widgetR   R   R   R   R   R   R   R   t   zmq_shell_sourcet   platformt
   startswithR   R   R   (    (    (    s7   lib/python2.7/site-packages/qtconsole/jupyter_widget.pyt   <module>   s<   "	   