-
Notifications
You must be signed in to change notification settings - Fork 20
/
pyquirks.py
56 lines (45 loc) · 1.88 KB
/
pyquirks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import logging
import importlib.util
try:
from PyQt6 import QtWidgets
except ImportError:
print("PyQt5 fallback (pyquirks.py)")
from PyQt5 import QtWidgets
from quirks import ScriptQuirks
PchumLog = logging.getLogger("pchumLogger")
class PythonQuirks(ScriptQuirks):
def loadModule(self, name, filename):
# imp is deprecated since Python 3.4
# return imp.load_source(name, filename)
spec = importlib.util.spec_from_file_location(name, filename)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def getExtension(self):
return ".py"
def modHas(self, module, attr):
if attr == "commands":
variables = vars(module)
for name, obj in variables.items():
if self.modHas(obj, "command"):
return True
return hasattr(module, attr)
def register(self, module):
variables = vars(module)
for name, obj in variables.items():
if self.modHas(obj, "command"):
try:
if not isinstance(obj("test"), str):
raise Exception
except:
PchumLog.error("Quirk malformed: %s", obj.command)
# Since this is executed before QApplication is constructed,
# This prevented pesterchum from starting entirely when a quirk was malformed :/
# (QWidget: Must construct a QApplication before a QWidget)
if QtWidgets.QApplication.instance() is not None:
msgbox = QtWidgets.QMessageBox()
msgbox.setWindowTitle("Error!")
msgbox.setText("Quirk malformed: %s" % (obj.command))
msgbox.exec()
else:
self.quirks[obj.command] = obj