a
    §ˆcn*  ã                   @   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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 eeƒG dd„ deeƒƒZG dd„ deƒZG dd„ deƒZdS )z$ Enthought pyface package component
é    N)ÚShell)ÚEventÚprovides)Úpython_name)ÚPythonDropTarget)ÚIPythonShellÚMPythonShell)ÚKeyPressedEventé   )ÚLayoutWidgetc                       s|   e Zd ZdZeƒ ZeeƒZd‡ 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.
    Nc                    s>   |  dd¡}tƒ jf d|i|¤Ž |r:|  ¡  t dt¡ dS )z Creates a new pager. ÚcreateTÚparentzšautomatic widget creation is deprecated and will be removed in a future Pyface version, use create=False and explicitly call create() for future behaviourN)ÚpopÚsuperÚ__init__r   ÚwarningsÚwarnÚPendingDeprecationWarning)Úselfr   Ztraitsr   ©Ú	__class__© ú\/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/pyface/ui/wx/python_shell.pyr   8   s    üzPythonShell.__init__c                 C   s   | j jS ©N)ÚcontrolÚinterp©r   r   r   r   ÚinterpreterN   s    zPythonShell.interpreterTc                 C   s"   |r| j  |¡ n| j  |¡ d S r   )r   Úhidden_pushZExecute)r   ÚcommandÚhiddenr   r   r   Úexecute_commandQ   s    zPythonShell.execute_commandc              	   C   sD  t j |¡}t 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_zf|s²| j ¡  | j d| ¡ tt|ƒ ¡ ||ƒ |sÔ| j ¡  W |t_|tjd< |t_	|	t_
|
t_n$|t_|tjd< |t_	|	t_
|
t_0 |d= |d= |d= |  ¡ j |¡ d S )NÚ__main__Ú__file__c                   S   s   dS )NTr   r   r   r   r   Ú<lambda>b   ó    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ÚwriteÚexecÚopenÚreadÚpromptr   ÚlocalsÚupdate)r   r*   r!   ÚfilenameZmain_modZprog_nsÚ	save_argvZ	save_mainZ	old_stdinZ
old_stdoutZ
old_stderrr   r   r   Úexecute_fileX   sH    




ü
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ÚhistoryIndexr   r   r   r   Úget_historyŽ   s    
zPythonShell.get_historyc                 C   s:   d|  krt |ƒks"n 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   r@   rA   )r   r@   Zhistory_indexr   r   r   Úset_historyš   s    
zPythonShell.set_historyc                 C   s:   t |dƒ}| tj| j¡ | t| ƒ¡ |j | j	¡ |S )Néÿÿÿÿ)
ÚPyShellZBindÚwxZEVT_CHARÚ_wx_on_charZSetDropTargetr   ÚhandlersÚappendZ_on_command_executed)r   r   Úshellr   r   r   Ú_create_control­   s
    
zPythonShell._create_controlc                 C   s€   d}t |dƒrVt|jtƒrVt|jƒdkrVt|jƒ}zt||diƒrF|}W n   Y n0 || jjj	|< | j 
|¡ | j ¡  tjS )z) Called when a drop occurs on the shell. ZdraggedÚnamer   T)ÚhasattrÚ
isinstancerN   ÚstrrC   r   Úevalr   r   r;   ÚrunZSetFocusrH   ÚDragCopy)r   ÚxÚyÚobjÚdefault_drag_resultrN   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.)rH   rT   )r   rU   rV   rW   rX   r   r   r   Úon_drag_overÝ   s    zPythonShell.on_drag_overc                 C   s®   |  ¡ r:| ¡ dkr:| jj ¡ }|dkrr| j |d ¡ n8|  ¡ rr| ¡ dkrr| jj ¡ }|dkrr| j |d ¡ t|  ¡ dk| ¡ dk| ¡ dk| ¡ |d�| _	| 
¡  dS )z? Called whenever a change is made to the text of the document. i=  é   r
   i?  iöÿÿÿ)Zalt_downZcontrol_downZ
shift_downZkey_codeÚeventN)ZAltDownZ
GetKeyCoderL   r   ZGetZoomZSetZoomr	   ZControlDownZ	ShiftDownÚkey_pressedZSkip)r   r\   Zzoomr   r   r   rI   å   s     


û	zPythonShell._wx_on_char)N)T)T)r(   Ú
__module__Ú__qualname__Ú__doc__r   Zcommand_executedr	   r]   r   r   r"   r?   rB   rE   rM   rY   rZ   rI   Ú__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 )rG   rF   Ú Nc	              	      s8   g | _ t| _tƒ j||||||||g|	¢R i |
¤Ž d S r   )rJ   ÚinputÚ	raw_inputr   r   )r   r   ÚidÚposÚsizeÚstyleZ	introTextr;   ZInterpClassÚargsÚkwdsr   r   r   r     s     ø	÷
özPyShell.__init__c                 C   sd   t  ¡  zLd| _| j |¡| _d| _| jsJ|  | ¡ ¡ | jD ]
}|ƒ  q>W t  	¡  n
t  	¡  0 dS )zk Send a command to the interpreter for execution without adding
            output to the display.
        TFN)
rH   ZBeginBusyCursorZwaitingr   ÚpushZmoreZ
addHistoryÚrstriprJ   ZEndBusyCursor)r   r    Úhandlerr   r   r   r   !  s    

zPyShell.hidden_pushc                 C   s"   |   tj¡ |  |¡ |  ¡  dS )z.Send command to the interpreter for execution.N)r6   r)   Úlinesepr   r:   )r   r    r   r   r   rk   3  s    
zPyShell.pushc                    s<   |   d¡ |  d¡ |  d¡ | jt_|  ¡  tƒ  ¡  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redirectStdinrd   ÚbuiltinsÚdestroyr   ÚDestroyr   r   r   r   rq   9  s    


zPyShell.Destroy)r(   r^   r_   rH   ZDefaultPositionZDefaultSizeZCLIP_CHILDRENr   r   rk   rq   ra   r   r   r   r   rG      s   ÷ rG   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 )r5   zA A portable /dev/null for use with PythonShell.execute_file.
    c                 C   s   dS ©Nr   r   r   r   r   r   ÚtellI  s    z_NullIO.tellrF   c                 C   s   dS ©Nrb   r   )r   Únr   r   r   r9   L  s    z_NullIO.readNc                 C   s   dS rt   r   )r   Úlengthr   r   r   ÚreadlineO  s    z_NullIO.readlinec                 C   s   g S r   r   r   r   r   r   Ú	readlinesR  s    z_NullIO.readlinesc                 C   s   d S r   r   )r   Úsr   r   r   r6   U  s    z_NullIO.writec                 C   s   d S r   r   )r   rD   r   r   r   Ú
writelinesX  s    z_NullIO.writelinesc                 C   s   dS rr   r   r   r   r   r   Úisatty[  s    z_NullIO.isattyc                 C   s   d S r   r   r   r   r   r   Úflush^  s    z_NullIO.flushc                 C   s   d S r   r   r   r   r   r   Úclosea  s    z_NullIO.closer   c                 C   s   d S r   r   )r   rf   Úmoder   r   r   Úseekd  s    z_NullIO.seek)rF   )N)r   )r(   r^   r_   r`   rs   r9   rw   rx   r6   rz   r{   r|   r}   r   r   r   r   r   r5   E  s   

r5   )r`   ro   r)   r/   r,   r   Zwx.py.shellr   ZPyShellBaserH   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layout_widgetr   r   rG   Úobjectr5   r   r   r   r   Ú<module>   s$    ZE