3
7ßL]ÿ(  ã               @   sÌ   d 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	Z	ddl
mZmZ ddlmZ ddlmZ ddlmZmZ ddlmZ d	d
lmZ ddlZeeƒG dd„ deeƒƒZG dd„ deƒZG dd„ dƒZdS )z$ Enthought pyface package component
é    N)ÚShell)ÚEventÚprovides)Úpython_name)ÚPythonDropTarget)ÚIPythonShellÚMPythonShell)ÚKeyPressedEventé   )ÚWidgetc                   sx   e Zd ZdZeZeeƒ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dd„ Zdd„ Z‡  ZS )ÚPythonShellzz The toolkit specific implementation of a PythonShell.  See the
    IPythonShell interface for the API documentation.
    c                s2   t t| ƒjf |Ž | j|ƒ| _| jjj| jƒ dS )z Creates a new pager. N)Úsuperr   Ú__init__Ú_create_controlÚcontrolÚhandlersÚappendZ_on_command_executed)ÚselfÚparentZtraits)Ú	__class__© ú=/tmp/pip-install-5sebvq2j/pyface/pyface/ui/wx/python_shell.pyr   :   s    zPythonShell.__init__c             C   s   | j jS )N)r   Úinterp)r   r   r   r   ÚinterpreterJ   s    zPythonShell.interpreterTc             C   s"   |r| j j|ƒ n| j j|ƒ d S )N)r   Úhidden_pushZExecute)r   ÚcommandÚhiddenr   r   r   Úexecute_commandM   s    zPythonShell.execute_commandc             C   s  t jj|ƒ}tjdƒ}|j}||d< dd„ |d< tj}|gt_tjd }|tjd< tj	}tj
}	tj}
|r~tƒ  t_	 t_
t_n| j t_	 t_
t_z<|s²| jjƒ  | jjd| ƒ t|||ƒ |sÌ| jjƒ  W d |t_|tjd< |t_	|	t_
|
t_X |d= |d= |d= | jƒ jj|ƒ d S )NÚ__main__Ú__file__c               S   s   dS )NTr   r   r   r   r   Ú<lambda>^   s    z*PythonShell.execute_file.<locals>.<lambda>Ú__nonzero__z# Executing "%s"
Ú__name__)ÚosÚpathÚbasenameÚtypesÚ
ModuleTypeÚ__dict__ÚsysÚargvÚmodulesÚstdinÚstdoutÚstderrÚ_NullIOr   ZclearCommandÚwriteZexecfileÚpromptr   ÚlocalsÚupdate)r   r$   r   ÚfilenameZmain_modZprog_nsÚ	save_argvZ	save_mainZ	old_stdinZ
old_stdoutZ
old_stderrr   r   r   Úexecute_fileT   s>    




zPythonShell.execute_filec             C   s   | j j| j jfS )a   Return the current command history and index.

        Returns
        -------
        history : list of str
            The list of commands in the new history.
        history_index : int from 0 to len(history)
            The current item in the command history navigation.
        )r   ÚhistoryÚhistoryIndex)r   r   r   r   Úget_historyŠ   s    
zPythonShell.get_historyc             C   s<   d|  kot |ƒkn  s$t |ƒ}t|ƒ| j_|| j_dS )a3   Replace the current command history and index with new ones.

        Parameters
        ----------
        history : list of str
            The list of commands in the new history.
        history_index : int from 0 to len(history)
            The current item in the command history navigation.
        r   N)ÚlenÚlistr   r7   r8   )r   r7   Zhistory_indexr   r   r   Úset_history–   s    
zPythonShell.set_historyc             C   s*   t |dƒ}tj|| jƒ |jt| ƒƒ |S )Nr
   éÿÿÿÿ)ÚPyShellÚwxZEVT_CHARÚ_wx_on_charZSetDropTargetr   )r   r   Úshellr   r   r   r   ©   s    
zPythonShell._create_controlc          
   C   s‚   d}t |dƒrXt|jtjƒrXt|jƒdkrXt|jƒ}yt||diƒrH|}W n   Y nX || jj	j
|< | jj|ƒ | jjƒ  tjS )z) Called when a drop occurs on the shell. ZdraggedÚnamer   T)ÚhasattrÚ
isinstancerB   ÚsixÚstring_typesr:   r   Úevalr   r   r2   ÚrunZSetFocusr?   ÚDragCopy)r   ÚxÚyÚobjÚdefault_drag_resultrB   Zpy_namer   r   r   Úon_drop¸   s    


zPythonShell.on_dropc             C   s   t jS )z@ Always returns wx.DragCopy to indicate we will be doing a copy.)r?   rI   )r   rJ   rK   rL   rM   r   r   r   Úon_drag_overÓ   s    zPythonShell.on_drag_overc             C   s®   |j ƒ r:|jƒ dkr:| jjjƒ }|dkrr| jj|d ƒ n8|j ƒ rr|jƒ dkrr| jjjƒ }|dkrr| jj|d ƒ 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. i=  é   r
   i?  é
   )Zalt_downZcontrol_downZ
shift_downZkey_codeÚeventNiöÿÿÿ)ZAltDownZ
GetKeyCoderA   r   ZGetZoomZSetZoomr	   ZControlDownZ	ShiftDownÚkey_pressedZSkip)r   rR   Zzoomr   r   r   r@   Û   s    



zPythonShell._wx_on_char)T)T)r"   Ú
__module__Ú__qualname__Ú__doc__r   Zcommand_executedr	   rS   r   r   r   r6   r9   r<   r   rN   rO   r@   Ú__classcell__r   r   )r   r   r   (   s   

6r   c                   sN   e Zd Zdejejejdddf‡ fdd„	Zdd„ Zdd	„ Z	‡ fd
d„Z
‡  ZS )r>   r
   Ú Nc	          	      s<   g | _ tjjj| _tt| ƒj||||||||f|	ž|
Ž d S )N)r   rE   ÚmovesÚbuiltinsÚ	raw_inputr   r>   r   )r   r   ÚidÚposÚsizeÚstyleZ	introTextr2   ZInterpClassÚargsÚkwds)r   r   r   r   ø   s    zPyShell.__init__c          
   C   s`   t jƒ  zHd| _| jj|ƒ| _d| _| jsN| j|jƒ ƒ x| jD ]
}|ƒ  q@W W dt j	ƒ  X dS )zk Send a command to the interpreter for execution without adding
            output to the display.
        TFN)
r?   ZBeginBusyCursorZwaitingr   ÚpushZmoreZ
addHistoryÚrstripr   ZEndBusyCursor)r   r   Úhandlerr   r   r   r     s    zPyShell.hidden_pushc             C   s"   | j tjƒ | j|ƒ | jƒ  dS )z.Send command to the interpreter for execution.N)r0   r#   Úlinesepr   r1   )r   r   r   r   r   rb     s    
zPyShell.pushc                sD   | j dƒ | jdƒ | jdƒ | jtjj_| jƒ  tt	| ƒj
ƒ  dS )z‹Cleanup before destroying the control...namely, return std I/O and
        the raw_input() function back to their rightful owners!
        FN)ZredirectStdoutZredirectStderrZredirectStdinr[   rE   rY   rZ   Údestroyr   ÚPyShellBaseÚDestroy)r   )r   r   r   rh     s    


zPyShell.Destroyr=   )r"   rT   rU   r?   ZDefaultPositionZDefaultSizeZCLIP_CHILDRENr   r   rb   rh   rW   r   r   )r   r   r>   ö   s   
r>   c               @   sf   e Zd Z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
dd„ Zdd„ Zddd„ZdS )r/   zA A portable /dev/null for use with PythonShell.execute_file.
    c             C   s   dS )Nr   r   )r   r   r   r   Útell+  s    z_NullIO.tellr
   c             C   s   dS )NrX   r   )r   Únr   r   r   Úread,  s    z_NullIO.readNc             C   s   dS )NrX   r   )r   Úlengthr   r   r   Úreadline-  s    z_NullIO.readlinec             C   s   g S )Nr   )r   r   r   r   Ú	readlines.  s    z_NullIO.readlinesc             C   s   d S )Nr   )r   Úsr   r   r   r0   /  s    z_NullIO.writec             C   s   d S )Nr   )r   r;   r   r   r   Ú
writelines0  s    z_NullIO.writelinesc             C   s   dS )Nr   r   )r   r   r   r   Úisatty1  s    z_NullIO.isattyc             C   s   d S )Nr   )r   r   r   r   Úflush2  s    z_NullIO.flushc             C   s   d S )Nr   )r   r   r   r   Úclose3  s    z_NullIO.closer   c             C   s   d S )Nr   )r   r]   Úmoder   r   r   Úseek4  s    z_NullIO.seekr=   )r=   )N)r   )r"   rT   rU   rV   ri   rk   rm   rn   r0   rp   rq   rr   rs   ru   r   r   r   r   r/   (  s   

r/   )rV   Zsix.moves.builtinsrE   r#   r)   r&   Zwx.py.shellr   rg   r?   Z
traits.apir   r   Ztraits.util.clean_stringsr   Zpyface.wx.drag_and_dropr   Zpyface.i_python_shellr   r   Zpyface.key_pressed_eventr	   Zwidgetr   r   r>   r/   r   r   r   r   Ú<module>   s$   
 N2