[elbe-devel] [PATCH] elbepack: main: untangle run_elbe_subcommand() from main()
Thomas Weißschuh
thomas.weissschuh at linutronix.de
Fri Aug 23 10:23:48 CEST 2024
main() needs to do a few things which are unnecessary for
run_elbe_subcommand(). Instead of passing arguments around to disable
the unnecessary steps, separate the two functions.
This will also make it easier to add more initialization steps to main()
which would create issues to call from run_elbe_subcommand() like the
setup of the logging system.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
elbepack/main.py | 17 ++++++++---------
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/elbepack/main.py b/elbepack/main.py
index ca8469ea14f0..96c5bd71863d 100644
--- a/elbepack/main.py
+++ b/elbepack/main.py
@@ -18,12 +18,14 @@ def get_cmdlist():
return [x for _, x, _ in pkgutil.iter_modules(elbepack.commands.__path__)]
+def _import_cmd_module(cmd):
+ return importlib.import_module('.' + cmd, elbepack.commands.__name__)
+
+
def main(argv=sys.argv):
parser = argparse.ArgumentParser(prog='elbe')
parser.add_argument('--version', action='version', version=f'%(prog)s v{elbe_version}')
parser.add_argument('--stacktrace-on-error', action='store_true', dest='stacktrace_on_error')
- parser.add_argument('--propagate-exception', action='store_true', dest='propagate_exception',
- help=argparse.SUPPRESS)
subparsers = parser.add_subparsers(required=True, dest='cmd')
@@ -34,19 +36,16 @@ def main(argv=sys.argv):
logging.getLogger('suds').setLevel(logging.WARNING)
- cmdmod = importlib.import_module('.' + args.cmd, elbepack.commands.__name__)
+ cmdmod = _import_cmd_module(args.cmd)
try:
cmdmod.run_command(cmd_argv)
except Exception as e:
- if args.propagate_exception:
- raise e
sys.exit(format_exception(e, output=sys.stderr,
base_module=elbepack,
verbose=args.stacktrace_on_error))
-def run_elbe_subcommand(args):
- return main([
- 'elbe', '--propagate-exception', *[os.fspath(arg) for arg in args],
- ])
+def run_elbe_subcommand(argv):
+ cmdmod = _import_cmd_module(argv[0])
+ return cmdmod.run_command([os.fspath(arg) for arg in argv[1:]])
---
base-commit: 92c2ef338b89517a5a1306aeb8021cc60c5dbf2f
change-id: 20240823-main-4c0a8616dd58
Best regards,
--
Thomas Weißschuh <thomas.weissschuh at linutronix.de>
More information about the elbe-devel
mailing list