[elbe-devel] [PATCH] elbepack: avoid string constants for import_module()

Thomas Weißschuh thomas.weissschuh at linutronix.de
Tue Jul 30 11:50:47 CEST 2024


Instead of hardcoding module names in strings, use references relative
to already imported module objects.

These are easier to check for automated tools.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/commands/daemon.py | 3 +--
 elbepack/main.py            | 4 +---
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/elbepack/commands/daemon.py b/elbepack/commands/daemon.py
index 3491092cd151..5e3ca6e8e856 100644
--- a/elbepack/commands/daemon.py
+++ b/elbepack/commands/daemon.py
@@ -82,8 +82,7 @@ def run_command(argv):
 
         for d in daemons:
             print(f'enable {d}')
-            module = 'elbepack.daemons.' + str(d)
-            cmdmod = importlib.import_module(module)
+            cmdmod = importlib.import_module('.' + d, elbepack.daemons.__name__)
             app = cmdmod.get_app()
             if hasattr(app, 'stop'):
                 stack.callback(app.stop)
diff --git a/elbepack/main.py b/elbepack/main.py
index 431e1c4b3a24..0ccb4b487173 100644
--- a/elbepack/main.py
+++ b/elbepack/main.py
@@ -42,8 +42,6 @@ def main(argv=sys.argv):
         usage()
         sys.exit(20)
 
-    modname = 'elbepack.commands.' + argv[1]
-
-    cmdmod = importlib.import_module(modname)
+    cmdmod = importlib.import_module('.' + argv[1], elbepack.commands.__name__)
 
     cmdmod.run_command(argv[2:])

---
base-commit: dcec7a10e0a4d63c7ee6c3d6077482ab98113211
change-id: 20240730-import_module-relative-4f40bb575c7b

Best regards,
-- 
Thomas Weißschuh <thomas.weissschuh at linutronix.de>



More information about the elbe-devel mailing list