From b956e8e2a65cd29f676082914d7b8ff7ec58d57c Mon Sep 17 00:00:00 2001 From: adario Date: Wed, 28 Aug 2019 19:08:42 +0200 Subject: [PATCH 1/3] find aliases without importing modules --- bumblebee/engine.py | 31 ++++++++++++++-------------- bumblebee/modules/aliases/datetime | 2 ++ bumblebee/modules/aliases/datetimetz | 2 ++ bumblebee/modules/aliases/pulseaudio | 2 ++ bumblebee/modules/aliases/test | 1 + bumblebee/modules/datetime.py | 2 -- bumblebee/modules/datetimetz.py | 2 -- bumblebee/modules/pulseaudio.py | 2 -- bumblebee/modules/test.py | 2 -- 9 files changed, 23 insertions(+), 23 deletions(-) create mode 100644 bumblebee/modules/aliases/datetime create mode 100644 bumblebee/modules/aliases/datetimetz create mode 100644 bumblebee/modules/aliases/pulseaudio create mode 100644 bumblebee/modules/aliases/test diff --git a/bumblebee/engine.py b/bumblebee/engine.py index 6c74f6a..07d32d6 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -16,16 +16,6 @@ try: except ImportError: from configparser import RawConfigParser -def all_modules(): - """Return a list of available modules""" - result = [] - path = os.path.dirname(bumblebee.modules.__file__) - for mod in [name for _, name, _ in pkgutil.iter_modules([path])]: - result.append({ - "name": mod - }) - return result - class Module(object): """Module instance base class @@ -231,14 +221,25 @@ class Engine(object): button=button["id"], cmd=module.parameter(button["name"])) def _read_aliases(self): + """Retruns a dictionary that maps every alias to its real module name""" result = {} - for module in all_modules(): + m_path = os.path.abspath(bumblebee.modules.__file__) + m_path = os.path.dirname(m_path) + ALIASES_PATH = m_path + "/aliases/" + for name in os.listdir(ALIASES_PATH): try: - mod = importlib.import_module("bumblebee.modules.{}".format(module["name"])) - for alias in getattr(mod, "ALIASES", []): - result[alias] = module["name"] + # listdir does not retur full paths + f_path = ALIASES_PATH + name + # skip any directory + if not os.path.isfile(f_path): continue + with open(f_path) as f: + for alias in f.readlines(): + alias = alias.strip() + # skip empty lines + if len(alias) == 0: continue + result[alias] = name except Exception as error: - log.warning("failed to import {}: {}".format(module["name"], str(error))) + log.warning("failed to load aliases of {}: {}".format(name, str(error))) return result def _load_module(self, module_name, config_name=None): diff --git a/bumblebee/modules/aliases/datetime b/bumblebee/modules/aliases/datetime new file mode 100644 index 0000000..8e43d6a --- /dev/null +++ b/bumblebee/modules/aliases/datetime @@ -0,0 +1,2 @@ +date +time diff --git a/bumblebee/modules/aliases/datetimetz b/bumblebee/modules/aliases/datetimetz new file mode 100644 index 0000000..08f6d17 --- /dev/null +++ b/bumblebee/modules/aliases/datetimetz @@ -0,0 +1,2 @@ +datetz +timetz diff --git a/bumblebee/modules/aliases/pulseaudio b/bumblebee/modules/aliases/pulseaudio new file mode 100644 index 0000000..9bcee50 --- /dev/null +++ b/bumblebee/modules/aliases/pulseaudio @@ -0,0 +1,2 @@ +pasink +pasource diff --git a/bumblebee/modules/aliases/test b/bumblebee/modules/aliases/test new file mode 100644 index 0000000..dc52a4f --- /dev/null +++ b/bumblebee/modules/aliases/test @@ -0,0 +1 @@ +test-alias diff --git a/bumblebee/modules/datetime.py b/bumblebee/modules/datetime.py index e9afeb2..8899c4a 100644 --- a/bumblebee/modules/datetime.py +++ b/bumblebee/modules/datetime.py @@ -16,8 +16,6 @@ import datetime import locale import bumblebee.engine -ALIASES = ["date", "time"] - def default_format(module): default = "%x %X" if module == "date": diff --git a/bumblebee/modules/datetimetz.py b/bumblebee/modules/datetimetz.py index e298249..1d0e3cb 100644 --- a/bumblebee/modules/datetimetz.py +++ b/bumblebee/modules/datetimetz.py @@ -26,8 +26,6 @@ import bumblebee.input import bumblebee.output import bumblebee.engine -ALIASES = ["datetz", "timetz"] - def default_format(module): default = "%x %X %Z" if module == "datetz": diff --git a/bumblebee/modules/pulseaudio.py b/bumblebee/modules/pulseaudio.py index 8b2f276..5cdd23c 100644 --- a/bumblebee/modules/pulseaudio.py +++ b/bumblebee/modules/pulseaudio.py @@ -24,8 +24,6 @@ import bumblebee.input import bumblebee.output import bumblebee.engine -ALIASES = ["pasink", "pasource"] - class Module(bumblebee.engine.Module): def __init__(self, engine, config): super(Module, self).__init__(engine, config, diff --git a/bumblebee/modules/test.py b/bumblebee/modules/test.py index b654575..5e92e0e 100644 --- a/bumblebee/modules/test.py +++ b/bumblebee/modules/test.py @@ -5,8 +5,6 @@ import bumblebee.engine -ALIASES = ["test-alias"] - class Module(bumblebee.engine.Module): def __init__(self, engine, config): super(Module, self).__init__(engine, config, From 2cad9f2e4663729d1bdc24149328b6d89aa68737 Mon Sep 17 00:00:00 2001 From: Martin Date: Fri, 30 Aug 2019 15:55:00 +0200 Subject: [PATCH 2/3] [battery_all] Fixed the battery path and used os to solve for the correct paths --- bumblebee/modules/battery_all.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bumblebee/modules/battery_all.py b/bumblebee/modules/battery_all.py index 868798f..8f4cebc 100644 --- a/bumblebee/modules/battery_all.py +++ b/bumblebee/modules/battery_all.py @@ -25,11 +25,10 @@ except ImportError: class Module(bumblebee.engine.Module): def __init__(self, engine, config): self._batteries = [] - # TODO: list all batteries - self._batteries.append("/sys/class/power_supply/BAT0") - self._batteries.append("/sys/class/power_supply/BAT1") - self._batteries.append("/sys/class/power_supply/battery") - + for battery in os.listdir('/sys/class/power_supply/'): + if not any(i in battery for i in ['AC', 'hidpp']): + self._batteries.append("/sys/class/power_supply/" + battery) + super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.capacity)) engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, From 56c3b77d0e9d17d00f667d4fe6211b09262aafac Mon Sep 17 00:00:00 2001 From: adario7 <24436551+adario7@users.noreply.github.com> Date: Tue, 3 Sep 2019 14:24:02 +0200 Subject: [PATCH 3/3] put back the method all_modules even if no longer used by `_read_aliases` it is still needed in other parts of the program. --- bumblebee/engine.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/bumblebee/engine.py b/bumblebee/engine.py index 07d32d6..fa54899 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -16,6 +16,16 @@ try: except ImportError: from configparser import RawConfigParser +def all_modules(): + """Return a list of available modules""" + result = [] + path = os.path.dirname(bumblebee.modules.__file__) + for mod in [name for _, name, _ in pkgutil.iter_modules([path])]: + result.append({ + "name": mod + }) + return result + class Module(object): """Module instance base class