2022-03-19 19:48:19 -04:00
|
|
|
import logging
|
|
|
|
import logging.config
|
2022-03-23 12:46:19 -04:00
|
|
|
import importlib.util
|
2022-03-19 19:48:19 -04:00
|
|
|
|
2022-08-19 07:12:58 -04:00
|
|
|
try:
|
|
|
|
from PyQt6 import QtWidgets
|
|
|
|
except ImportError:
|
|
|
|
print("PyQt5 fallback (pyquirks.py)")
|
|
|
|
from PyQt5 import QtWidgets
|
2022-03-19 19:48:19 -04:00
|
|
|
|
2021-12-01 12:29:17 -05:00
|
|
|
import ostools
|
2022-03-19 19:48:19 -04:00
|
|
|
from quirks import ScriptQuirks
|
|
|
|
|
2021-12-01 12:29:17 -05:00
|
|
|
_datadir = ostools.getDataDir()
|
|
|
|
logging.config.fileConfig(_datadir + "logging.ini")
|
2021-08-10 16:45:48 -04:00
|
|
|
PchumLog = logging.getLogger('pchumLogger')
|
2011-06-07 11:48:35 -04:00
|
|
|
|
2012-11-12 23:52:26 -05:00
|
|
|
class PythonQuirks(ScriptQuirks):
|
|
|
|
def loadModule(self, name, filename):
|
2022-03-23 12:46:19 -04:00
|
|
|
# imp is depreciated 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
|
|
|
|
|
2012-11-12 23:52:26 -05:00
|
|
|
def getExtension(self):
|
|
|
|
return '.py'
|
2011-06-07 11:48:35 -04:00
|
|
|
|
2012-11-12 23:52:26 -05:00
|
|
|
def modHas(self, module, attr):
|
|
|
|
if attr == 'commands':
|
|
|
|
variables = vars(module)
|
2021-03-23 17:36:43 -04:00
|
|
|
for name, obj in variables.items():
|
2012-11-12 23:52:26 -05:00
|
|
|
if self.modHas(obj, 'command'):
|
|
|
|
return True
|
|
|
|
return hasattr(module, attr)
|
2011-06-07 11:48:35 -04:00
|
|
|
|
2012-11-12 23:52:26 -05:00
|
|
|
def register(self, module):
|
|
|
|
variables = vars(module)
|
2021-03-23 17:36:43 -04:00
|
|
|
for name, obj in variables.items():
|
2012-11-12 23:52:26 -05:00
|
|
|
if self.modHas(obj, 'command'):
|
2011-06-07 11:48:35 -04:00
|
|
|
try:
|
2021-03-23 17:36:43 -04:00
|
|
|
if not isinstance(obj("test"), str):
|
2011-06-07 11:48:35 -04:00
|
|
|
raise Exception
|
|
|
|
except:
|
2021-03-25 13:41:07 -04:00
|
|
|
#print("Quirk malformed: %s" % (obj.command))
|
2021-08-10 16:45:48 -04:00
|
|
|
PchumLog.error("Quirk malformed: %s" % (obj.command))
|
2021-03-25 13:41:07 -04:00
|
|
|
|
|
|
|
# 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() != None:
|
|
|
|
msgbox = QtWidgets.QMessageBox()
|
|
|
|
msgbox.setWindowTitle("Error!")
|
|
|
|
msgbox.setText("Quirk malformed: %s" % (obj.command))
|
2022-06-26 22:18:37 -04:00
|
|
|
msgbox.exec()
|
2011-06-07 11:48:35 -04:00
|
|
|
else:
|
2012-11-12 23:52:26 -05:00
|
|
|
self.quirks[obj.command] = obj
|
2011-06-07 11:48:35 -04:00
|
|
|
|