[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/<name>.py"

fixes #757
This commit is contained in:
tobi-wan-kenobi 2021-01-17 14:18:58 +01:00
parent 413abdcae7
commit 21ded8f640
2 changed files with 10 additions and 1 deletions

View file

@ -17,6 +17,7 @@ except Exception as e:
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
import sys
"""Loads a module by name """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)) log.debug("importing {} from contrib".format(module_short))
return getattr(mod, "Module")(config, theme) return getattr(mod, "Module")(config, theme)
except ImportError as e: 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") return Error(config=config, module=module_name, error="unable to load module")

View file

@ -11,6 +11,7 @@ Adding a new module to ``bumblebee-status`` is straight-forward:
``bumblebee-status`` (i.e. a module called ``bumblebee-status`` (i.e. a module called
``bumblebee_status/modules/contrib/test.py`` will be loaded using ``bumblebee_status/modules/contrib/test.py`` will be loaded using
``bumblebee-status -m test``) ``bumblebee-status -m test``)
- Alternatively, you can put your module in ``~/.config/bumblebee-status/modules/``
- The module name must follow the `Python Naming Conventions <https://www.python.org/dev/peps/pep-0008/#package-and-module-names>`_ - The module name must follow the `Python Naming Conventions <https://www.python.org/dev/peps/pep-0008/#package-and-module-names>`_
- See below for how to actually write the module - See below for how to actually write the module
- Test (run ``bumblebee-status`` in the CLI) - Test (run ``bumblebee-status`` in the CLI)