From 50439e59f52f3ab2ebd1174b84d4b6fed21332ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Dausseur?= Date: Fri, 26 Apr 2024 19:44:25 +0200 Subject: [PATCH] added a help_module function and renamed some variables to ease integration with "cmd" module. --- VERSION | 2 +- src/appengine/__init__.py | 45 +++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/VERSION b/VERSION index ceab6e1..2f45361 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.1 \ No newline at end of file +0.2 \ No newline at end of file diff --git a/src/appengine/__init__.py b/src/appengine/__init__.py index 61b0b31..97bc4c0 100644 --- a/src/appengine/__init__.py +++ b/src/appengine/__init__.py @@ -54,7 +54,7 @@ class AppEngineException(Exception): class Commands(Thread): defmod = None - precmd = "cmd_" + prefcmd = "cmd_" def __init__(self, config: ConfigParser, log: logging.Handler) -> None: super().__init__() @@ -110,15 +110,24 @@ class Commands(Thread): ret = ret + " " + module + "\n" 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: success = False ret = (AEErrs.INTERNAL_ERROR.value, "function not found") - if hasattr(self, self.precmd + method) and inspect.ismethod( - getattr(self, self.precmd + method) + if hasattr(self, self.prefcmd + method) and inspect.ismethod( + getattr(self, self.prefcmd + method) ): self.lock.acquire() try: - f = getattr(self, self.precmd + method) + f = getattr(self, self.prefcmd + method) ret = f(*args, **kwargs) success = True self.log.info( @@ -181,12 +190,12 @@ class Commands(Thread): if len(args) == 0: 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: - ret = ret + m[0][len(self.precmd):] + "\n" + ret = ret + m[0][len(self.prefcmd):] + "\n" else: if isinstance(args[0], str): - cmd = self.precmd + args[0] + cmd = self.prefcmd + args[0] c = getattr(self, cmd, None) if c: r = inspect.getdoc(c) @@ -207,13 +216,13 @@ class CommandsLoader: self.modpath = modpath sys.path.append(os.path.join(modpath)) Commands.defmod = self.config["general"].get("default") - precmd = self.config["general"].get("methods_prefix") - if not precmd is None: - Commands.precmd = precmd - self.precmds = "cmds_" - precmds = self.config["general"].get("modules_prefix") - if not precmds is None: - self.precmds = precmds + prefcmd = self.config["general"].get("methods_prefix") + if not prefcmd is None: + Commands.prefcmd = prefcmd + self.prefcmds = "cmds_" + prefcmds = self.config["general"].get("modules_prefix") + if not prefcmds is None: + self.prefcmds = prefcmds self.log = log self.lock = Lock() self._load_commands() @@ -224,7 +233,7 @@ class CommandsLoader: with os.scandir(self.modpath) as it: for entry in it: if ( - entry.name.startswith(self.precmds) + entry.name.startswith(self.prefcmds) and entry.name.endswith(".py") and entry.is_file() ): @@ -232,7 +241,7 @@ class CommandsLoader: if not obj is None: nmod = obj.nickname if nmod is None: - nmod = (entry.name[:-3])[len(self.precmds):] + nmod = (entry.name[:-3])[len(self.prefcmds):] cmds.update({nmod: obj}) self.log.info('module "{}" loaded'.format(nmod)) else: @@ -251,8 +260,8 @@ class CommandsLoader: members = inspect.getmembers(module, inspect.isclass) conf = None - if name[len(self.precmds):] in self.config.sections(): - conf = self.config[name[len(self.precmds):]] + if name[len(self.prefcmds):] in self.config.sections(): + conf = self.config[name[len(self.prefcmds):]] obj = None for n, c in members: