diff --git a/bumblebee/module.py b/bumblebee/module.py index 2b60381..a26931f 100644 --- a/bumblebee/module.py +++ b/bumblebee/module.py @@ -36,24 +36,16 @@ class Module(object): self._config = config self._output = output - def data(self): - pass - - def critical(self): + def critical(self, widget): return False - def warning(self): + def warning(self, widget): return False - def state(self): + def state(self, widget): return "default" - def instance(self): + def instance(self, widget): return None - def next(self): - return False - - - # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/battery.py b/bumblebee/modules/battery.py index 2b3b8de..6cf4a96 100644 --- a/bumblebee/modules/battery.py +++ b/bumblebee/modules/battery.py @@ -24,13 +24,13 @@ class Module(bumblebee.module.Module): 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) - def critical(self): + def critical(self, widget): 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: self._status = f.read().strip() diff --git a/bumblebee/modules/cpu.py b/bumblebee/modules/cpu.py index 0df03a1..3c76901 100644 --- a/bumblebee/modules/cpu.py +++ b/bumblebee/modules/cpu.py @@ -23,10 +23,10 @@ class Module(bumblebee.module.Module): self._perc = psutil.cpu_percent(percpu=False) 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) - def critical(self): + def critical(self, widget): return self._perc > self._config.parameter("cpu.critical", 80) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/disk.py b/bumblebee/modules/disk.py index 506d9d2..4f35588 100644 --- a/bumblebee/modules/disk.py +++ b/bumblebee/modules/disk.py @@ -33,13 +33,13 @@ class Module(bumblebee.module.Module): bumblebee.util.bytefmt(self._size), self._perc) ) - def instance(self): + def instance(self, widget): return self._path - def warning(self): + def warning(self, widget): 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) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/memory.py b/bumblebee/modules/memory.py index 8245733..c1b8905 100644 --- a/bumblebee/modules/memory.py +++ b/bumblebee/modules/memory.py @@ -12,24 +12,28 @@ 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, args): - super(Module, self).__init__(args) + def __init__(self, output, config): + super(Module, self).__init__(output, config) self._mem = psutil.virtual_memory() output.add_callback(module=self.__module__, button=1, cmd="gnome-system-monitor") - def data(self): + def widgets(self): self._mem = psutil.virtual_memory() 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): - return self._mem.percent < 20 + def warning(self, widget): + return self._mem.percent < self._config.parameter("memory.warning", 20) - def critical(self): - return self._mem.percent < 10 + def critical(self, widget): + return self._mem.percent < self._config.parameter("memory.critical", 10) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/nic.py b/bumblebee/modules/nic.py index 9040752..670d8d4 100644 --- a/bumblebee/modules/nic.py +++ b/bumblebee/modules/nic.py @@ -12,72 +12,59 @@ def description(): return "Displays the names, IP addresses and status of each available interface." class Module(bumblebee.module.Module): - def __init__(self, output, args): - super(Module, self).__init__(args) + def __init__(self, output, config): + super(Module, self).__init__(output, config) 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" - def data(self): - if len(self._interfaces) <= self._index: - return "n/a" - self._intf = self._interfaces[self._index] - self._state = "down" + def widgets(self): + result = [] + interfaces = [ i for i in netifaces.interfaces() if not i.startswith(self._exclude) ] 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: - 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 + return result def _iswlan(self, intf): - if not "wlan{}".format(intf) in self._cache: - iw = pyroute2.IW() - ip = pyroute2.IPRoute() - idx = ip.link_lookup(ifname=intf)[0] - try: - iw.get_interface_by_ifindex(idx) - self._cache["wlan{}".format(intf)] = True - except Exception as e: - self._cache["wlan{}".format(intf)] = False - return self._cache["wlan{}".format(intf)] + iw = pyroute2.IW() + ip = pyroute2.IPRoute() + idx = ip.link_lookup(ifname=intf)[0] + try: + iw.get_interface_by_ifindex(idx) + return True + except Exception as e: + return False def _istunnel(self, intf): return intf.startswith("tun") - def instance(self): - return self._intf + def state(self, widget): + intf = widget.get("intf") + t = "wireless" if self._iswlan(intf) else "wired" - def state(self): - t = "wireless" if self._iswlan(self._intf) else "wired" + t = "tunnel" if self._istunnel(intf) else t - 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): - return self._state != "up" - - def critical(self): - return self._state == "down" + def critical(self, widget): + return widget.get("state") == "down" # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/output.py b/bumblebee/output.py index 7613add..be9bbb8 100644 --- a/bumblebee/output.py +++ b/bumblebee/output.py @@ -8,15 +8,22 @@ class Widget(object): def __init__(self, obj, text): self._obj = obj 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): - return self._obj.state() + return self._obj.state(self) def warning(self): - return self._obj.warning() + return self._obj.warning(self) def critical(self): - return self._obj.critical() + return self._obj.critical(self) def module(self): return self._obj.__module__.split(".")[-1] @@ -25,7 +32,7 @@ class Widget(object): return self._obj.__module__ def instance(self): - rv = getattr(self._obj, "instance")() + rv = getattr(self._obj, "instance")(self) def text(self): return self._text