from pyqtgraph.Qt import QtCore, QtGui
import sys, re, os, time, traceback, subprocess
import pyqtgraph as pg
from . import template
import pyqtgraph.exceptionHandling as exceptionHandling
import pickle
class ConsoleWidget(QtGui.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}
============ =============================================================================
"""
QtGui.QWidget.__init__(self, parent)
if namespace is None:
namespace = {}
self.localNamespace = namespace
self.editor = editor
self.multiline = None
self.inCmd = False
self.ui = template.Ui_Form()
self.ui.setupUi(self)
self.output = self.ui.output
self.input = self.ui.input
self.input.setFocus()
if text is not None:
self.output.setPlainText(text)
self.historyFile = historyFile
history = self.loadHistory()
if history is not None:
self.input.history = [""] + history
self.ui.historyList.addItems(history[::-1])
self.ui.historyList.hide()
self.ui.exceptionGroup.hide()
self.input.sigExecuteCmd.connect(self.runCmd)
self.ui.historyBtn.toggled.connect(self.ui.historyList.setVisible)
self.ui.historyList.itemClicked.connect(self.cmdSelected)
self.ui.historyList.itemDoubleClicked.connect(self.cmdDblClicked)
self.ui.exceptionBtn.toggled.connect(self.ui.exceptionGroup.setVisible)
self.ui.catchAllExceptionsBtn.toggled.connect(self.catchAllExceptions)
self.ui.catchNextExceptionBtn.toggled.connect(self.catchNextException)
self.ui.clearExceptionBtn.clicked.connect(self.clearExceptionClicked)
self.ui.exceptionStackList.itemClicked.connect(self.stackItemClicked)
self.ui.exceptionStackList.itemDoubleClicked.connect(self.stackItemDblClicked)
self.exceptionHandlerRunning = False
self.currentTraceback = None
def loadHistory(self):
"""Return the list of previously-invoked command strings (or None)."""
if self.historyFile is not None:
return pickle.load(open(self.historyFile, 'rb'))
def saveHistory(self, history):
"""Store the list of previously-invoked command strings."""
if self.historyFile is not None:
pickle.dump(open(self.historyFile, 'wb'), history)
def runCmd(self, cmd):
#cmd = str(self.input.lastCmd)
self.stdout = sys.stdout
self.stderr = sys.stderr
encCmd = re.sub(r'>', '>', re.sub(r'<', '<', cmd))
encCmd = re.sub(r' ', ' ', encCmd)
self.ui.historyList.addItem(cmd)
self.saveHistory(self.input.history[1:100])
try:
sys.stdout = self
sys.stderr = self
if self.multiline is not None:
self.write("
%s\n"%encCmd, html=True)
self.execMulti(cmd)
else:
self.write("