From 18d7e1befbfc2f27a4f5eadf646022706e348278 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sat, 5 Nov 2016 12:28:05 +0100 Subject: [PATCH] [config] Allow parameter passing via commandline Allow the user to specify arbitrary configuration parameters from the commandline and evaluate those in the modules (and elsewhere). Re-enable the CPU module as a first showcase of this functionality. --- bumblebee/config.py | 13 ++++++++++++- bumblebee/modules/cpu.py | 23 ++++++++++++++--------- bumblebee/theme.py | 4 ++-- bumblebee/themes/solarized-powerline.json | 1 + 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/bumblebee/config.py b/bumblebee/config.py index 381fef9..65ee7f1 100644 --- a/bumblebee/config.py +++ b/bumblebee/config.py @@ -47,9 +47,16 @@ class Config(object): self._args = self._parser.parse_args(args) + for p in self._args.parameters: + key, value = p.split("=") + self.parameter(key, value) + + def set(self, name, value): + self._store[name] = value + def parameter(self, name, default=None): if not name in self._store: - self._store[name] = default + self.set(name, default) return self._store.get(name, default) def increase(self, name, limit, default): @@ -76,6 +83,10 @@ class Config(object): choices = [ "modules", "themes" ], action=print_usage, ) + parser.add_argument("-p", "--parameters", nargs="+", + help="Provide configuration parameters to individual modules.", + default=[] + ) parser.add_argument("-t", "--theme", help="Specify which theme to use for " "drawing the modules", default="default", diff --git a/bumblebee/modules/cpu.py b/bumblebee/modules/cpu.py index e99ddd0..bfadcdd 100644 --- a/bumblebee/modules/cpu.py +++ b/bumblebee/modules/cpu.py @@ -11,22 +11,27 @@ def description(): return "Displays CPU utilization across all CPUs." class Module(bumblebee.module.Module): - def __init__(self, output, args): - super(Module, self).__init__(args) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._perc = psutil.cpu_percent(percpu=False) + self._config = config - 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 data(self): + def widgets(self): self._perc = psutil.cpu_percent(percpu=False) - - return "{:05.02f}%".format(self._perc) + return [ + bumblebee.output.Widget(self, + "{:05.02f}%".format(self._perc) + ) + ] def warning(self): - return self._perc > 70 + return self._perc > self._config.parameter("cpu.warning", 70) def critical(self): - return self._perc > 80 + return self._perc > self._config.parameter("cpu.critical", 80) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/theme.py b/bumblebee/theme.py index f4baadb..657a4be 100644 --- a/bumblebee/theme.py +++ b/bumblebee/theme.py @@ -19,7 +19,7 @@ class Theme: self.begin() def begin(self): - self._config.parameter("theme.cycleidx", 0) + self._config.set("theme.cycleidx", 0) self._cycle = self._cycles[0] if len(self._cycles) > 0 else {} self._background = [ None, None ] @@ -64,7 +64,7 @@ class Theme: return self._background[1] def separator_block_width(self, widget): - return 9 + return self._get(widget, "separator-block-width") def _get(self, widget, name): module = widget.module() diff --git a/bumblebee/themes/solarized-powerline.json b/bumblebee/themes/solarized-powerline.json index 32d4319..0996104 100644 --- a/bumblebee/themes/solarized-powerline.json +++ b/bumblebee/themes/solarized-powerline.json @@ -18,6 +18,7 @@ "bg-warning": "#b58900", "default-separators": false, + "separator-block-width": 0, "separator": "" }, "date": {