From 4e648cf0096898c15de6de0177b4f322cdfb6ec7 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sat, 5 Nov 2016 14:26:02 +0100 Subject: [PATCH] [modules] Add module-specific configuration Big oversight in my previous commits: Widgets need to be able to have specific configurations (i.e. the path for different instances of the "disk" module has to be different). To account for that, it is now possible to assign an "alias" to a module instance using ":" (for example: -m "disk:home"). This alias is then used for the configuration parameter resolution automatically, for example: -m disk:home -p home.path=/home As a consequence, parameter names in the module code are now relative to the module, which means: shorter! --- bumblebee/config.py | 23 ++++++++++++++++++++++- bumblebee/engine.py | 5 +++-- bumblebee/module.py | 9 ++++++--- bumblebee/modules/battery.py | 10 +++++----- bumblebee/modules/cpu.py | 8 ++++---- bumblebee/modules/disk.py | 13 +++++-------- bumblebee/modules/memory.py | 13 +++++++------ bumblebee/modules/nic.py | 4 ++-- bumblebee/modules/pulseaudio.py | 4 ++-- bumblebee/modules/spacer.py | 6 +++--- bumblebee/modules/time.py | 7 +++---- 11 files changed, 62 insertions(+), 40 deletions(-) diff --git a/bumblebee/config.py b/bumblebee/config.py index 65ee7f1..ef11a49 100644 --- a/bumblebee/config.py +++ b/bumblebee/config.py @@ -35,6 +35,23 @@ class print_usage(argparse.Action): 80, initial_indent=self._indent*2, subsequent_indent=self._indent*4) print "" +class ModuleConfig(object): + def __init__(self, config, prefix): + self._prefix = prefix + self._config = config + + def set(self, name, value): + name = self._prefix + name + return self._config.set(name, value) + + def parameter(self, name, default=None): + name = self._prefix + name + return self._config.parameter(name, default) + + def increase(self, name, limit, default): + name = self._prefix + name + return self._config.increase(name, limit, default) + class Config(object): def __init__(self, args): self._raw = args @@ -69,7 +86,11 @@ class Config(object): return self._args.theme def modules(self): - return self._args.modules + result = [] + for m in self._args.modules: + items = m.split(":") + result.append({ "name": items[0], "alias": items[1] if len(items) > 1 else None }) + return result def _parser(self): parser = argparse.ArgumentParser(description="display system data in the i3bar") diff --git a/bumblebee/engine.py b/bumblebee/engine.py index d8c2e22..a1d366f 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -16,9 +16,10 @@ class Engine: self._theme = bumblebee.theme.Theme(config) self._output = bumblebee.output.output(config) - def load_module(self, name): + def load_module(self, modulespec): + name = modulespec["name"] module = importlib.import_module("bumblebee.modules.{}".format(name)) - return getattr(module, "Module")(self._output, self._config) + return getattr(module, "Module")(self._output, self._config, modulespec["alias"]) def load_modules(self): for m in self._config.modules(): diff --git a/bumblebee/module.py b/bumblebee/module.py index a26931f..f7b3ad6 100644 --- a/bumblebee/module.py +++ b/bumblebee/module.py @@ -2,6 +2,7 @@ import os import pkgutil import importlib +import bumblebee.config import bumblebee.modules def modules(): @@ -32,9 +33,11 @@ class ModuleDescription(object): return "n/a" class Module(object): - def __init__(self, output, config): - self._config = config + def __init__(self, output, config, alias=None): self._output = output + self._alias = alias + name = "{}.".format(alias if alias else self.__module__.split(".")[-1]) + self._config = bumblebee.config.ModuleConfig(config, name) def critical(self, widget): return False @@ -46,6 +49,6 @@ class Module(object): return "default" def instance(self, widget): - return None + return self._alias if self._alias else self.__module__.split(".")[-1] # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/battery.py b/bumblebee/modules/battery.py index 6cf4a96..4b4e96f 100644 --- a/bumblebee/modules/battery.py +++ b/bumblebee/modules/battery.py @@ -11,9 +11,9 @@ def description(): return "Displays battery status, percentage and whether it's charging or discharging." class Module(bumblebee.module.Module): - def __init__(self, output, config): - super(Module, self).__init__(output, config) - self._battery = config.parameter("battery.device", "BAT0") + def __init__(self, output, config, alias): + super(Module, self).__init__(output, config, alias) + self._battery = config.parameter("device", "BAT0") self._capacity = 0 self._status = "Unknown" @@ -25,10 +25,10 @@ class Module(bumblebee.module.Module): return bumblebee.output.Widget(self,"{:02d}%".format(self._capacity)) def warning(self, widget): - return self._capacity < self._config.parameter("battery.warning", 20) + return self._capacity < self._config.parameter("warning", 20) def critical(self, widget): - return self._capacity < self._config.parameter("battery.critical", 10) + return self._capacity < self._config.parameter("critical", 10) def state(self, widget): with open("/sys/class/power_supply/{}/status".format(self._battery)) as f: diff --git a/bumblebee/modules/cpu.py b/bumblebee/modules/cpu.py index 3c76901..3d1acdb 100644 --- a/bumblebee/modules/cpu.py +++ b/bumblebee/modules/cpu.py @@ -11,8 +11,8 @@ def description(): return "Displays CPU utilization across all CPUs." class Module(bumblebee.module.Module): - def __init__(self, output, config): - super(Module, self).__init__(output, config) + def __init__(self, output, config, alias): + super(Module, self).__init__(output, config, alias) self._perc = psutil.cpu_percent(percpu=False) # TODO @@ -24,9 +24,9 @@ class Module(bumblebee.module.Module): return bumblebee.output.Widget(self, "{:05.02f}%".format(self._perc)) def warning(self, widget): - return self._perc > self._config.parameter("cpu.warning", 70) + return self._perc > self._config.parameter("warning", 70) def critical(self, widget): - return self._perc > self._config.parameter("cpu.critical", 80) + return self._perc > self._config.parameter("critical", 80) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/disk.py b/bumblebee/modules/disk.py index 4f35588..3618faf 100644 --- a/bumblebee/modules/disk.py +++ b/bumblebee/modules/disk.py @@ -12,9 +12,9 @@ def description(): return "Shows free diskspace, total diskspace and the percentage of free disk space." class Module(bumblebee.module.Module): - def __init__(self, output, config): - super(Module, self).__init__(output, config) - self._path = self._config.parameter("disk.path", "/") + def __init__(self, output, config, alias): + super(Module, self).__init__(output, config, alias) + self._path = self._config.parameter("path", "/") # TODO # output.add_callback(module=self.__module__, button=1, @@ -33,13 +33,10 @@ class Module(bumblebee.module.Module): bumblebee.util.bytefmt(self._size), self._perc) ) - def instance(self, widget): - return self._path - def warning(self, widget): - return self._perc > self._config.parameter("disk.warning", 80) + return self._perc > self._config.parameter("warning", 80) def critical(self, widget): - return self._perc > self._config.parameter("disk.critical", 90) + return self._perc > self._config.parameter("critical", 90) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/memory.py b/bumblebee/modules/memory.py index c1b8905..0717daf 100644 --- a/bumblebee/modules/memory.py +++ b/bumblebee/modules/memory.py @@ -12,12 +12,13 @@ def description(): return "Shows available RAM, total amount of RAM and the percentage of available RAM." class Module(bumblebee.module.Module): - def __init__(self, output, config): - super(Module, self).__init__(output, config) + def __init__(self, output, config, alias): + super(Module, self).__init__(output, config, alias) self._mem = psutil.virtual_memory() - output.add_callback(module=self.__module__, button=1, - cmd="gnome-system-monitor") +# TODO +# output.add_callback(module=self.__module__, button=1, +# cmd="gnome-system-monitor") def widgets(self): self._mem = psutil.virtual_memory() @@ -31,9 +32,9 @@ class Module(bumblebee.module.Module): ) def warning(self, widget): - return self._mem.percent < self._config.parameter("memory.warning", 20) + return self._mem.percent < self._config.parameter("warning", 20) def critical(self, widget): - return self._mem.percent < self._config.parameter("memory.critical", 10) + return self._mem.percent < self._config.parameter("critical", 10) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/nic.py b/bumblebee/modules/nic.py index 209d9ed..6cff11f 100644 --- a/bumblebee/modules/nic.py +++ b/bumblebee/modules/nic.py @@ -12,8 +12,8 @@ def description(): return "Displays the names, IP addresses and status of each available interface." class Module(bumblebee.module.Module): - def __init__(self, output, config): - super(Module, self).__init__(output, config) + def __init__(self, output, config, alias): + super(Module, self).__init__(output, config, alias) self._exclude = ( "lo", "virbr", "docker", "vboxnet" ) self._state = "down" self._typecache = {} diff --git a/bumblebee/modules/pulseaudio.py b/bumblebee/modules/pulseaudio.py index abbf4e0..3e6c1a3 100644 --- a/bumblebee/modules/pulseaudio.py +++ b/bumblebee/modules/pulseaudio.py @@ -26,8 +26,8 @@ def description(): return "See 'pasource'." class Module(bumblebee.module.Module): - def __init__(self, output, config): - super(Module, self).__init__(output, config) + def __init__(self, output, config, alias): + super(Module, self).__init__(output, config, alias) self._module = self.__module__.split(".")[-1] self._left = 0 diff --git a/bumblebee/modules/spacer.py b/bumblebee/modules/spacer.py index c82952c..d72cc1d 100644 --- a/bumblebee/modules/spacer.py +++ b/bumblebee/modules/spacer.py @@ -11,10 +11,10 @@ def description(): return "Draws an empty field." class Module(bumblebee.module.Module): - def __init__(self, output, config): - super(Module, self).__init__(output, config) + def __init__(self, output, config, alias): + super(Module, self).__init__(output, config, alias) def widgets(self): - return bumblebee.output.Widget(self, "") + return bumblebee.output.Widget(self, self._config.parameter("text", "")) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/time.py b/bumblebee/modules/time.py index 358e75b..985997b 100644 --- a/bumblebee/modules/time.py +++ b/bumblebee/modules/time.py @@ -18,8 +18,8 @@ def description(): return "Displays the current time, using the optional format string as input for strftime." class Module(bumblebee.module.Module): - def __init__(self, output, config): - super(Module, self).__init__(output, config) + def __init__(self, output, config, alias): + super(Module, self).__init__(output, config, alias) module = self.__module__.split(".")[-1] default = "%x %X" @@ -28,8 +28,7 @@ class Module(bumblebee.module.Module): if module == "time": default = "%X" - param_name = "{}.format".format(module) - self._fmt = config.parameter(param_name, default) + self._fmt = config.parameter("format", default) def widgets(self): return bumblebee.output.Widget(self, datetime.datetime.now().strftime(self._fmt))