[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!
This commit is contained in:
Tobias Witek 2016-11-05 14:26:02 +01:00
parent 9f9b27ad7a
commit 4e648cf009
11 changed files with 62 additions and 40 deletions

View file

@ -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")

View file

@ -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():

View file

@ -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

View file

@ -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:

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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 = {}

View file

@ -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

View file

@ -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

View file

@ -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))