[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 <modulename>:<A>:<B>:<C>

The module gets the arguments as list.
This commit is contained in:
Tobias Witek 2016-10-30 18:10:25 +01:00
parent 4ad41a8ee0
commit e895400589
4 changed files with 26 additions and 5 deletions

View file

@ -1,6 +1,6 @@
class Module(object): class Module(object):
def __init__(self, theme): def __init__(self, theme, args):
self._theme = theme self._theme = theme
def theme(self): def theme(self):

View file

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

View file

@ -2,8 +2,8 @@ import datetime
import bumblebee.module import bumblebee.module
class Module(bumblebee.module.Module): class Module(bumblebee.module.Module):
def __init__(self, theme): def __init__(self, theme, args):
super(Module, self).__init__(theme) super(Module, self).__init__(theme, args)
def data(self): def data(self):
return datetime.datetime.now().strftime("%x %X") return datetime.datetime.now().strftime("%x %X")

View file

@ -28,8 +28,10 @@ def main():
for m in args.modules: for m in args.modules:
# TODO: how to cleanly handle errors here? # TODO: how to cleanly handle errors here?
# (useful error messages) # (useful error messages)
module = importlib.import_module("bumblebee.modules.%s" % m) module_name = m if not ":" in m else m.split(":")[0]
modules.append(getattr(module, "Module")(theme)) 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() output = bumblebee.outputs.i3.i3bar()