
    e7                         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mZ ddl	m
Z
 ddlmZ  G d d	ej                        Zy)
    N   )getConfigOption)QtCore	QtWidgets   )
ReplWidget)ExceptionHandlerWidgetc                   l    e Zd ZdZddZd ZddZddZddZd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zy)ConsoleWidgeta  
    Widget displaying console output and accepting command input.
    Implements:
        
      - eval python expressions / exec python statements
      - storable history of commands
      - exception handling allowing commands to be interpreted in the context of any level in the exception stack frame
    
    Why not just use python in an interactive shell (or ipython) ? There are a few reasons:
       
      - pyside does not yet allow Qt event processing and interactive shell at the same time
      - on some systems, typing in the console _blocks_ the qt event loop until the user presses enter. This can
        be baffling and frustrating to users since it would appear the program has frozen.
      - some terminals (eg windows cmd.exe) have notoriously unfriendly interfaces
      - ability to add extra features like exception stack introspection
      - ability to have multiple interactive prompts, including for spawned sub-processes
    Nc                    t         j                  j                  | |       | j                          |i }| |d<   || _        || _        | j                  j                  | _        | j                  j                  | _        | j                  j                          || j                  j                  |       || _        	 | j                         }|6dg|z   | j                  _        | j$                  j'                  |ddd          d| _        y# t        $ r0}t        j                  t        j                            d}Y d}~td}~ww xY w)a  
        ==============  =============================================================================
        **Arguments:**
        namespace       dictionary containing the initial variables present in the default namespace
        historyFile     optional file for storing command history
        text            initial text to display in the console window
        editor          optional string for invoking code editor (called when stack trace entries are 
                        double-clicked). May contain {fileName} and {lineNum} format keys. Example:: 
                      
                            editorCommand --loadfile {fileName} --gotoline {lineNum}
        ==============  =============================================================================
        N__console__ )r   QWidget__init___setupUilocalNamespaceeditorreploutputinputsetFocussetPlainTexthistoryFileloadHistory	Exceptionsys
excepthookexc_infohistoryhistoryListaddItemscurrentTraceback)selfparent	namespacer   textr   r    excs           ;/usr/lib/python3/dist-packages/pyqtgraph/console/Console.pyr   zConsoleWidget.__init__   s    	""40I#'	- 'ii&&YY__


KK$$T*&	&&(G "$DJJ%%gddm4 $  	NNCLLN+G	s   =D 	E&EEc                    t        j                  |       | _        | j                  | j                         | j                  j	                  dddd       | j                  j                  d       t        j                  t        j                  j                  j                  |       | _        | j                  j                  | j                  dd       t        | j                  | j                  |       | _        | j                  j                  | j                          t        j"                  |       | _        | j$                  j'                          | j                  j                  | j$                         t        j(                  d|       | _        | j*                  j-                  d       | j                   j.                  j                  | j*                         | j                   j0                  j3                  | j4                         | j                   j6                  j3                  | j8                         t;        |       | _        | j<                  j'                          | j                  j                  | j<                         t        j(                  d|       | _        | j>                  j-                  d       | j                   j.                  j                  | j>                         | j<                  j@                  j3                  | jB                         | j>                  jD                  j3                  | j<                  jF                         | j*                  jD                  j3                  | j$                  jF                         | j$                  jH                  j3                  | jJ                         | j$                  jL                  j3                  | jN                         y )Nr   HistoryTzExceptions..)(r   QGridLayoutlayout	setLayoutsetContentsMargins
setSpacing	QSplitterr   QtOrientationVerticalsplitter	addWidgetr   globalslocalsr   QListWidgetr!   hideQPushButton
historyBtnsetCheckableinputLayoutsigCommandEnteredconnect_commandEnteredsigCommandRaisedException_commandRaisedExceptionr	   
excHandlerexceptionBtnsigStackItemDblClicked_stackItemDblClickedtoggled
setVisibleitemClickedcmdSelecteditemDoubleClickedcmdDblClicked)r$   s    r)   r   zConsoleWidget._setupUiJ   s   ++D1t{{#&&q!Q2q!!++FII,A,A,J,JDQdmmQ2t||T[[$?			*$006 0 01#//	4@$$T*		''8		##++D,@,@A		++33D4P4PQ060%11.$G&&t,		''(9(9:..66t7P7PQ!!))$//*D*DE''(8(8(C(CD$$,,T-=-=>**2243E3EF    c                 t    |r| j                   j                  d       | j                  j                  |       y NT)rE   
setCheckedrD   catchAllExceptionsr$   catchs     r)   rR   z ConsoleWidget.catchAllExceptionso   +    ((.**51rN   c                 t    |r| j                   j                  d       | j                  j                  |       y rP   )rE   rQ   rD   catchNextExceptionrS   s     r)   rW   z ConsoleWidget.catchNextExceptiont   rU   rN   c                 :    | j                   j                  |       y N)rD   setStackr$   frames     r)   rZ   zConsoleWidget.setStacky   s      'rN   c                     | j                   _t        j                  j                  | j                         r5t	        | j                   d      5 }t        j                  |      cddd       S yy# 1 sw Y   yxY w)z@Return the list of previously-invoked command strings (or None).Nrb)r   ospathexistsopenpickleload)r$   pfs     r)   r   zConsoleWidget.loadHistory|   sc    'BGGNN4;K;K,Ld&&- '{{2' ' -M'' 's   A--A6c                     | j                   6t        | j                   d      5 }t        j                  ||       ddd       yy# 1 sw Y   yxY w)z5Store the list of previously-invoked command strings.Nwb)r   rb   rc   dump)r$   r    re   s      r)   saveHistoryzConsoleWidget.saveHistory   sM    'd&&- )GR() ) () )s   AAc                     | j                   j                  |       | j                  | j                  j                  dd        | j                   j                         }|j                  |j                                y )Nr   d   )r!   addItemri   r   r    verticalScrollBarsetValuemaximum)r$   r   cmdsbs       r)   rA   zConsoleWidget._commandEntered   s[      %++Ac23//1
BJJL!rN   c                 :    | j                   j                  |       y rY   )rD   exceptionHandler)r$   r   r(   s      r)   rC   z%ConsoleWidget._commandRaisedException   s    ((-rN   c                     | j                   j                         }|0| j                   j                  j                         r|j                  S | j
                  S rY   )rD   selectedFramerunSelectedFrameCheck	isChecked	f_globalsr   r[   s     r)   r7   zConsoleWidget.globals   sE    --/!F!F!P!P!R??"&&&rN   c                     | j                   j                         }|0| j                   j                  j                         r|j                  S | j
                  S rY   )rD   ru   rv   rw   f_localsr   r[   s     r)   r8   zConsoleWidget.locals   sE    --/!F!F!P!P!R>>!&&&rN   c                     | j                   j                  |      dz    }| j                  j                  |       | j                  j	                          y Nr   )r!   rowr   
setHistoryr   r$   itemindexs      r)   rK   zConsoleWidget.cmdSelected   sB    ""&&t,Q./

e$

rN   c                     | j                   j                  |      dz    }| j                  j                  |       | j                  j	                          y r|   )r!   r}   r   r~   execCmdr   s      r)   rM   zConsoleWidget.cmdDblClicked   sB    ""&&t,Q./

e$

rN   c                    | j                   }|t        d      }|y | j                  j                         }|j                  }|j
                  j                  }t        j                  | j                   j                  ||      d       y )NeditorCommand)fileNamelineNumT)shell)
r   r   rD   ru   f_linenof_codeco_filename
subprocessPopenformat)r$   handlerr   r   tbr   r   s          r)   rG   z"ConsoleWidget._stackItemDblClicked   sq    >$_5F>__**,++99((++Xw+OW[\rN   )NNNNN)TrY   )__name__
__module____qualname____doc__r   r   rR   rW   rZ   r   ri   rA   rC   r7   r8   rK   rM   rG    rN   r)   r   r      sQ    "*%X#GJ2
2
(')".''

	]rN   r   )r_   r   rc   r   r   r   r2   r   r   repl_widgetr   exception_widgetr	   r   r   r   rN   r)   <module>r      s3    	 
    " # 4f]I%% f]rN   