From edf861c9852464bba8d46ac68381f03d4f21b2da Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Sun, 5 Apr 2020 14:17:54 +0200 Subject: [PATCH] jwutils.Cmds: Fix empty to cmds member variable Cmds.cmds was sometimes empty, so fix that and make sure it points to the Cmds instance it was crated by. Actually the name of the Cmds class is a bad pick, should have been App or something, so to add .app, too, as a path for future compatibility. Signed-off-by: Jan Lindemann --- tools/python/jwutils/Cmds.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tools/python/jwutils/Cmds.py b/tools/python/jwutils/Cmds.py index b2e2cf2..c604fd2 100644 --- a/tools/python/jwutils/Cmds.py +++ b/tools/python/jwutils/Cmds.py @@ -12,12 +12,18 @@ from jwutils import log class Cmds: # export + def __instantiate(self, cls): + r = cls() + r.cmds = self # Rename Cmds class to App, "Cmds" isn't very self-explanatory + r.app = self + return r + def __add_cmd_to_parser(self, cmd, parsers): parser = cmd.add_parser(parsers) cmd.add_arguments(parser) if len(cmd.child_classes) > len(cmd.children): for c in cmd.child_classes: - cmd.children.append(c()) + cmd.children.append(self.__instantiate(c)) for sub_cmd in cmd.children: subparsers = parser.add_subparsers(title='Available commands of ' + cmd.name, metavar='') self.__add_cmd_to_parser(sub_cmd, subparsers) @@ -64,8 +70,7 @@ class Cmds: # export if inspect.isabstract(c): continue log.slog(log.DEBUG, 'instantiating command "{}"'.format(c)) - cmd = c() - cmd.cmds = self + cmd = self.__instantiate(c) #cmd.add_parser(subparsers) self.__cmds.append(cmd) for child in cmd.child_classes: