2021-03-25 13:41:07 -04:00
|
|
|
import os, sys, re, ostools, logging
|
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
|
2013-02-10 12:49:44 -05:00
|
|
|
|
|
|
|
lua.globals().package.loaded[name] = None
|
2013-02-26 01:02:30 -05:00
|
|
|
|
2013-02-10 12:49:44 -05:00
|
|
|
CurrentDir = os.getcwd()
|
|
|
|
os.chdir('quirks')
|
|
|
|
try:
|
|
|
|
return lua.require(name)
|
|
|
|
except Error as e:
|
2021-04-10 19:21:21 -04:00
|
|
|
logging.error(e)
|
2013-02-10 12:49:44 -05:00
|
|
|
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):
|
2013-02-10 12:49:44 -05:00
|
|
|
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:
|
2013-02-10 12:49:44 -05:00
|
|
|
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:
|
2021-03-25 13:41:07 -04:00
|
|
|
#print("Quirk malformed: %s" % (name))
|
|
|
|
logging.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:
|
2013-02-10 12:49:44 -05:00
|
|
|
self.quirks[name] = CommandWrapper
|
2012-11-12 23:52:26 -05:00
|
|
|
|