This commit is contained in:
François Dausseur
2023-04-26 16:55:17 +02:00
parent 36a369086d
commit 73c07adf5f

View File

@@ -1,126 +0,0 @@
#!/usr/bin/python3
import os
from configparser import ConfigParser
import logging
from systemd import journal
import inspect
from importlib import import_module
from commands import Commands
from threading import Lock
class CommandsLoader:
def __init__(self, config: ConfigParser, log: logging.Handler, modpath: str):
self.config = config
self.modpath = modpath
Commands.defmod = self.config['general'].get('default')
self.log = log
self.lock = Lock()
self._load_commands()
self._set_cmods()
self._load_dependencies()
def _load_commands(self):
cmds = {}
with os.scandir(self.modpath) as it:
for entry in it:
if entry.name.startswith('cmds_') and entry.name.endswith('.py') and entry.is_file():
obj = self._load_module(entry.name[:-3])
if not obj is None:
nmod = obj.nickname
if nmod is None:
nmod = (entry.name[:-3])[5:]
cmds.update({nmod: obj})
self.log.info('module "{}" loaded'.format(nmod))
else:
self.log.error(
'The module "{}" could not be loaded'.format(entry.name[:-3]))
self.cmods = cmds
def _load_module(self, name):
module = import_module("."+name, 'commands')
members = inspect.getmembers(module, inspect.isclass)
conf = None
if name[5:] in self.config.sections():
conf = self.config[name[5:]]
obj = None
for n, c in members:
if issubclass(c, Commands) and (n != 'Commands'):
obj = c(conf, self.log)
obj.log = self.log
obj.lock = self.lock
break
return obj
def _set_cmods(self):
for k, v in self.cmods.items():
v.cmods = self.cmods
def _load_dependencies(self):
for k, v in self.cmods.items():
if hasattr(v, "dependencies"):
for p in v.dependencies:
if p in self.cmods.keys():
setattr(v, p, self.cmods[p])
else:
self.log.error(
'Dependency "{}" of module "{}" could not be satisfied'.format(p, k))
def start(self):
for k, v in self.cmods.items():
if v.threaded:
v.start()
def join(self):
for k, v in self.cmods.items():
if v.threaded:
v.join()
class AppEngine:
def __init__(self, app_name: str, conf_file : str="", mod_dir: str="", log_file: str="", debug: bool=False) -> None:
self.app_name = app_name
self.conf = ConfigParser()
if conf_file != '':
self.parse_config(conf_file)
self.def_log(log_file)
if debug:
self.log.setLevel(logging.DEBUG)
else:
self.log.setLevel(logging.WARNING)
def parse_config(self, cf: str):
if os.path.exists(cf) and os.path.isfile(cf):
self.conf.read(cf)
else:
raise Exception('Configuration file not found')
def def_log(self, log_file):
self.log = logging.getLogger(self.app_name)
if log_file != '':
fname = log_file
is_writeable = False
if not os.path.isabs(fname):
fname = os.path.join(os.getcwd(), fname)
if os.access(os.path.dirname(fname), os.W_OK):
is_writeable = True
if is_writeable:
self.log.addHandler(logging.FileHandler(fname))
else:
self.log.addHandler(journal.JournalHandler())
self.log.error('No write permissions: "{}"'.format(fname))
else:
self.log.addHandler(journal.JournalHandler())
def exec(self, modpath: str=""):
cl = CommandsLoader(self.conf, self.log, modpath)
cl.start()
cl.join()