[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.
This commit is contained in:
Tobias Witek 2016-11-05 12:28:05 +01:00
parent 353c47d76e
commit 18d7e1befb
4 changed files with 29 additions and 12 deletions

View file

@ -47,9 +47,16 @@ class Config(object):
self._args = self._parser.parse_args(args) 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): def parameter(self, name, default=None):
if not name in self._store: if not name in self._store:
self._store[name] = default self.set(name, default)
return self._store.get(name, default) return self._store.get(name, default)
def increase(self, name, limit, default): def increase(self, name, limit, default):
@ -76,6 +83,10 @@ class Config(object):
choices = [ "modules", "themes" ], choices = [ "modules", "themes" ],
action=print_usage, 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 " parser.add_argument("-t", "--theme", help="Specify which theme to use for "
"drawing the modules", "drawing the modules",
default="default", default="default",

View file

@ -11,22 +11,27 @@ def description():
return "Displays CPU utilization across all CPUs." return "Displays CPU utilization across all CPUs."
class Module(bumblebee.module.Module): class Module(bumblebee.module.Module):
def __init__(self, output, args): def __init__(self, output, config):
super(Module, self).__init__(args) super(Module, self).__init__(output, config)
self._perc = psutil.cpu_percent(percpu=False) self._perc = psutil.cpu_percent(percpu=False)
self._config = config
output.add_callback(module=self.__module__, button=1, # TODO
cmd="gnome-system-monitor") # 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) self._perc = psutil.cpu_percent(percpu=False)
return [
return "{:05.02f}%".format(self._perc) bumblebee.output.Widget(self,
"{:05.02f}%".format(self._perc)
)
]
def warning(self): def warning(self):
return self._perc > 70 return self._perc > self._config.parameter("cpu.warning", 70)
def critical(self): def critical(self):
return self._perc > 80 return self._perc > self._config.parameter("cpu.critical", 80)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -19,7 +19,7 @@ class Theme:
self.begin() self.begin()
def begin(self): 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._cycle = self._cycles[0] if len(self._cycles) > 0 else {}
self._background = [ None, None ] self._background = [ None, None ]
@ -64,7 +64,7 @@ class Theme:
return self._background[1] return self._background[1]
def separator_block_width(self, widget): def separator_block_width(self, widget):
return 9 return self._get(widget, "separator-block-width")
def _get(self, widget, name): def _get(self, widget, name):
module = widget.module() module = widget.module()

View file

@ -18,6 +18,7 @@
"bg-warning": "#b58900", "bg-warning": "#b58900",
"default-separators": false, "default-separators": false,
"separator-block-width": 0,
"separator": "" "separator": ""
}, },
"date": { "date": {