a
    §ˆcöC  ã                   @   s  d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 ddl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dlmZ eee ej!j" #d¡ƒƒZ$G dd„ deƒZ%G dd„ de%ƒZ&G dd„ de%ƒZ'eeƒG dd„ deƒƒZ(dS )z= The wx-backend Pyface widget for an embedded IPython shell.
é    N)ÚWxController)ÚInterpreter)ÚFile)ÚIPythonShell)ÚKeyPressedEvent)ÚEventÚInstanceÚprovidesÚStr)Úpython_name)ÚPythonDropTargeté   )ÚWidgetÚ.c                   @   s   e Zd ZdZdd„ ZdS )ÚIPythonControllerzD An WxController for IPython version >= 0.9. Adds a few extras.
    c                    s0   t jˆ g|¢R i |¤Ž ‡ fdd„}|ˆ j_d S )Nc                    s   ˆ   ¡  d S ©N)ÚClearAll)Úargs©Úself© ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/pyface/ui/wx/ipython_widget.pyÚcls3   s    z'IPythonController.__init__.<locals>.cls)r   Ú__init__Úipython0Z	magic_cls)r   r   Úkwargsr   r   r   r   r   /   s    zIPythonController.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   +   s   r   c                       s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚIPython010ControllerzE A WxController hacked/patched specifically for the 0.10 branch.
    Fc                    s&   t ƒ  ||¡ |r"| j jd8  _d S )Nr   )ÚsuperÚexecute_commandÚshellÚcurrent_cell_number©r   ÚcommandÚhidden©Ú	__class__r   r   r"   =   s    z$IPython010Controller.execute_command)F)r   r   r   r   r"   Ú__classcell__r   r   r(   r   r    9   s   r    c                       sˆ   e Zd ZdZeƒ ZdZdd„ Zdd„ Ze	eeƒZ
dd„ Z‡ fd	d
„Zdd„ Zddd„Zdd„ Zdd„ Zddd„Zdd„ Zdd„ Z‡  ZS )ÚIPython09ControllerzD A WxController hacked/patched specifically for the 0.9 branch.
    Nc                 C   s$   | j du rt| dƒr| jj| _ | j S )z% Returns the IPython banner.
        Nr   )Ú_bannerÚhasattrr   ZBANNERr   r   r   r   Ú_get_bannerY   s    


zIPython09Controller._get_bannerc                 C   s
   || _ d S r   )r,   )r   Úvaluer   r   r   Ú_set_bannerb   s    zIPython09Controller._set_bannerc              	   O   sœ   ddl m} d|j_ddd„}tj}|t_tj| g|¢R i |¤Ž |t_t| j	ƒD ]D}| 
d¡sbqRt| j	|ƒ}z|jd u r€d|_W qR ty”   Y qR0 qRd S )	Nr   )ÚiplibTc                 S   s   dS )NÚ
r   )Úxr   r   r   Úmy_rawinputq   s    z1IPython09Controller.__init__.<locals>.my_rawinputÚmagicÚ )N)ÚIPythonr1   ZInteractiveShellZ
isthreadedÚbuiltinsÚ	raw_inputr   r   Údirr   Ú
startswithÚgetattrr   ÚAttributeError)r   r   r   r1   r4   Zold_rawinputÚfuncnameÚfuncr   r   r   r   g   s     



zIPython09Controller.__init__c                    s8   |   |¡}tƒ  |¡\}}|dt|ƒ … | }||fS )z² Returns a list of possible completions for line.
        Overridden from the base class implementation to fix bugs in retrieving
        the completion text from line.

        N)Ú_get_completion_textr!   ÚcompleteÚlen)r   ÚlineZcompletion_textÚ
suggestionÚcompletionsÚnew_liner(   r   r   rA   ˆ   s    
zIPython09Controller.completec                 C   sŽ   |dv rdS t | j d¡ƒdkr0t d|¡s0dS |  ¡  z6| d¡}| d¡sV|d7 }t 	|d	d
¡}|  
¡  W n ty„   d}Y n0 |S dS )a…   Check if a string forms a complete, executable set of
        commands.

        For the line-oriented frontend, multi-line code is not executed
        as soon as it is complete: the users has to enter two line
        returns.

        Overridden from the base class (linefrontendbase.py in IPythonrontend
        to handle a bug with using the '' symbol in multi-line inputs.
        )r6   r2   Tr2   é   z\n[\t ]*\n[\t ]*$Fú\z

z<string>ÚexecN)rB   Úinput_bufferÚsplitÚreÚfindallÚcapture_outputÚrstripÚendswithÚcodeopZcompile_commandZrelease_outputÚ	Exception)r   ÚstringZclean_stringÚis_completer   r   r   rT   ”   s$    ÿ

ÿ
zIPython09Controller.is_completeFc                 C   sÀ   |r$| j  |¡}| j  jd8  _|S | ¡ }t| d¡ƒdkrF|d7 }|  |¡}|| _|  t	 
¡ ¡ |  d¡ |  |d ¡s°|  ¡  |  d¡ |  | jj| jd d d�¡ dS |  ¡  dS dS )	zU Execute a command, not only in the model, but also in the
            view.
        r   r2   zIncomplete or invalid inputÚnumber©rU   FTN)r#   Úexecuter$   rO   rB   rK   Úprefilter_inputrJ   ZProcessEventÚwxZ
PaintEventÚwriterT   Z_colorize_input_bufferZrender_errorÚ
new_promptÚinput_prompt_templateÚ
substituteÚlast_resultÚ	_on_enter)r   r&   r'   ÚresultZcleaned_commandr   r   r   r"   Á   s,    


ÿÿz#IPython09Controller.execute_commandc                 C   s*   |   ¡  |  | jj| jd d d�¡ dS )z& Empty completely the widget.
        rU   r   rV   N)r   r[   r\   r]   r^   r   r   r   r   Úclear_screenè   s    ÿÿz IPython09Controller.clear_screenc                 C   s"   | j d }d| }dt|ƒ d S )z�Returns the current continuation prompt.
        Overridden to generate a continuation prompt matching the length of the
        current prompt.rU   zIn [%d]r   ú:)r^   rB   )r   ÚnZ	promptstrr   r   r   Úcontinuation_promptò   s    
z'IPython09Controller.continuation_promptc                 C   s€   | j rtd| jtjd� | j}|s:|  ¡ r||r||d dks||  |¡\}}|r|t|  |¡ƒ}| j	||d� | j r|t|tjd� dS )zÝ Updates the popup completion menu if it exists. If create is
            true, open the menu.
            Overridden from the base class implementation to filter out
            delimiters from the input buffer.
        Ú_popup_completion)Úfileéÿÿÿÿr   )ÚoffsetN)
ÚdebugÚprintrJ   ÚsysÚ
__stdout__ZAutoCompActiverA   rB   r@   Zpop_completion)r   ÚcreaterC   rD   rE   rh   r   r   r   re   ü   s     ÿÿ
ÿz%IPython09Controller._popup_completionc                 C   s    d}t  |¡}| |¡d }|S )z` Returns the text to be completed by breaking the line at specified
        delimiters.
        z)\s|=|,|:|\((?!.*\))|\[(?!.*\])|\{(?!.*\})rg   )rL   ÚcompilerK   )r   rC   Z
expressionZcomplete_sepÚtextr   r   r   r@     s    	
z(IPython09Controller._get_completion_textc                 C   s´   | j }|  | |  ¡ d¡¡}|  |¡r6| j||d� nz||  ¡  |  | |  ¡ d¡dd… ¡ | _ t| d¡ƒdkr‚|  j d7  _ |dd…  d¡d  	¡  
d¡r°|  j d	7  _ dS )
zó Called when the return key is pressed in a line editing
            buffer.
            Overridden from the base class implementation (in
            IPython/frontend/linefrontendbase.py) to include a continuation
            prompt.
        r6   )Z
raw_stringNrg   r2   rG   z		rb   ú	)rJ   rX   Úreplacerd   rT   rW   Z_get_indent_stringrB   rK   rO   rP   )r   Zcurrent_bufferZcleaned_bufferr   r   r   r_   )  s$    ÿ
ÿÿþÿ zIPython09Controller._on_enter)F)F)r   r   r   r   r
   Útitler,   r.   r0   ÚpropertyZbannerr   rA   rT   r"   ra   rd   re   r@   r_   r*   r   r   r(   r   r+   E   s   	
!-
'


r+   c                       st   e Zd ZdZeƒ ZeeƒZee	dƒZ
‡ fdd„Zdd„ Zddd	„Zdd
d„Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚIPythonWidgetzz The toolkit specific implementation of a PythonShell.  See the
    IPythonShell interface for the API documentation.
    r   c                    s.   t  dt¡ tƒ jf i |¤Ž |  |¡| _dS )z Creates a new pager. zRthe Wx IPython widget us deprecated and will be removed in a future Pyface versionN)ÚwarningsÚwarnÚPendingDeprecationWarningr!   r   Ú_create_controlÚcontrol)r   ÚparentZtraitsr(   r   r   r   Z  s    ýzIPythonWidget.__init__c                 C   s   | j S r   )Úinterpr   r   r   r   Úinterpreterm  s    zIPythonWidget.interpreterTc                 C   s   | j j||d� d| _d S )N©r'   T©ry   r"   Úcommand_executedr%   r   r   r   r"   p  s    zIPythonWidget.execute_commandc                 C   s"   | j jdd|  |d� d| _d S )Nz%run z"%s"r}   Tr~   )r   Úpathr'   r   r   r   Úexecute_filet  s    zIPythonWidget.execute_filec                 C   sd   t }td dkr2td dkr"t}ntd dkr2t}||d| jd�}| tj| j¡ | 	t
| ƒ¡ |S )Nr   r   é	   é
   rg   )r#   )r   ÚIPYTHON_VERSIONr+   r    r{   ZBindrY   ZEVT_CHARÚ_wx_on_charZSetDropTargetr   )r   rz   Úklassr#   r   r   r   rx   |  s    zIPythonWidget._create_controlc                 C   sÒ   t |tƒr| j |j¡ n¨t |tƒrRt|ƒdkrRt |d tƒrR| j |d j¡ npd}t|dƒr¨t |jt	ƒr¨t|jƒdkr¨t
|jƒ}zt||diƒr˜|}W n   Y n0 || jj|< | j|dd� | j ¡  tjS )z) Called when a drop occurs on the shell. r   r   ZdraggedÚnameTFr}   )Ú
isinstanceÚEnthoughtFilery   rZ   Zabsolute_pathÚlistrB   r-   r‡   Ústrr   Úevalr{   Zuser_nsr"   ZSetFocusrY   ÚDragCopy)r   r3   ÚyÚobjÚdefault_drag_resultr‡   Zpy_namer   r   r   Úon_drop’  s4    
ÿ
þýÿ
þý

zIPythonWidget.on_dropc                 C   s   t jS )z@ Always returns wx.DragCopy to indicate we will be doing a copy.)rY   r�   )r   r3   rŽ   r�   r�   r   r   r   Úon_drag_over½  s    zIPythonWidget.on_drag_overc                 C   s<   t | ¡ dk| ¡ dk| ¡ dk| ¡ |d�| _| ¡  dS )z? Called whenever a change is made to the text of the document. r   )Zalt_downZcontrol_downZ
shift_downZkey_codeÚeventN)r   ZAltDownZControlDownZ	ShiftDownZ
GetKeyCodeÚkey_pressedZSkip)r   r“   r   r   r   r…   Æ  s    


û	zIPythonWidget._wx_on_char)T)T)r   r   r   r   r   r   r   r”   r   r   r{   r   r|   r"   r�   rx   r‘   r’   r…   r*   r   r   r(   r   rt   D  s   


+	rt   ))r   r8   rQ   rL   rk   ru   r7   ZIPython.frontend.wx.wx_frontendr   ZIPython.kernel.core.interpreterr   rY   Zapptools.io.filer   r‰   Zpyface.i_python_shellr   Zpyface.key_pressed_eventr   Z
traits.apir   r   r	   r
   Ztraits.util.clean_stringsr   Zpyface.wx.drag_and_dropr   Zwidgetr   ÚtupleÚmapÚintZReleaseZversion_baserK   r„   r   r    r+   rt   r   r   r   r   Ú<module>   s0     