
    
8da                     \   d Z ddlmZ ddlmZ ddlZddl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$        %                    d          rdZ&ndZ& G d de          Z' G d de'          Z( G d de(          Z'dS )zA FrontendWidget that emulates a repl for a Jupyter kernel.

This supports the additional functionality provided by Jupyter kernel.
    )
namedtuple)PopenN)warn)QtCoreQtGui)IPythonLexerIPython3Lexer)get_lexer_by_name)ClassNotFound)__version__)BoolUnicodeobservedefault   )FrontendWidget)stylesz/In [<span class="in-prompt-number">%i</span>]: z0Out[<span class="out-prompt-number">%i</span>]: 
 z&ipykernel.zmqshell.ZMQInteractiveShellwinnotepadc                       e Zd ZdZdS )IPythonWidgetz4Dummy class for config inheritance. Destroyed below.N)__name__
__module____qualname____doc__     8lib/python3.11/site-packages/qtconsole/jupyter_widget.pyr   r   1   s        >>>>r   r   c                       e Zd ZdZ ed          Z ej        ee          Z	 e
edd          Z e
dd          Z e
dd          Z e
dd          Z e
ed	          Z e
ed	          Z e
ed	          Z e
ed	          Z e
ed	          Z ed
g d          ZdZdZdZdZdZ dZ! fdZ"d Z# fdZ$d Z%d0dZ&d Z'd Z(d Z) fdZ*d Z+d Z,d Z-d1dZ.d Z/d Z0d2d!Z1d1d"Z2d3d#Z3d3d$Z4d3d%Z5d& Z6d' Z7d( Z8d) Z9 e:d*          d1d+            Z; e:d,          d1d-            Z< e=d.          d/             Z> xZ?S )4JupyterWidgetz&A FrontendWidget for a Jupyter kernel.FTaN  
        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"``.
        )confighelpa  
        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.
        a  
        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
        z
        If not empty, use this Pygments style for syntax highlighting.
        Otherwise, the style sheet is queried for Pygments style
        information.
        )r#   _PromptBlock)blocklengthnumber
edit_magicask_exitset_next_inputpagec                 h    t                      j        |i | | j        | j        | j        | j        | j        | j        | j        | j	        i| _
        d | _        d | _        | j        r)|                                  |                                  n|                                  d | _        d| _        d S )NF)super__init___payload_source_edit_handle_payload_edit_payload_source_exit_handle_payload_exit_payload_source_page_handle_payload_page_payload_source_next_input_handle_payload_next_input_payload_handlers_previous_prompt_obj_keep_kernel_on_exitstyle_sheet_style_sheet_changed_syntax_style_changedset_default_stylelanguage_name_prompt_requested)selfargskw	__class__s      r    r/   zJupyterWidget.__init__r   s    $%"%%% %(A%(A%(A+d.M	"P
 %)!$(!  	%%%'''&&((((""$$$ "!&r   c                    | j                             d|                    dd                     |                                 }| j                            d          }|rW|j        |d         d         k    rA|j        |                                 k    r%|j        | j	        k    r|d         }|d         }|d         }|d	         }t          |d
          }t          ||          }|                                 }||k     r*|                    t          j        j        ||z
             n/||k    r)|                    t          j        j        ||z
             | j                            |           ||z
  }	|                    t          j        j        |	           |                     ||           dS dS dS dS dS )z9Support Jupyter's improved completion machinery.
        zcomplete: %scontentr   completeparent_headermsg_idmatchescursor_start
cursor_endr   )nN)logdebugget_get_cursor_request_infoidpos_get_input_buffer_cursor_poscodeinput_buffermaxmovePositionr   QTextCursorLeftRight_controlsetTextCursor_complete_with_items)
rA   repcursorinforF   rJ   startend
cursor_posoffsets
             r    _handle_complete_replyz$JupyterWidget._handle_complete_reply   s    	~swwy"'='=>>>!!##!%%j11 	7TWO 4X >>>H A A C CCCI!222)nGi(GN+E,'CqMMEc5//C ::<<JZ##E$5$:'1C'7 $ : : : :z!!##E$5$;'*Z'7 $ : : : M''///5[F  1 6&AAA%%fg666665	7 	7>>CC22r   c                    |d                              d          }| j        d                              |          }|r}|j        dk    rrd| _        |d         }|d         dk    r|                                  n |d	         d
z   }|                     |           | j        d                             |           dS t                                          |           dS )z!Support prompt requests.
        rH   rI   executepromptFrF   statusabortedexecution_countr   N)rP   rR   kindr@   _show_interpreter_promptpopr.   _handle_execute_reply)rA   msgrI   rb   rF   r(   rD   s         r    rq   z#JupyterWidget._handle_execute_reply   s     _%))(33!),0088 
	/DI))%*D")nGx I----//// !23a7--f555y)--f55555GG))#.....r   c                 d   |d         }d|vr| j                             d|z             |                    dd          dk    rY| j        sR| j                             d           d| _        t	          j        d	           | j                            d
d           nd| _        dS d| _        |d         }| j                             dt          |                     g }d}|D ]7\  }}}|
                                }||k    r|                    |           |}8|                     |           dS )z_ Handle history tail replies, which are only supported
            by Jupyter kernels.
        rF   historyzHistory request failed: %rrk   r   rl   z Retrying aborted history requestTg      ?tail  hist_access_typerM   FNz&Received history reply with %i entries)rN   errorrP   _retrying_history_requesttimesleepkernel_clientrt   rO   lenrstripappend_set_history)rA   rr   rF   history_itemsitems	last_cell_cells           r    _handle_history_replyz#JupyterWidget._handle_history_reply   sK    i.G##HNN7?@@@{{8R((I55040N 6 ABBB15.
4   "**FT*JJJJ16.F).&	*?]ASASTTT	' 	! 	!JAq$;;==Dy  T""" 	%     r   c                 
   |                     dd          }|                     ||          }|                     | j        |          }|                    d           t          |d                                                             d                    D ]Q\  }}|dk    r|                     ||           n|                     ||           | 	                    ||dz              R| 
                    |dz              dS )z.Insert function for input from other frontendsrm   r   remoter   rV   r   N)rP   _make_in_prompt_make_continuation_prompt_prompt
insertText	enumeratestripsplit_insert_html_insert_plain_text_update_prompt)	rA   ra   rF   r   rM   rj   cont_promptilines	            r    _insert_other_inputz!JupyterWidget._insert_other_input   s
   KK)1--%%a%7744T\&4QQ$ !6!6!8!8!>!>t!D!DEE 	9 	9GAtAvv!!&&1111!!&+666##FD4K8888 	AE"""""r   c                 2   | j                             d|                    dd                     |                     |          r%|                     | j        |d         d           dS | j        s&|                     | j        |d         dd           dS dS )	zHandle an execute_input messagezexecute_input: %srF   r   Tbefore_promptF)r   r   N)rN   rO   rP   include_output_append_customr   r   )rA   rr   s     r    _handle_execute_inputz#JupyterWidget._handle_execute_input   s    *CGGIr,B,BCCCs## 	2(#i.   N N N N N 	2(#i."5   2 2 2 2 2	2 	2r   c                    | j                             d|                    dd                     |                     |          r$|                                  |d         }|                    dd          }|d         }d|v r|                     | j        d	           |                     |                     || 	                    |           
          d	           |d         }d|v r1| j        
                    d          s|                     dd	           |                     || j        z   d	           | 	                    |          s|                     dd	           dS dS dS dS )z Handle an execute_result messagezexecute_result: %srF   r   rm   r   data
text/plainTr   r   r   N)rN   rO   rP   r   flush_clearoutput_append_plain_text
output_sep_append_html_make_out_prompt	from_hereendswithoutput_sep2)rA   rr   rF   prompt_numberr   texts         r    _handle_execute_resultz$JupyterWidget._handle_execute_result   s   +SWWY-C-CDDDs## 	F""$$$)nG#KK(91==M6?Dt##''t'LLL!!))-DNNSVDWDW@W)XX"& "    L) 4<<(@(@(F(F<++D+EEE''t/?(?t'TTT~~c** F++D+EEEEE'	F 	F
 $#F Fr   c                     |                      |          r\|                                  |d         d         }d|v r|d         }|                     |d           |                     dd           dS dS )z2The base handler for the ``display_data`` message.rF   r   r   Tr   N)r   r   r   )rA   rr   r   r   s       r    _handle_display_dataz"JupyterWidget._handle_display_data  s    
 s## 		0""$$$y>&)D t##L)''d333##D$/////		0 		0r   c                    |d         }|d         d         | _         |d                             dd          }	 |dk    rt                      }n)|dk    rt                      }nt	          | j                   }|| j        _        n# t          $ r Y nw xY w|                    dd          | _        | j	        r)d	| _	        t                                                       d
S d
S )zHandle kernel info replies.rF   language_infonamepygments_lexerr   ipython3ipython2bannerFN)r?   rP   r	   r   r
   _highlighter_lexerr   kernel_banner	_startingr.   _started_channels)rA   r`   rF   r   lexerrD   s        r    _handle_kernel_info_replyz'JupyterWidget._handle_kernel_info_reply'  s    i.$_5f= 1556FKK	 ++%:--$)$*<==',D$$ 	 	 	D	 %[[266> 	("DNGG%%'''''	( 	(s   A
B 
BBc                 ~    d| _         | j                                         | j                            dd           dS )zMake a history requestTru   rv   rw   N)r   r}   kernel_infort   rA   s    r    r   zJupyterWidget._started_channels@  s@    &&(((""Fd"CCCCCr   c                    | j                             d|                    dd                     |d         }d                    |d                   dz   }	 |                     ||                     |                      dS )zHandle an execute_error messagezexecute_error: %srF   r   r   	tracebackF &nbsp;z<br/>enamez<span class="error">%s</span>r   N)rN   rO   rP   joinreplacer   r   r   )rA   rr   rF   r   r   ename_styleds         r    _process_execute_errorz$JupyterWidget._process_execute_errorK  s    *CGGIr,B,BCCCi.IIgk233d:		V  ##IPSATAT=T#UUUUUr   c                 d    | j                             |d                   }|dS  ||           dS )z@ Reimplemented to dispatch payloads to handler methods.
        sourceNFT)r8   rP   )rA   itemhandlers      r    _process_execute_payloadz&JupyterWidget._process_execute_payloadd  s:     (,,T(^<<?5GDMMM4r   Nc                 4   |U| j         rdS d| _         | j                            dd          }|                     |dd          }|| j        d         |<   dS | j        | _        |                     |                     |          d           | j	        
                                                                }t          | j                  }|                     |||          | _        |                     |                     | j                  d           dS )	2 Reimplemented for IPython-style prompts.
        NTr   )silentrj   Fri   html)r@   r}   ri   _ExecutionRequestrR   	input_sep_prompt_sep_show_promptr   r]   document	lastBlockr~   r   r%   r9   _set_continuation_promptr   )rA   r(   rI   rb   r&   r'   s         r    ro   z&JupyterWidget._show_interpreter_prompto  s(    >% %)D"'//4/@@F))&(EBBD48Dy)&1F  >$..v66TBBB&&((2244T\""$($5$5eVV$L$L! 	%%**4<88t 	& 	E 	E 	E 	E 	Er   c                    | j         dS | j         j        }|                                rP|                                r=t	          j        |          }|                    t          j        j        t          j        j        | j         j	                   | 
                    |          }|                     ||          | _        | j                            |                                           | j                            |                                dz
             | j                                                                        }t+          | j                  }|                     |||          | _         dS dS dS )z1Replace the last displayed prompt with a new one.Nr   )r9   r&   isValidr   r   rZ   rY   r\   
KeepAnchorr'   r    _insert_html_fetching_plain_textr   r   rehighlightBlock_prompt_cursorsetPositionpositionr]   r   r   r~   r%   )rA   new_prompt_numberr&   ra   rj   r'   s         r    r   zJupyterWidget._update_prompt  si   $,F)/ ==?? 	\uzz|| 	\ &u--F 1 7 % 1 < $ 9 @B B B ))*;<<F@@   DL
 ..v||~~>>> ++FOO,=,=,ABBB M**,,6688E&&F(,(9(9%IZ([([D%%%+	\ 	\ 	\ 	\r   c                    |d         }|d         dk    r| j         r| j         j        }nd}n|d         }| j         r,| j         j        |k    r|                     |           d| _         |                     |dz              dS )r   rF   rk   rl   r   rm   Nr   )r9   r(   r   ro   )rA   rr   rF   previous_prompt_numbers       r    "_show_interpreter_prompt_for_replyz0JupyterWidget._show_interpreter_prompt_for_reply  s     i.8	))( +)-)B)I&&)*&&%,->%?"$ 	-)04JJJ 6777(,D% 	%%&<q&@AAAAAr   lightbgc                 J   |                                 }|dk    r$t          j        | _        t          j        | _        dS |dk    r$t          j        | _        t          j        | _        dS |dk    r$t          j        | _        t          j	        | _        dS t          d|z            )z 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   linuxnocolorzNo such color scheme: %sN)lowerr   default_light_style_sheetr;   default_light_syntax_stylesyntax_styledefault_dark_style_sheetdefault_dark_syntax_styledefault_bw_style_sheetdefault_bw_syntax_styleKeyError)rA   colorss     r    r>   zJupyterWidget.set_default_style  s     9%?D & ADW__%>D & @DY%<D & >D5f<===r   c                 D   | j         r| j                            ||           d
S | j        s|                     d           d
S 	 d|z  }|r$| j        r| j                            ||          }nN	 | j                                        }|d|z   z  }n+# t          $ r | j                            |          }Y nw xY w	 t          |d           d
S # t          $ r d}|                     ||z             Y d
S w xY w# t          $ r |                     d	           Y d
S w xY w)z 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.
        z{No default editor available.
Specify a GUI text editor in the `JupyterWidget.editor` configurable to enable the %edit magicz"%s")filenamer   r   )r   T)shellz)Opening editor with command "%s" failed.
zInvalid editor command.
N)
custom_editcustom_edit_requestedemiteditorr   editor_lineformatr   r   OSError)rA   r   r   commandrr   s        r    _editzJupyterWidget._edit  s     	;&++Hd;;;;; 	;## %5 6 6 6 6 6;!H, 	2D, 	2".55x;? 6 A AGG2"&+"4"4"6"6  3>1 $ H H H"&+"4"4h"4"G"GH;'...... ; ; ;FC++C'M::::::;  E E E''(CDDDDDDEsH   +C< 0B 		C< %B:7C< 9B::C< >C $C98C9<DDc                     	 | j         |z  }n&# t          $ r ddlm}  || j                   }Y nw xY w|r
| j        |z   }d|z  S )z; Given a prompt number, returns an HTML In prompt.
        r   escape!<span class="in-prompt">%s</span>)	in_prompt	TypeErrorxml.sax.saxutilsr  other_output_prefixrA   r(   r   bodyr  s        r    r   zJupyterWidget._make_in_prompt  sy    	*>F*DD 	* 	* 	*//////6$.))DDD	*  	3+d2D2T99   
  00c                     d}t          |                    d                    t          |          z
  }|r0|t          | j                            d          d                   z  }d|z  |z   }d|z  S )zf Given a plain text version of an In prompt, returns an HTML
            continuation prompt.
        z...: r   r   r  )r~   lstripr  rsplit)rA   rj   r   	end_charsspace_countr	  s         r    r   z'JupyterWidget._make_continuation_prompt  sx     	&----..Y? 	J3t7>>tDDRHIIIK+%	12T99r   c                     	 | j         |z  }n&# t          $ r ddlm}  || j                   }Y nw xY w|r
| j        |z   }d|z  S )z< Given a prompt number, returns an HTML Out prompt.
        r   r  z"<span class="out-prompt">%s</span>)
out_promptr  r  r  r  r  s        r    r   zJupyterWidget._make_out_prompt  sy    	+?V+DD 	+ 	+ 	+//////6$/**DDD	+  	3+d2D3d::r
  c                 J    |                      |d         |d                    d S )Nr   line_number)r   rA   r   s     r    r1   z"JupyterWidget._handle_payload_edit-  s&    

4
#T-%899999r   c                 T    |d         | _         | j                            |            d S )N
keepkernel)r:   exit_requestedr   r  s     r    r3   z"JupyterWidget._handle_payload_exit0  s,    $($6!  &&&&&r   c                      |d         | _         d S )Nr   )rW   r  s     r    r7   z(JupyterWidget._handle_payload_next_input4  s     Lr   c                     |d         }d|v r*| j         dk    r|                     |d         d           d S |                     |d         d           d S )Nr   z	text/htmlrichTr   r   F)rn   _page)rA   r   r   s      r    r5   z"JupyterWidget._handle_payload_page7  sf     F|$49#6#6JJtK(tJ44444JJtL)J66666r   r;   c                    |                      | j                   | j        1| j                                                            | j                   | j        3| j                                                            | j                   dS dS )z9 Set the style sheets of the underlying widgets.
        N)setStyleSheetr;   r]   r   setDefaultStyleSheet_page_controlrA   changeds     r    r<   z"JupyterWidget._style_sheet_changedC  s     	4+,,,=$M""$$99$:JKKK)''))>>t?OPPPPP *)r   r   c                     | j         dS | j        r@| j                             | j                   | j                            | j                   dS | j                             | j                   dS )z3 Set the style for the syntax highlighter.
        N)r   r   	set_style_ansi_processorset_background_colorset_style_sheetr;   r!  s     r    r=   z#JupyterWidget._syntax_style_changedN  sx     $F 	@''(9::: 55d6GHHHHH--d.>?????r   r   c                 8    d                     t                    S )NzJupyter QtConsole {version}
)version)r   r   r   s    r    _banner_defaultzJupyterWidget._banner_default]  s    .55k5JJJr   )T)N)r   )F)@r   r   r   r   r   r   r   Signalobjectr   r   default_editorr   r   r;   r   default_in_promptr  default_out_promptr  default_input_sepr   default_output_sepr   default_output_sep2r   r   r%   r0   r2   r6   r4   rz   r   r/   rg   rq   r   r   r   r   r   r   r   r   r   ro   r   r   r>   r   r   r   r   r1   r3   r7   r5   r   r<   r=   r   r*  __classcell__rD   s   @r    r"   r"   5   s       00
 $u++K)FM&&99W^D  F '  K '  K 7$  L )$777I+D999J)$777I+D999J'-d;;;K :n.K.K.KLLL'%!1! %I' ' ' ' '< 7  7  7D/ / / / /"! ! !B# # # # 	2 	2 	2F F F00 0 0 ( ( ( ( (2D D DV V V2	 	 	E E E E4\ \ \>B B B2> > > >4%; %; %; %;N: : : :	: 	: 	: 	:; ; ; ;&: : :' ' ') ) )7 7 7 W]Q Q Q Q W^
@ 
@ 
@ 
@ WXK K K K K K Kr   r"   c                   "     e Zd ZdZ fdZ xZS )r   z$Deprecated class; use JupyterWidget.c                 d    t          dt                      t                      j        |i | d S )Nz.IPythonWidget is deprecated; use JupyterWidget)r   DeprecationWarningr.   r/   )rA   arC   rD   s      r    r/   zIPythonWidget.__init__f  s=    =	! 	! 	!!"r"""""r   )r   r   r   r   r/   r3  r4  s   @r    r   r   d  s>        ..# # # # # # # # #r   ))r   collectionsr   
subprocessr   sysr{   warningsr   qtpyr   r   IPython.lib.lexersr   r	   pygments.lexersr
   pygments.utilr   	qtconsoler   	traitletsr   r   r   r   frontend_widgetr   r   r   r.  r/  r0  r1  r2  zmq_shell_sourceplatform
startswithr-  r   r"   r   r   r    <module>rG     s    # " " " " "       



                : : : : : : : : - - - - - - ' ' ' ' ' ' ! ! ! ! ! ! 5 5 5 5 5 5 5 5 5 5 5 5 + + + + + +       F G     < <5!! NNN? ? ? ? ?N ? ? ?jK jK jK jK jKM jK jK jK^# # # # #M # # # # #r   