added a help_module function and renamed some

variables to ease integration with
"cmd" module.
This commit is contained in:
François Dausseur
2024-04-26 19:44:25 +02:00
parent 4449d29a59
commit 50439e59f5
2 changed files with 28 additions and 19 deletions

View File

@@ -1 +1 @@
0.1 0.2

View File

@@ -54,7 +54,7 @@ class AppEngineException(Exception):
class Commands(Thread): class Commands(Thread):
defmod = None defmod = None
precmd = "cmd_" prefcmd = "cmd_"
def __init__(self, config: ConfigParser, log: logging.Handler) -> None: def __init__(self, config: ConfigParser, log: logging.Handler) -> None:
super().__init__() super().__init__()
@@ -110,15 +110,24 @@ class Commands(Thread):
ret = ret + " " + module + "\n" ret = ret + " " + module + "\n"
return success, ret.strip() return success, ret.strip()
def help_module(self, module, *args):
if module in self.cmods.keys():
if len(args) > 0:
return self.cmods[module].cmd_help(args[0])
else:
return self.cmods[module].cmd_help()
else:
return "No module with this name"
def _execute_command(self, method: str, *args, **kwargs) -> tuple: def _execute_command(self, method: str, *args, **kwargs) -> tuple:
success = False success = False
ret = (AEErrs.INTERNAL_ERROR.value, "function not found") ret = (AEErrs.INTERNAL_ERROR.value, "function not found")
if hasattr(self, self.precmd + method) and inspect.ismethod( if hasattr(self, self.prefcmd + method) and inspect.ismethod(
getattr(self, self.precmd + method) getattr(self, self.prefcmd + method)
): ):
self.lock.acquire() self.lock.acquire()
try: try:
f = getattr(self, self.precmd + method) f = getattr(self, self.prefcmd + method)
ret = f(*args, **kwargs) ret = f(*args, **kwargs)
success = True success = True
self.log.info( self.log.info(
@@ -181,12 +190,12 @@ class Commands(Thread):
if len(args) == 0: if len(args) == 0:
cmds = inspect.getmembers(self, predicate=inspect.ismethod) cmds = inspect.getmembers(self, predicate=inspect.ismethod)
cmds = [x for x in cmds if x[0].startswith(self.precmd)] cmds = [x for x in cmds if x[0].startswith(self.prefcmd)]
for m in cmds: for m in cmds:
ret = ret + m[0][len(self.precmd):] + "\n" ret = ret + m[0][len(self.prefcmd):] + "\n"
else: else:
if isinstance(args[0], str): if isinstance(args[0], str):
cmd = self.precmd + args[0] cmd = self.prefcmd + args[0]
c = getattr(self, cmd, None) c = getattr(self, cmd, None)
if c: if c:
r = inspect.getdoc(c) r = inspect.getdoc(c)
@@ -207,13 +216,13 @@ class CommandsLoader:
self.modpath = modpath self.modpath = modpath
sys.path.append(os.path.join(modpath)) sys.path.append(os.path.join(modpath))
Commands.defmod = self.config["general"].get("default") Commands.defmod = self.config["general"].get("default")
precmd = self.config["general"].get("methods_prefix") prefcmd = self.config["general"].get("methods_prefix")
if not precmd is None: if not prefcmd is None:
Commands.precmd = precmd Commands.prefcmd = prefcmd
self.precmds = "cmds_" self.prefcmds = "cmds_"
precmds = self.config["general"].get("modules_prefix") prefcmds = self.config["general"].get("modules_prefix")
if not precmds is None: if not prefcmds is None:
self.precmds = precmds self.prefcmds = prefcmds
self.log = log self.log = log
self.lock = Lock() self.lock = Lock()
self._load_commands() self._load_commands()
@@ -224,7 +233,7 @@ class CommandsLoader:
with os.scandir(self.modpath) as it: with os.scandir(self.modpath) as it:
for entry in it: for entry in it:
if ( if (
entry.name.startswith(self.precmds) entry.name.startswith(self.prefcmds)
and entry.name.endswith(".py") and entry.name.endswith(".py")
and entry.is_file() and entry.is_file()
): ):
@@ -232,7 +241,7 @@ class CommandsLoader:
if not obj is None: if not obj is None:
nmod = obj.nickname nmod = obj.nickname
if nmod is None: if nmod is None:
nmod = (entry.name[:-3])[len(self.precmds):] nmod = (entry.name[:-3])[len(self.prefcmds):]
cmds.update({nmod: obj}) cmds.update({nmod: obj})
self.log.info('module "{}" loaded'.format(nmod)) self.log.info('module "{}" loaded'.format(nmod))
else: else:
@@ -251,8 +260,8 @@ class CommandsLoader:
members = inspect.getmembers(module, inspect.isclass) members = inspect.getmembers(module, inspect.isclass)
conf = None conf = None
if name[len(self.precmds):] in self.config.sections(): if name[len(self.prefcmds):] in self.config.sections():
conf = self.config[name[len(self.precmds):]] conf = self.config[name[len(self.prefcmds):]]
obj = None obj = None
for n, c in members: for n, c in members: