allow statements in debug console

This commit is contained in:
Damien Elmes 2012-04-25 10:12:17 +09:00
parent 8d2a74a6b5
commit 2d11b6e7d7
2 changed files with 73 additions and 28 deletions

View file

@ -885,26 +885,57 @@ will be lost. Continue?"""))
d = self.debugDiag = QDialog() d = self.debugDiag = QDialog()
frm = aqt.forms.debug.Ui_Dialog() frm = aqt.forms.debug.Ui_Dialog()
frm.setupUi(d) frm.setupUi(d)
d.connect(frm.line, SIGNAL("returnPressed()"), lambda: self.onDebugRet(frm)) s = self.debugDiagShort = QShortcut(QKeySequence("ctrl+return"), d)
d.show() self.connect(s, SIGNAL("activated()"),
lambda: self.onDebugRet(frm))
s = self.debugDiagShort = QShortcut(
QKeySequence("ctrl+shift+return"), d)
self.connect(s, SIGNAL("activated()"),
lambda: self.onDebugPrint(frm))
d.exec_()
def _captureOutput(self, on):
mw = self
class Stream(object):
def write(self, data):
mw._output += data
if on:
self._output = ""
self._oldStderr = sys.stderr
self._oldStdout = sys.stdout
s = Stream()
sys.stderr = s
sys.stdout = s
else:
sys.stderr = self._oldStderr
sys.stdout = self._oldStdout
def _debugCard(self):
return self.reviewer.card.__dict__
def onDebugPrint(self, frm):
frm.text.setPlainText("pp(%s)" % frm.text.toPlainText())
self.onDebugRet(frm)
def onDebugRet(self, frm): def onDebugRet(self, frm):
import pprint, traceback import pprint, traceback
line = frm.line.text() text = frm.text.toPlainText()
if not line: card = self._debugCard
return mw = self
def card(): pp = pprint.pprint
return self.reviewer.card.__dict__ self._captureOutput(True)
locals = dict(mw=self, card=card)
newline = "\n"
try: try:
ret = eval(line, globals(), locals) exec text
except Exception, e: except:
newline = "" self._output += traceback.format_exc()
ret = traceback.format_exc() self._captureOutput(False)
if not isinstance(ret, basestring): buf = ""
ret = pprint.pformat(ret) for c, line in enumerate(text.strip().split("\n")):
frm.log.appendPlainText(">>> %s\n%s%s" % (line, ret, newline)) if c == 0:
buf += ">>> %s\n" % line
else:
buf += "... %s\n" % line
frm.log.appendPlainText(buf + (self._output or "<no output>"))
frm.log.ensureCursorVisible() frm.log.ensureCursorVisible()
# System specific code # System specific code

View file

@ -15,21 +15,35 @@
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout"> <widget class="QPlainTextEdit" name="text">
<item> <property name="sizePolicy">
<widget class="QLabel" name="label"> <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<property name="text"> <horstretch>0</horstretch>
<string>Expression:</string> <verstretch>1</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>100</height>
</size>
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QLineEdit" name="line"/>
</item>
</layout>
</item>
<item> <item>
<widget class="QPlainTextEdit" name="log"> <widget class="QPlainTextEdit" name="log">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>8</verstretch>
</sizepolicy>
</property>
<property name="font">
<font>
<family>Courier</family>
<pointsize>14</pointsize>
</font>
</property>
<property name="focusPolicy"> <property name="focusPolicy">
<enum>Qt::ClickFocus</enum> <enum>Qt::ClickFocus</enum>
</property> </property>