added a help_module function and renamed some
variables to ease integration with "cmd" module.
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user