2022-03-19 19:48:19 -04:00
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
2021-12-01 12:29:17 -05:00
|
|
|
import ostools
|
2022-03-19 19:48:19 -04:00
|
|
|
|
2021-12-01 12:29:17 -05:00
|
|
|
_datadir = ostools.getDataDir()
|
2022-10-07 16:51:40 -04:00
|
|
|
PchumLog = logging.getLogger("pchumLogger")
|
|
|
|
|
2012-11-12 23:52:26 -05:00
|
|
|
|
|
|
|
class ScriptQuirks(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._datadir = ostools.getDataDir()
|
|
|
|
self.home = os.getcwd()
|
|
|
|
self.quirks = {}
|
|
|
|
self.last = {}
|
|
|
|
self.scripts = []
|
2022-10-07 16:51:40 -04:00
|
|
|
# self.load()
|
2012-11-12 23:52:26 -05:00
|
|
|
|
|
|
|
def loadModule(self, name, filename):
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
def modHas(self, module, attr):
|
|
|
|
return False
|
|
|
|
|
|
|
|
def loadAll(self):
|
|
|
|
self.last = self.quirks.copy()
|
|
|
|
self.quirks.clear()
|
|
|
|
for script in self.scripts:
|
2021-08-10 16:45:48 -04:00
|
|
|
PchumLog.info(script.getExtension())
|
2012-11-12 23:52:26 -05:00
|
|
|
script.load()
|
2022-10-07 16:51:40 -04:00
|
|
|
# print script.quirks
|
2012-11-12 23:52:26 -05:00
|
|
|
for q in script.quirks:
|
|
|
|
self.quirks.update(script.quirks)
|
|
|
|
for k in self.last:
|
|
|
|
if k in self.quirks:
|
|
|
|
if self.last[k] == self.quirks[k]:
|
|
|
|
del self.quirks[k]
|
2022-10-07 16:51:40 -04:00
|
|
|
# print self.quirks
|
|
|
|
if hasattr(self, "quirks"):
|
2021-04-11 08:46:11 -04:00
|
|
|
# See https://stackoverflow.com/questions/12843099/python-logging-typeerror-not-all-arguments-converted-during-string-formatting
|
2022-10-07 16:51:40 -04:00
|
|
|
reg_quirks = ("Registered quirks:", "(), ".join(self.quirks) + "()")
|
2021-08-10 16:45:48 -04:00
|
|
|
PchumLog.info(reg_quirks)
|
2012-11-12 23:52:26 -05:00
|
|
|
else:
|
2021-08-10 16:45:48 -04:00
|
|
|
PchumLog.warning("Couldn't find any script quirks")
|
2012-11-12 23:52:26 -05:00
|
|
|
|
|
|
|
def add(self, script):
|
|
|
|
self.scripts.append(script)
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
self.last = self.quirks.copy()
|
|
|
|
self.quirks.clear()
|
|
|
|
extension = self.getExtension()
|
|
|
|
filenames = []
|
2022-10-07 16:51:40 -04:00
|
|
|
if not os.path.exists(os.path.join(self.home, "quirks")):
|
|
|
|
os.makedirs(os.path.join(self.home, "quirks"), exist_ok=True)
|
|
|
|
for fn in os.listdir(os.path.join(self.home, "quirks")):
|
|
|
|
if fn.endswith(extension) and not fn.startswith("_"):
|
|
|
|
filenames.append(os.path.join(self.home, "quirks", fn))
|
|
|
|
if hasattr(self, "_datadir"):
|
|
|
|
if not os.path.exists(os.path.join(self._datadir, "quirks")):
|
|
|
|
os.makedirs(os.path.join(self._datadir, "quirks"), exist_ok=True)
|
|
|
|
for fn in os.listdir(os.path.join(self._datadir, "quirks")):
|
|
|
|
if fn.endswith(extension) and not fn.startswith("_"):
|
|
|
|
filenames.append(os.path.join(self._datadir, "quirks", fn))
|
2012-11-12 23:52:26 -05:00
|
|
|
|
|
|
|
modules = []
|
|
|
|
for filename in filenames:
|
|
|
|
extension_length = len(self.getExtension())
|
|
|
|
name = os.path.basename(filename)[:-extension_length]
|
|
|
|
try:
|
|
|
|
module = self.loadModule(name, filename)
|
|
|
|
if module is None:
|
|
|
|
continue
|
2016-11-29 15:20:41 -05:00
|
|
|
except Exception as e:
|
2022-10-07 16:51:40 -04:00
|
|
|
PchumLog.warning(
|
|
|
|
"Error loading %s: %s (in quirks.py)" % (os.path.basename(name), e)
|
|
|
|
)
|
2012-11-12 23:52:26 -05:00
|
|
|
else:
|
2022-10-07 16:51:40 -04:00
|
|
|
if self.modHas(module, "setup"):
|
2012-11-12 23:52:26 -05:00
|
|
|
module.setup()
|
2022-10-07 16:51:40 -04:00
|
|
|
if self.modHas(module, "commands"):
|
2012-11-12 23:52:26 -05:00
|
|
|
self.register(module)
|
|
|
|
modules.append(name)
|
|
|
|
for k in self.last:
|
|
|
|
if k in self.quirks:
|
|
|
|
if self.last[k] == self.quirks[k]:
|
|
|
|
del self.quirks[k]
|
|
|
|
|
|
|
|
def funcre(self):
|
2022-10-07 16:51:40 -04:00
|
|
|
if not hasattr(self, "quirks"):
|
2012-11-12 23:52:26 -05:00
|
|
|
return r"\\[0-9]+"
|
|
|
|
f = r"("
|
|
|
|
for q in self.quirks:
|
2022-10-07 16:51:40 -04:00
|
|
|
f = f + q + r"\(|"
|
2012-11-12 23:52:26 -05:00
|
|
|
f = f + r"\)|\\[0-9]+)"
|
|
|
|
return f
|