Coverage for lib/lottie/gui/console.py: 0%
62 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-20 16:17 +0100
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-20 16:17 +0100
1import io
2import sys
3import inspect
4import traceback
5import contextlib
7from PyQt5.QtWidgets import *
10class Console(QWidget):
11 def __init__(self, *a):
12 super().__init__(*a)
13 self.context_global = {}
14 self.context_local = {}
16 layout = QVBoxLayout()
17 self.setLayout(layout)
19 self.lines = QPlainTextEdit()
20 self.lines.setReadOnly(True)
21 layout.addWidget(self.lines)
23 self.input = QLineEdit()
24 self.input.returnPressed.connect(self.on_input)
25 layout.addWidget(self.input)
27 def set_font(self, font):
28 self.lines.setFont(font)
29 self.input.setFont(font)
31 def define(self, name, value):
32 self.context_global[name] = value
34 def on_input(self):
35 self.eval(self.input.text())
36 self.input.clear()
38 def eval(self, line):
39 self.lines.appendPlainText(">>> " + line)
41 try:
42 try:
43 expression = True
44 code = compile(line, "<console>", "eval")
45 except SyntaxError:
46 expression = False
47 code = compile(line, "<console>", "single")
49 sterrout = io.StringIO()
50 with contextlib.redirect_stderr(sterrout):
51 with contextlib.redirect_stdout(sterrout):
52 if expression:
53 value = eval(code, self.context_global, self.context_local)
54 else:
55 value = None
56 exec(code, self.context_global, self.context_local)
58 stdstreams = sterrout.getvalue()
59 if stdstreams:
60 if stdstreams.endswith("\n"):
61 stdstreams = stdstreams[:-1]
62 self.lines.appendPlainText(stdstreams)
63 if value is not None:
64 self.lines.appendPlainText(repr(value))
65 except Exception:
66 etype, value, tb = sys.exc_info()
67 # Find how many frames to print to hide the current frame and above
68 parent = inspect.currentframe().f_back
69 i = 0
70 for frame, ln in traceback.walk_tb(tb):
71 i += 1
72 if frame.f_back == parent:
73 i = 0
75 file = io.StringIO()
76 traceback.print_exception(etype, value, tb, limit=-i, file=file)
77 self.lines.appendPlainText(file.getvalue())
79 self.lines.verticalScrollBar().setValue(self.lines.verticalScrollBar().maximum())