1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
import os
import sys
import pickle
import subprocess
from .. import getConfigOption
from ..Qt import QtCore, QtWidgets
from .repl_widget import ReplWidget
from .exception_widget import ExceptionHandlerWidget
class ConsoleWidget(QtWidgets.QWidget):
"""
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
"""
def __init__(self, parent=None, namespace=None, historyFile=None, text=None, editor=None):
"""
============== =============================================================================
**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}
============== =============================================================================
"""
QtWidgets.QWidget.__init__(self, parent)
self._setupUi()
if namespace is None:
namespace = {}
namespace['__console__'] = self
self.localNamespace = namespace
self.editor = editor
self.output = self.repl.output
self.input = self.repl.input
self.input.setFocus()
if text is not None:
self.output.setPlainText(text)
self.historyFile = historyFile
try:
history = self.loadHistory()
except Exception as exc:
sys.excepthook(*sys.exc_info())
history = None
if history is not None:
self.input.history = [""] + history
self.historyList.addItems(history[::-1])
self.currentTraceback = None
def _setupUi(self):
self.layout = QtWidgets.QGridLayout(self)
self.setLayout(self.layout)
self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setSpacing(0)
self.splitter = QtWidgets.QSplitter(QtCore.Qt.Orientation.Vertical, self)
self.layout.addWidget(self.splitter, 0, 0)
self.repl = ReplWidget(self.globals, self.locals, self)
self.splitter.addWidget(self.repl)
self.historyList = QtWidgets.QListWidget(self)
self.historyList.hide()
self.splitter.addWidget(self.historyList)
self.historyBtn = QtWidgets.QPushButton('History', self)
self.historyBtn.setCheckable(True)
self.repl.inputLayout.addWidget(self.historyBtn)
self.repl.sigCommandEntered.connect(self._commandEntered)
self.repl.sigCommandRaisedException.connect(self._commandRaisedException)
self.excHandler = ExceptionHandlerWidget(self)
self.excHandler.hide()
self.splitter.addWidget(self.excHandler)
self.exceptionBtn = QtWidgets.QPushButton("Exceptions..", self)
self.exceptionBtn.setCheckable(True)
self.repl.inputLayout.addWidget(self.exceptionBtn)
self.excHandler.sigStackItemDblClicked.connect(self._stackItemDblClicked)
self.exceptionBtn.toggled.connect(self.excHandler.setVisible)
self.historyBtn.toggled.connect(self.historyList.setVisible)
self.historyList.itemClicked.connect(self.cmdSelected)
self.historyList.itemDoubleClicked.connect(self.cmdDblClicked)
def catchAllExceptions(self, catch=True):
if catch:
self.exceptionBtn.setChecked(True)
self.excHandler.catchAllExceptions(catch)
def catchNextException(self, catch=True):
if catch:
self.exceptionBtn.setChecked(True)
self.excHandler.catchNextException(catch)
def setStack(self, frame=None):
self.excHandler.setStack(frame)
def loadHistory(self):
"""Return the list of previously-invoked command strings (or None)."""
if self.historyFile is not None and os.path.exists(self.historyFile):
with open(self.historyFile, 'rb') as pf:
return pickle.load(pf)
def saveHistory(self, history):
"""Store the list of previously-invoked command strings."""
if self.historyFile is not None:
with open(self.historyFile, 'wb') as pf:
pickle.dump(history, pf)
def _commandEntered(self, repl, cmd):
self.historyList.addItem(cmd)
self.saveHistory(self.input.history[1:100])
sb = self.historyList.verticalScrollBar()
sb.setValue(sb.maximum())
def _commandRaisedException(self, repl, exc):
self.excHandler.exceptionHandler(exc)
def globals(self):
frame = self.excHandler.selectedFrame()
if frame is not None and self.excHandler.runSelectedFrameCheck.isChecked():
return frame.f_globals
else:
return self.localNamespace
def locals(self):
frame = self.excHandler.selectedFrame()
if frame is not None and self.excHandler.runSelectedFrameCheck.isChecked():
return frame.f_locals
else:
return self.localNamespace
def cmdSelected(self, item):
index = -(self.historyList.row(item)+1)
self.input.setHistory(index)
self.input.setFocus()
def cmdDblClicked(self, item):
index = -(self.historyList.row(item)+1)
self.input.setHistory(index)
self.input.execCmd()
def _stackItemDblClicked(self, handler, item):
editor = self.editor
if editor is None:
editor = getConfigOption('editorCommand')
if editor is None:
return
tb = self.excHandler.selectedFrame()
lineNum = tb.f_lineno
fileName = tb.f_code.co_filename
subprocess.Popen(self.editor.format(fileName=fileName, lineNum=lineNum), shell=True)
|