o
    tfa                     @   s  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rkdZ&ndZ&G dd deZ'G dd de'Z(G d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winZnotepadc                   @   s   e Zd ZdZdS )IPythonWidgetz4Dummy class for config inheritance. Destroyed below.N)__name__
__module____qualname____doc__ r   r   a/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/qtconsole/jupyter_widget.pyr   1   s    r   c                       s  e Zd ZdZedZeeeZ	e
edddZe
dddZe
dddZe
dddZe
edd	Ze
edd	Ze
edd	Ze
edd	Ze
edd	Zed
g dZdZdZdZdZdZ dZ! fddZ"dd Z# fddZ$dd Z%dKddZ&dd Z'dd Z(dd Z) fd d!Z*d"d# Z+d$d% Z,d&d' Z-dLd)d*Z.d+d, Z/d-d. Z0dMd0d1Z1dLd2d3Z2dNd4d5Z3dNd6d7Z4dNd8d9Z5d:d; Z6d<d= Z7d>d? Z8d@dA Z9e:dBdLdCdDZ;e:dEdLdFdGZ<e=dHdIdJ Z>  Z?S )O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numberZ
edit_magicZask_exitZset_next_inputpagec                    st   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__r   r   r&   r   s   

zJupyterWidget.__init__c           
      C   s  | j d|dd |  }| jd}|r|j|d d kr|j|  kr|j| j	kr|d }|d }|d }|d	 }t
|d
}t
||}|  }||k r[|jtjj|| d n||krj|jtjj|| d | j| || }	|jtjj|	d | || dS dS dS dS dS )z9Support Jupyter's improved completion machinery.
        zcomplete: %scontentr   Zcompleteparent_headermsg_idmatchesZcursor_startZ
cursor_endr   )nN)logdebuggetZ_get_cursor_request_infoidposZ_get_input_buffer_cursor_poscodeinput_buffermaxmovePositionr   QTextCursorZLeftRight_controlZsetTextCursorZ_complete_with_items)
r8   repcursorinfor=   r@   startendZ
cursor_posoffsetr   r   r   _handle_complete_reply   s8   



z$JupyterWidget._handle_complete_replyc                    s   |d  d}| jd  |}|r=|jdkr=d| _|d }|d dkr(|   n|d	 d
 }| | | jd | dS t | dS )z!Support prompt requests.
        r>   r?   executepromptFr=   statusabortedexecution_countr   N)rD   rE   kindr7   _show_interpreter_promptpopr%   _handle_execute_reply)r8   msgr?   rQ   r=   r#   r;   r   r   r^      s   

z#JupyterWidget._handle_execute_replyc                 C   s   |d }d|vr8| j d|  |dddkr3| js3| j d d| _td	 | jjd
dd dS d| _dS d| _|d }| j dt	| g }d}|D ]\}}}|
 }||krb|| |}qN| | dS )z_ Handle history tail replies, which are only supported
            by Jupyter kernels.
        r=   historyzHistory request failed: %rrX   r   rY   z Retrying aborted history requestTg      ?tail  Zhist_access_typerA   FNz&Received history reply with %i entries)rB   errorrD   _retrying_history_requesttimesleepkernel_clientr`   rC   lenrstripappendZ_set_history)r8   r_   r=   Zhistory_itemsitems	last_cell_cellr   r   r   _handle_history_reply   s2   

z#JupyterWidget._handle_history_replyc           	      C   s   | dd}| j||d}| j| j|d}|d t|d  dD ]\}}|dkr4| || n| || | 	||d  q%| 
|d  dS )z.Insert function for input from other frontendsrZ   r   remoter   rH   r   N)rD   _make_in_prompt_make_continuation_prompt_promptZ
insertText	enumeratestripsplitZ_insert_htmlZ_insert_plain_text_update_prompt)	r8   rP   r=   rr   rA   rW   Zcont_promptiliner   r   r   _insert_other_input   s   
z!JupyterWidget._insert_other_inputc                 C   s`   | j d|dd | |r| j| j|d dd dS | js.| j| j|d ddd dS dS )	zHandle an execute_input messagezexecute_input: %sr=   r   Tbefore_promptF)r~   rr   N)rB   rC   rD   include_outputZ_append_customr|   ru   )r8   r_   r   r   r   _handle_execute_input   s   



z#JupyterWidget._handle_execute_inputc                 C   s   | j d|dd | |rk|   |d }|dd}|d }d|v rm| j| jdd	 | j| j|| 	| d
dd	 |d }d|v rS| j
dsS| jddd	 | j|| j dd	 | 	|so| jddd	 dS dS dS dS )z Handle an execute_result messagezexecute_result: %sr=   r   rZ   r   data
text/plainTr}   rq   r   N)rB   rC   rD   r   flush_clearoutput_append_plain_text
output_sep_append_html_make_out_prompt	from_hereendswithoutput_sep2)r8   r_   r=   Zprompt_numberr   textr   r   r   _handle_execute_result   s*   

z$JupyterWidget._handle_execute_resultc                 C   sN   |  |r%|   |d d }d|v r|d }| |d | dd dS dS )z2The base handler for the ``display_data`` message.r=   r   r   Tr   N)r   r   r   )r8   r_   r   r   r   r   r   _handle_display_data  s   
z"JupyterWidget._handle_display_datac                    s   |d }|d d | _ |d dd}z|dkrt }n|dkr$t }nt| j }|| j_W n	 ty7   Y nw |dd| _| j	rLd	| _	t
   d
S d
S )zHandle kernel info replies.r=   Zlanguage_infonamepygments_lexerr   Zipython3Zipython2bannerFN)r6   rD   r   r   r	   _highlighter_lexerr
   Zkernel_banner	_startingr%   _started_channels)r8   rO   r=   r   lexerr;   r   r   _handle_kernel_info_reply'  s$   
z'JupyterWidget._handle_kernel_info_replyc                 C   s$   d| _ | j  | jjddd dS )zMake a history requestTra   rb   rc   N)r   rh   Zkernel_infor`   r8   r   r   r   r   @  s   
zJupyterWidget._started_channelsc                 C   sL   | j d|dd |d }d|d d }	 | j|| | d dS )zHandle an execute_error messagezexecute_error: %sr=   r   r   	tracebackF &nbsp;z<br/>enamez<span class="error">%s</span>r}   N)rB   rC   rD   joinreplacer   r   r   )r8   r_   r=   r   r   Zename_styledr   r   r   _process_execute_errorK  s
   z$JupyterWidget._process_execute_errorc                 C   s(   | j |d }|du rdS || dS )z@ Reimplemented to dispatch payloads to handler methods.
        sourceNFT)r/   rD   )r8   itemhandlerr   r   r   _process_execute_payloadd  s
   z&JupyterWidget._process_execute_payloadNc                 C   s   |du r$| j r	dS d| _ | jjddd}| |dd}|| jd |< dS | j| _| j| |dd | j	
  }t| j}| |||| _| j| | jdd dS )	2 Reimplemented for IPython-style prompts.
        NTr   )ZsilentrW   FrV   html)r7   rh   rV   Z_ExecutionRequestrE   	input_sepZ_prompt_sepZ_show_promptrs   rN   document	lastBlockri   ru   r    r0   Z_set_continuation_promptrt   )r8   r#   r?   rQ   r!   r"   r   r   r   r\   o  s    

z&JupyterWidget._show_interpreter_promptc                 C   s   | j du rdS | j j}| rY| r[t|}|tjjtjj| j j	 | 
|}| ||| _| j|  | j| d  | j  }t| j}| |||| _ dS dS dS )z1Replace the last displayed prompt with a new one.Nr   )r0   r!   ZisValidr   r   rL   rK   rM   Z
KeepAnchorr"   rs   Z _insert_html_fetching_plain_textru   r   ZrehighlightBlockZ_prompt_cursorZsetPositionpositionrN   r   r   ri   r    )r8   Znew_prompt_numberr!   rP   rW   r"   r   r   r   ry     s&   




zJupyterWidget._update_promptc                 C   sf   |d }|d dkr| j r| j j}nd}n|d }| j r*| j j|kr*| | d| _ | |d  dS )r   r=   rX   rY   r   rZ   Nr   )r0   r#   ry   r\   )r8   r_   r=   Zprevious_prompt_numberr   r   r   "_show_interpreter_prompt_for_reply  s   

z0JupyterWidget._show_interpreter_prompt_for_replylightbgc                 C   sh   |  }|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 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   linuxZnocolorzNo such color scheme: %sN)lowerr   Zdefault_light_style_sheetr2   Zdefault_light_syntax_stylesyntax_styleZdefault_dark_style_sheetZdefault_dark_syntax_styleZdefault_bw_style_sheetZdefault_bw_syntax_styleKeyError)r8   colorsr   r   r   r5     s   	zJupyterWidget.set_default_stylec                 C   s   | j r| j|| d
S | js| d d
S z2d| }|r)| jr)| jj||d}nz| j }W n ty@   | jj|d}Y nw |d| 7 }W n tyW   | d Y d
S w z	t|dd W d
S  t	yt   d	}| ||  Y d
S 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   zInvalid editor command.
T)shellz)Opening editor with command "%s" failed.
N)
custom_editcustom_edit_requestedemiteditorr   editor_lineformatr   r   OSError)r8   r   r{   commandr_   r   r   r   _edit  s4   
zJupyterWidget._editc                 C   N   z| j | }W n ty   ddlm} || j }Y nw |r#| j| }d| 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r8   r#   rr   bodyr   r   r   r   rs        
zJupyterWidget._make_in_promptc                 C   sJ   d}t |dt | }|r|t | jdd 7 }d| | }d| S )zf Given a plain text version of an In prompt, returns an HTML
            continuation prompt.
        z...: r   r   r   )ri   lstripr   rsplit)r8   rW   rr   Z	end_charsZspace_countr   r   r   r   rt     s   z'JupyterWidget._make_continuation_promptc                 C   r   )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   r   r   r   r     r   zJupyterWidget._make_out_promptc                 C   s   |  |d |d  d S )Nr   line_number)r   r8   r   r   r   r   r(   -  s   z"JupyterWidget._handle_payload_editc                 C   s   |d | _ | j|  d S )NZ
keepkernel)r1   Zexit_requestedr   r   r   r   r   r*   0  s   
z"JupyterWidget._handle_payload_exitc                 C   s   |d | _ d S )Nr   )rI   r   r   r   r   r.   4  s   z(JupyterWidget._handle_payload_next_inputc                 C   sF   |d }d|v r| j dkr| j|d dd d S | j|d dd d S )Nr   z	text/htmlrichTr   r   F)r[   Z_page)r8   r   r   r   r   r   r,   7  s   z"JupyterWidget._handle_payload_pager2   c                 C   sL   |  | j | jdur| j | j | jdur$| j | j dS dS )z9 Set the style sheets of the underlying widgets.
        N)ZsetStyleSheetr2   rN   r   ZsetDefaultStyleSheetZ_page_controlr8   changedr   r   r   r3   C  s   

z"JupyterWidget._style_sheet_changedr   c                 C   sF   | j du r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   Z	set_styleZ_ansi_processorZset_background_colorZset_style_sheetr2   r   r   r   r   r4   N  s   
z#JupyterWidget._syntax_style_changedr   c                 C   s   dj tdS )NzJupyter QtConsole {version}
)version)r   r   r   r   r   r   _banner_default]  s   zJupyterWidget._banner_default)T)N)r   )F)@r   r   r   r   r   r   r   ZSignalobjectr   r   default_editorr   r   r2   r   default_in_promptr   default_out_promptr   default_input_sepr   default_output_sepr   default_output_sep2r   r   r    r'   r)   r-   r+   re   r   r&   rU   r^   rp   r|   r   r   r   r   r   r   r   r\   ry   r   r5   r   rs   rt   r   r(   r*   r.   r,   r   r3   r4   r   r   __classcell__r   r   r;   r   r   5   st    	"
!



'


r   c                       s    e Zd ZdZ fddZ  ZS )r   z$Deprecated class; use JupyterWidget.c                    s    t dt t j|i | d S )Nz.IPythonWidget is deprecated; use JupyterWidget)r   DeprecationWarningr%   r&   )r8   ar:   r;   r   r   r&   f  s   zIPythonWidget.__init__)r   r   r   r   r&   r   r   r   r;   r   r   d  s    ))r   collectionsr   
subprocessr   sysrf   warningsr   Zqtpyr   r   ZIPython.lib.lexersr   r   Zpygments.lexersr	   Zpygments.utilr
   Z	qtconsoler   Z	traitletsr   r   r   r   Zfrontend_widgetr   r   r   r   r   r   r   r   Zzmq_shell_sourceplatform
startswithr   r   r   r   r   r   r   <module>   s<        3