python-lottie  0.7.0+dev66cafb9
A framework to work with lottie files and telegram animated stickers (tgs)
console.py
Go to the documentation of this file.
1 import io
2 import sys
3 import inspect
4 import traceback
5 import contextlib
6 
7 from PyQt5.QtWidgets import *
8 
9 
10 class Console(QWidget):
11  def __init__(self, *a):
12  super().__init__(*a)
13  self.context_globalcontext_global = {}
14  self.context_localcontext_local = {}
15 
16  layout = QVBoxLayout()
17  self.setLayout(layout)
18 
19  self.lineslines = QPlainTextEdit()
20  self.lineslines.setReadOnly(True)
21  layout.addWidget(self.lineslines)
22 
23  self.inputinput = QLineEdit()
24  self.inputinput.returnPressed.connect(self.on_inputon_input)
25  layout.addWidget(self.inputinput)
26 
27  def set_font(self, font):
28  self.lineslines.setFont(font)
29  self.inputinput.setFont(font)
30 
31  def define(self, name, value):
32  self.context_globalcontext_global[name] = value
33 
34  def on_input(self):
35  self.evaleval(self.inputinput.text())
36  self.inputinput.clear()
37 
38  def eval(self, line):
39  self.lineslines.appendPlainText(">>> " + line)
40 
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")
48 
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_globalcontext_global, self.context_localcontext_local)
54  else:
55  value = None
56  exec(code, self.context_globalcontext_global, self.context_localcontext_local)
57 
58  stdstreams = sterrout.getvalue()
59  if stdstreams:
60  if stdstreams.endswith("\n"):
61  stdstreams = stdstreams[:-1]
62  self.lineslines.appendPlainText(stdstreams)
63  if value is not None:
64  self.lineslines.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
74 
75  file = io.StringIO()
76  traceback.print_exception(etype, value, tb, limit=-i, file=file)
77  self.lineslines.appendPlainText(file.getvalue())
78 
79  self.lineslines.verticalScrollBar().setValue(self.lineslines.verticalScrollBar().maximum())
def define(self, name, value)
Definition: console.py:31
def eval(self, line)
Definition: console.py:38
def __init__(self, *a)
Definition: console.py:11
def set_font(self, font)
Definition: console.py:27