From e895400589d730a59df1626b7d57cd5c28666eb1 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 30 Oct 2016 18:10:25 +0100 Subject: [PATCH] [modules] Add battery indicator plugin Add a plugin that displays the remaining battery power in %. This also introduces the concept of arguments that can be passed to a module during startup by delimiting the module name with ":", for example: -m battery:BAT1 to query the BAT1 device. Note that this works to an arbitray length, i.e. if a module accepts 3 parameters: -m ::: The module gets the arguments as list. --- bumblebee/module.py | 2 +- bumblebee/modules/battery.py | 19 +++++++++++++++++++ bumblebee/modules/time.py | 4 ++-- i3bumblebee | 6 ++++-- 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 bumblebee/modules/battery.py diff --git a/bumblebee/module.py b/bumblebee/module.py index 823c830..8f005f6 100644 --- a/bumblebee/module.py +++ b/bumblebee/module.py @@ -1,6 +1,6 @@ class Module(object): - def __init__(self, theme): + def __init__(self, theme, args): self._theme = theme def theme(self): diff --git a/bumblebee/modules/battery.py b/bumblebee/modules/battery.py new file mode 100644 index 0000000..e5be816 --- /dev/null +++ b/bumblebee/modules/battery.py @@ -0,0 +1,19 @@ +import datetime +import bumblebee.module + +class Module(bumblebee.module.Module): + def __init__(self, theme, args): + super(Module, self).__init__(theme, args) + self._battery = "BAT0" if not args else args[0] + self._capacity = 0 + self._status = "Unknown" + + def data(self): + with open("/sys/class/power_supply/%s/capacity" % self._battery) as f: + self._capacity = int(f.read()) + with open("/sys/class/power_supply/%s/status" % self._battery) as f: + self._status = f.read().strip() + + return "%02d%%" % self._capacity + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/time.py b/bumblebee/modules/time.py index 4b6a1db..2259823 100644 --- a/bumblebee/modules/time.py +++ b/bumblebee/modules/time.py @@ -2,8 +2,8 @@ import datetime import bumblebee.module class Module(bumblebee.module.Module): - def __init__(self, theme): - super(Module, self).__init__(theme) + def __init__(self, theme, args): + super(Module, self).__init__(theme, args) def data(self): return datetime.datetime.now().strftime("%x %X") diff --git a/i3bumblebee b/i3bumblebee index 5ddcbbd..8b4602a 100755 --- a/i3bumblebee +++ b/i3bumblebee @@ -28,8 +28,10 @@ def main(): for m in args.modules: # TODO: how to cleanly handle errors here? # (useful error messages) - module = importlib.import_module("bumblebee.modules.%s" % m) - modules.append(getattr(module, "Module")(theme)) + module_name = m if not ":" in m else m.split(":")[0] + module_args = None if not ":" in m else m.split(":")[1:] + module = importlib.import_module("bumblebee.modules.%s" % module_name) + modules.append(getattr(module, "Module")(theme, module_args)) output = bumblebee.outputs.i3.i3bar()