[modules] Re-enable nic module + allow widget states

All callback from a widget into a module (e.g. for retrieving the status
or the criticality state) now get a widget passed. This has the purpose
of allowing a module to store state/widget specific data somewhere. This
way, for instance, it is possible to store the interface name as part of
the widget, thus making it possible to show the status of the correct
interface.
This commit is contained in:
Tobias Witek 2016-11-05 13:42:26 +01:00
parent bd0089dac0
commit a9a62a738d
7 changed files with 74 additions and 84 deletions

View file

@ -36,24 +36,16 @@ class Module(object):
self._config = config self._config = config
self._output = output self._output = output
def data(self): def critical(self, widget):
pass
def critical(self):
return False return False
def warning(self): def warning(self, widget):
return False return False
def state(self): def state(self, widget):
return "default" return "default"
def instance(self): def instance(self, widget):
return None return None
def next(self):
return False
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -24,13 +24,13 @@ class Module(bumblebee.module.Module):
return bumblebee.output.Widget(self,"{:02d}%".format(self._capacity)) return bumblebee.output.Widget(self,"{:02d}%".format(self._capacity))
def warning(self): def warning(self, widget):
return self._capacity < self._config.parameter("battery.warning", 20) return self._capacity < self._config.parameter("battery.warning", 20)
def critical(self): def critical(self, widget):
return self._capacity < self._config.parameter("battery.critical", 10) return self._capacity < self._config.parameter("battery.critical", 10)
def state(self): def state(self, widget):
with open("/sys/class/power_supply/{}/status".format(self._battery)) as f: with open("/sys/class/power_supply/{}/status".format(self._battery)) as f:
self._status = f.read().strip() self._status = f.read().strip()

View file

@ -23,10 +23,10 @@ class Module(bumblebee.module.Module):
self._perc = psutil.cpu_percent(percpu=False) self._perc = psutil.cpu_percent(percpu=False)
return bumblebee.output.Widget(self, "{:05.02f}%".format(self._perc)) return bumblebee.output.Widget(self, "{:05.02f}%".format(self._perc))
def warning(self): def warning(self, widget):
return self._perc > self._config.parameter("cpu.warning", 70) return self._perc > self._config.parameter("cpu.warning", 70)
def critical(self): def critical(self, widget):
return self._perc > self._config.parameter("cpu.critical", 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

@ -33,13 +33,13 @@ class Module(bumblebee.module.Module):
bumblebee.util.bytefmt(self._size), self._perc) bumblebee.util.bytefmt(self._size), self._perc)
) )
def instance(self): def instance(self, widget):
return self._path return self._path
def warning(self): def warning(self, widget):
return self._perc > self._config.parameter("disk.warning", 80) return self._perc > self._config.parameter("disk.warning", 80)
def critical(self): def critical(self, widget):
return self._perc > self._config.parameter("disk.critical", 90) return self._perc > self._config.parameter("disk.critical", 90)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -12,24 +12,28 @@ def description():
return "Shows available RAM, total amount of RAM and the percentage of available RAM." return "Shows available RAM, total amount of RAM and the percentage of available RAM."
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._mem = psutil.virtual_memory() self._mem = psutil.virtual_memory()
output.add_callback(module=self.__module__, button=1, output.add_callback(module=self.__module__, button=1,
cmd="gnome-system-monitor") cmd="gnome-system-monitor")
def data(self): def widgets(self):
self._mem = psutil.virtual_memory() self._mem = psutil.virtual_memory()
used = self._mem.total - self._mem.available used = self._mem.total - self._mem.available
return "{}/{} ({:05.02f}%)".format(bumblebee.util.bytefmt(used), bumblebee.util.bytefmt(self._mem.total), self._mem.percent) return bumblebee.output.Widget(self, "{}/{} ({:05.02f}%)".format(
bumblebee.util.bytefmt(used),
bumblebee.util.bytefmt(self._mem.total),
self._mem.percent)
)
def warning(self): def warning(self, widget):
return self._mem.percent < 20 return self._mem.percent < self._config.parameter("memory.warning", 20)
def critical(self): def critical(self, widget):
return self._mem.percent < 10 return self._mem.percent < self._config.parameter("memory.critical", 10)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -12,72 +12,59 @@ def description():
return "Displays the names, IP addresses and status of each available interface." return "Displays the names, IP addresses and status of each available interface."
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._exclude = ( "lo", "virbr", "docker", "vboxnet" ) self._exclude = ( "lo", "virbr", "docker", "vboxnet" )
self._interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ]
self._index = 0
self._intf = self._interfaces[0] if len(self._interfaces) > 0 else None
self._cache = {}
self._state = "down" self._state = "down"
def data(self): def widgets(self):
if len(self._interfaces) <= self._index: result = []
return "n/a" interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ]
self._intf = self._interfaces[self._index]
self._state = "down"
addr = [] addr = []
state = "down"
for intf in interfaces:
try:
if netifaces.AF_INET in netifaces.ifaddresses(intf):
for ip in netifaces.ifaddresses(intf)[netifaces.AF_INET]:
if "addr" in ip and ip["addr"] != "":
addr.append(ip["addr"])
state = "up"
except Exception as e:
addr = []
widget = bumblebee.output.Widget(self, "{} {} {}".format(
intf, state, ", ".join(addr)
))
widget.set("intf", intf)
widget.set("state", state)
result.append(widget)
try: return result
if netifaces.AF_INET in netifaces.ifaddresses(self._intf):
for ip in netifaces.ifaddresses(self._intf)[netifaces.AF_INET]:
if "addr" in ip and ip["addr"] != "":
addr.append(ip["addr"])
self._state = "up"
except Exception as e:
self._state = "down"
addr = []
return "{} {} {}".format(self._intf, self._state, ", ".join(addr))
def next(self):
self._index += 1
if self._index < len(self._interfaces):
return True
self._index = 0
# reload to support hotplug
self._interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ]
return False
def _iswlan(self, intf): def _iswlan(self, intf):
if not "wlan{}".format(intf) in self._cache: iw = pyroute2.IW()
iw = pyroute2.IW() ip = pyroute2.IPRoute()
ip = pyroute2.IPRoute() idx = ip.link_lookup(ifname=intf)[0]
idx = ip.link_lookup(ifname=intf)[0] try:
try: iw.get_interface_by_ifindex(idx)
iw.get_interface_by_ifindex(idx) return True
self._cache["wlan{}".format(intf)] = True except Exception as e:
except Exception as e: return False
self._cache["wlan{}".format(intf)] = False
return self._cache["wlan{}".format(intf)]
def _istunnel(self, intf): def _istunnel(self, intf):
return intf.startswith("tun") return intf.startswith("tun")
def instance(self): def state(self, widget):
return self._intf intf = widget.get("intf")
t = "wireless" if self._iswlan(intf) else "wired"
def state(self): t = "tunnel" if self._istunnel(intf) else t
t = "wireless" if self._iswlan(self._intf) else "wired"
t = "tunnel" if self._istunnel(self._intf) else t return "{}-{}".format(t, widget.get("state"))
return "{}-{}".format(t, self._state) def warning(self, widget):
return widget.get("state") != "up"
def warning(self): def critical(self, widget):
return self._state != "up" return widget.get("state") == "down"
def critical(self):
return self._state == "down"
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -8,15 +8,22 @@ class Widget(object):
def __init__(self, obj, text): def __init__(self, obj, text):
self._obj = obj self._obj = obj
self._text = text self._text = text
self._store = {}
def set(self, key, value):
self._store[key] = value
def get(self, key, default=None):
return self._store.get(key, default)
def state(self): def state(self):
return self._obj.state() return self._obj.state(self)
def warning(self): def warning(self):
return self._obj.warning() return self._obj.warning(self)
def critical(self): def critical(self):
return self._obj.critical() return self._obj.critical(self)
def module(self): def module(self):
return self._obj.__module__.split(".")[-1] return self._obj.__module__.split(".")[-1]
@ -25,7 +32,7 @@ class Widget(object):
return self._obj.__module__ return self._obj.__module__
def instance(self): def instance(self):
rv = getattr(self._obj, "instance")() rv = getattr(self._obj, "instance")(self)
def text(self): def text(self):
return self._text return self._text