3
7ßL]
E  ã               @   s$  d Z ddlm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 ddlZe e!e"ej#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.
é    )Úprint_functionN)Ú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                s*   t jˆ f|ž|Ž ‡ fdd„}|ˆ j_d S )Nc                s   ˆ j ƒ  d S )N)ÚClearAll)Úargs)Úself© ú?/tmp/pip-install-5sebvq2j/pyface/pyface/ui/wx/ipython_widget.pyÚcls9   s    z'IPythonController.__init__.<locals>.cls)r   Ú__init__Úipython0Z	magic_cls)r   r   Úkwargsr   r   )r   r   r   5   s    zIPythonController.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   1   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 t| ƒj||ƒ |r&| j jd8  _d S )Nr   )Úsuperr   Úexecute_commandÚshellÚcurrent_cell_number)r   ÚcommandÚhidden)Ú	__class__r   r   r!   B   s    z$IPython010Controller.execute_command)F)r   r   r   r   r!   Ú__classcell__r   r   )r&   r   r   >   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krt| dƒr| jj| _ | j S )z% Returns the IPython banner.
        Nr   )Ú_bannerÚhasattrr   ZBANNER)r   r   r   r   Ú_get_banner^   s    


zIPython09Controller._get_bannerc             C   s
   || _ d S )N)r)   )r   Úvaluer   r   r   Ú_set_bannerg   s    zIPython09Controller._set_bannerc             O   s¨   ddl m} d|j_ddd„}tjjj}|tjj_tj	| f|ž|Ž |tjj_xTt
| jƒD ]F}|jdƒsjqZt| j|ƒ}y|jd krˆd|_W qZ tk
rž   Y qZX qZW d S )	Nr   )ÚiplibTc             S   s   dS )NÚ
r   )Úxr   r   r   Úmy_rawinputv   s    z1IPython09Controller.__init__.<locals>.my_rawinputÚmagicÚ )N)ÚIPythonr.   ZInteractiveShellZ
isthreadedÚsixÚmovesÚbuiltinsÚ	raw_inputr   r   Údirr   Ú
startswithÚgetattrr   ÚAttributeError)r   r   r   r.   r1   Zold_rawinputÚfuncnameÚfuncr   r   r   r   m   s     






zIPython09Controller.__init__c                s<   | j |ƒ}tt| ƒj|ƒ\}}|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    r(   ÚcompleteÚlen)r   ÚlineZcompletion_textÚ
suggestionÚcompletionsÚnew_line)r&   r   r   r@   Œ   s
    


zIPython09Controller.completec             C   s    |dkrdS t | jjdƒƒdkr2tjd|ƒ r2dS | jƒ  y6|jdƒ}|jdƒsX|d7 }tj	|d	d
ƒ}| j
ƒ  W n& tk
r– } z
d}W Y dd}~X nX |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.
        r3   r/   Té   z\n[\t ]*\n[\t ]*$Fú\z

z<string>ÚexecN)r3   r/   )rA   Úinput_bufferÚsplitÚreÚfindallZcapture_outputÚrstripÚendswithÚcodeopZcompile_commandZrelease_outputÚ	Exception)r   ÚstringZclean_stringÚis_completeÚer   r   r   rR   ™   s     

 zIPython09Controller.is_completeFc             C   sÀ   |r$| j j|ƒ}| j  jd8  _|S |jƒ }t|jdƒƒdkrF|d7 }| j|ƒ}|| _| jt	j
ƒ ƒ | jdƒ | j|d ƒs°| jƒ  | jdƒ | j| jj| jd d d�ƒ dS | jƒ  dS dS )	zU Execute a command, not only in the model, but also in the
            view.
        r   r/   zIncomplete or invalid inputÚnumber)rT   FTN)r"   Úexecuter#   rM   rA   rJ   Úprefilter_inputrI   ZProcessEventÚwxZ
PaintEventÚwriterR   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j| jd d d�ƒ dS )z& Empty completely the widget.
        rT   r   )rT   N)r   rY   rZ   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.rT   zIn [%d]r   ú:)r\   rA   )r   ÚnZ	promptstrr   r   r   Úcontinuation_promptî   s    
z'IPython09Controller.continuation_promptc             C   s†   | j rtd| jtjd� | j}| jƒ r8|r8|d dk s@|dkr‚| j|ƒ\}}|r‚t| j|ƒƒ}| 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)Úfiler   r   T)ÚoffsetNéÿÿÿÿ)
ÚdebugÚprintrI   ÚsysÚ
__stdout__ZAutoCompActiver@   rA   r?   Zpop_completion)r   ÚcreaterB   rC   rD   re   r   r   r   rc   ø   s    z%IPython09Controller._popup_completionc             C   s    d}t j|ƒ}|j|ƒd }|S )z` Returns the text to be completed by breaking the line at specified
        delimiters.
        z)\s|=|,|:|\((?!.*\))|\[(?!.*\])|\{(?!.*\})r   rf   )rK   ÚcompilerJ   )r   rB   Z
expressionZcomplete_sepÚtextr   r   r   r?     s    	
z(IPython09Controller._get_completion_textc             C   s´   | j }| j|j| jƒ dƒƒ}| j|ƒr6| j||d� nz|| jƒ  | j|j| jƒ dƒdd
… ƒ | _ t|jdƒƒdkr‚|  j d7  _ |dd… jdƒd j	ƒ j
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.
        r3   )Z
raw_stringNr   r/   rF   z		r`   ú	rf   rf   rf   )rI   rV   Úreplacerb   rR   rU   Z_get_indent_stringrA   rJ   rM   rN   )r   Zcurrent_bufferZcleaned_bufferr   r   r   r]   $  s    

 zIPython09Controller._on_enter)F)F)r   r   r   r   r   Útitler)   r+   r-   ÚpropertyZbannerr   r@   rR   r!   r_   rb   rc   r?   r]   r'   r   r   )r&   r   r(   J   s   	
*
$

r(   c                   sr   e Zd ZdZeZeeƒZee	f ƒ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.
    c                s"   t t| ƒjf |Ž | j|ƒ| _dS )z Creates a new pager. N)r    rr   r   Ú_create_controlÚcontrol)r   ÚparentZtraits)r&   r   r   r   T  s    zIPythonWidget.__init__c             C   s   | j S )N)Úinterp)r   r   r   r   Úinterpretera  s    zIPythonWidget.interpreterTc             C   s   | j j||d� d| _d S )N)r%   T)rt   r!   Úcommand_executed)r   r$   r%   r   r   r   r!   d  s    zIPythonWidget.execute_commandc             C   s"   | j jdd|  |d� d| _d S )Nz%run z"%s")r%   T)rt   r!   rx   )r   Úpathr%   r   r   r   Úexecute_fileh  s    zIPythonWidget.execute_filec             C   sb   t }td dkr2td dkr"t}ntd dkr2t}||d| jd�}tj|| jƒ |jt	| ƒƒ |S )Nr   r   é	   é
   )r"   rf   )
r   ÚIPYTHON_VERSIONr(   r   rv   rW   ZEVT_CHARÚ_wx_on_charZSetDropTargetr   )r   ru   Úklassr"   r   r   r   rs   p  s    zIPythonWidget._create_controlc          
   C   sÔ   t |tƒr| jj|jƒ nªt |tƒrRt|ƒdkrRt |d tƒrR| jj|d jƒ nrd}t|dƒrªt |jt	j
ƒrªt|jƒdkrªt|jƒ}yt||diƒrš|}W n   Y nX || jj|< | j|dd� | jjƒ  tjS )z) Called when a drop occurs on the shell. r   r   ZdraggedÚnameTF)r%   )Ú
isinstanceÚEnthoughtFilert   rX   Zabsolute_pathÚlistrA   r*   r€   r5   Ústring_typesr   Úevalrv   Zuser_nsr!   ZSetFocusrW   ÚDragCopy)r   r0   ÚyÚobjÚdefault_drag_resultr€   Zpy_namer   r   r   Úon_drop†  s$    



zIPythonWidget.on_dropc             C   s   t jS )z@ Always returns wx.DragCopy to indicate we will be doing a copy.)rW   r†   )r   r0   r‡   rˆ   r‰   r   r   r   Úon_drag_over«  s    zIPythonWidget.on_drag_overc             C   s<   t |jƒ dk|jƒ dk|jƒ dk|jƒ |d�| _|jƒ  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   rx   r   r�   r	   r   rv   r   rw   r!   rz   rs   rŠ   r‹   r~   r'   r   r   )r&   r   rr   =  s   


%	rr   )+r   Ú
__future__r   Zsix.moves.builtinsr5   rO   rK   ri   r4   ZIPython.frontend.wx.wx_frontendr   ZIPython.kernel.core.interpreterr   rW   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_baserJ   r}   r   r   r(   rr   r   r   r   r   Ú<module>   s0    t