python-lottie  0.6.11+devcecd248
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 parser
4 import inspect
5 import traceback
6 import contextlib
7 
8 from PyQt5.QtWidgets import *
9 
10 
11 class Console(QWidget):
12  def __init__(self, *a):
13  super().__init__(*a)
14  self.context_global = {}
15  self.context_local = {}
16 
17  layout = QVBoxLayout()
18  self.setLayout(layout)
19 
20  self.lines = QPlainTextEdit()
21  self.lines.setReadOnly(True)
22  layout.addWidget(self.lines)
23 
24  self.input = QLineEdit()
25  self.input.returnPressed.connect(self.on_input)
26  layout.addWidget(self.input)
27 
28  def set_font(self, font):
29  self.lines.setFont(font)
30  self.input.setFont(font)
31 
32  def define(self, name, value):
33  self.context_global[name] = value
34 
35  def on_input(self):
36  self.eval(self.input.text())
37  self.input.clear()
38 
39  def eval(self, line):
40  self.lines.appendPlainText(">>> " + line)
41 
42  try:
43  try:
44  expression = True
45  code = compile(line, "<console>", "eval")
46  except SyntaxError:
47  expression = False
48  code = compile(line, "<console>", "single")
49 
50  sterrout = io.StringIO()
51  with contextlib.redirect_stderr(sterrout):
52  with contextlib.redirect_stdout(sterrout):
53  if expression:
54  value = eval(code, self.context_global, self.context_local)
55  else:
56  value = None
57  exec(code, self.context_global, self.context_local)
58 
59  stdstreams = sterrout.getvalue()
60  if stdstreams:
61  if stdstreams.endswith("\n"):
62  stdstreams = stdstreams[:-1]
63  self.lines.appendPlainText(stdstreams)
64  if value is not None:
65  self.lines.appendPlainText(repr(value))
66  except Exception:
67  etype, value, tb = sys.exc_info()
68  # Find how many frames to print to hide the current frame and above
69  parent = inspect.currentframe().f_back
70  i = 0
71  for frame, ln in traceback.walk_tb(tb):
72  i += 1
73  if frame.f_back == parent:
74  i = 0
75 
76  file = io.StringIO()
77  traceback.print_exception(etype, value, tb, limit=-i, file=file)
78  self.lines.appendPlainText(file.getvalue())
79 
80  self.lines.verticalScrollBar().setValue(self.lines.verticalScrollBar().maximum())
lottie.gui.console.Console.lines
lines
Definition: console.py:20
lottie.gui.console.Console.eval
def eval(self, line)
Definition: console.py:39
lottie.gui.console.Console.input
input
Definition: console.py:24
lottie.gui.console.Console.context_local
context_local
Definition: console.py:15
lottie.gui.console.Console.context_global
context_global
Definition: console.py:14
lottie.gui.console.Console
Definition: console.py:11
lottie.gui.console.Console.on_input
def on_input(self)
Definition: console.py:35
lottie.gui.console.Console.__init__
def __init__(self, *a)
Definition: console.py:12
lottie.gui.console.Console.define
def define(self, name, value)
Definition: console.py:32
lottie.gui.console.Console.set_font
def set_font(self, font)
Definition: console.py:28
QtWidgets