From 21ded8f640359829159248aa5b7dfcbb71e56661 Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Sun, 17 Jan 2021 14:18:58 +0100 Subject: [PATCH] [core] Allow module loading from user directory If a module fails to load from both core and contrib, fall back to loading by file name from "~/.config/bumblebee-status/modules/.py" fixes #757 --- bumblebee_status/core/module.py | 10 +++++++++- docs/development/module.rst | 1 + 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/bumblebee_status/core/module.py b/bumblebee_status/core/module.py index dad06e2..145d16d 100644 --- a/bumblebee_status/core/module.py +++ b/bumblebee_status/core/module.py @@ -17,6 +17,7 @@ except Exception as e: log = logging.getLogger(__name__) +import sys """Loads a module by name @@ -45,7 +46,14 @@ def load(module_name, config=core.config.Config([]), theme=None): log.debug("importing {} from contrib".format(module_short)) return getattr(mod, "Module")(config, theme) except ImportError as e: - log.fatal("failed to import {} from contrib: {}".format(module_short, e)) + try: + log.warning("failed to import {} from system: {}".format(module_short, e)) + mod = importlib.machinery.SourceFileLoader("modules.{}".format(module_short), + os.path.expanduser("~/.config/bumblebee-status/modules/{}.py".format(module_short))).load_module() + log.debug("importing {} from user".format(module_short)) + return getattr(mod, "Module")(config, theme) + except ImportError as e: + log.fatal("failed to import {}: {}".format(module_short, e)) return Error(config=config, module=module_name, error="unable to load module") diff --git a/docs/development/module.rst b/docs/development/module.rst index 113a6f7..cc71900 100644 --- a/docs/development/module.rst +++ b/docs/development/module.rst @@ -11,6 +11,7 @@ Adding a new module to ``bumblebee-status`` is straight-forward: ``bumblebee-status`` (i.e. a module called ``bumblebee_status/modules/contrib/test.py`` will be loaded using ``bumblebee-status -m test``) +- Alternatively, you can put your module in ``~/.config/bumblebee-status/modules/`` - The module name must follow the `Python Naming Conventions `_ - See below for how to actually write the module - Test (run ``bumblebee-status`` in the CLI)