pesterchum/luaquirks.py

74 lines
2.3 KiB
Python
Raw Normal View History

import logging, logging.config
2021-12-01 12:29:17 -05:00
import ostools
_datadir = ostools.getDataDir()
logging.config.fileConfig(_datadir + "logging.ini")
PchumLog = logging.getLogger('pchumLogger')
import os, sys, re, ostools
2012-11-12 23:52:26 -05:00
try:
import lua
except ImportError:
lua = None
from quirks import ScriptQuirks
2021-03-23 17:36:43 -04:00
from PyQt5 import QtCore, QtGui, QtWidgets
2012-11-12 23:52:26 -05:00
class LuaQuirks(ScriptQuirks):
def loadModule(self, name, filename):
if lua is None:
return None
lua.globals().package.loaded[name] = None
2013-02-26 01:02:30 -05:00
CurrentDir = os.getcwd()
os.chdir('quirks')
try:
return lua.require(name)
except Error as e:
PchumLog.error(e)
return None
finally:
os.chdir(CurrentDir)
2012-11-12 23:52:26 -05:00
def getExtension(self):
return '.lua'
def modHas(self, module, attr):
return module[attr] is not None
def register(self, module):
class Wrapper(object):
def __init__(self, module, name):
self.module = module
self.name = name
def __call__(self, text):
CurrentDir = os.getcwd()
os.chdir('quirks')
try:
return self.module.commands[self.name](lua.globals().tostring(text))
except:
return None
finally:
os.chdir(CurrentDir)
2012-11-12 23:52:26 -05:00
for name in module.commands:
CommandWrapper = Wrapper(module,name)
2012-11-12 23:52:26 -05:00
try:
2021-03-23 17:36:43 -04:00
if not isinstance(CommandWrapper("test"), str):
2012-11-12 23:52:26 -05:00
raise Exception
except:
#print("Quirk malformed: %s" % (name))
PchumLog.error("Quirk malformed: %s" % (name))
# 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" % (name))
msgbox.exec_()
2012-11-12 23:52:26 -05:00
else:
self.quirks[name] = CommandWrapper
2012-11-12 23:52:26 -05:00