diff --git a/bumblebee/modules/battery.py b/bumblebee/modules/battery.py index cf1251e..90e58f6 100644 --- a/bumblebee/modules/battery.py +++ b/bumblebee/modules/battery.py @@ -24,7 +24,7 @@ class Module(bumblebee.engine.Module): self._capacity = 100 self._ac = False - def capacity(self): + def capacity(self, widget): if self._ac: return "ac" if self._capacity == -1: diff --git a/bumblebee/modules/brightness.py b/bumblebee/modules/brightness.py index 56b5314..99712f0 100644 --- a/bumblebee/modules/brightness.py +++ b/bumblebee/modules/brightness.py @@ -24,7 +24,7 @@ class Module(bumblebee.engine.Module): engine.input.register_callback(self, button=bumblebee.input.WHEEL_DOWN, cmd="xbacklight -{}%".format(step)) - def brightness(self): + def brightness(self, widget): return "{:03.0f}%".format(self._brightness) def update(self, widgets): diff --git a/bumblebee/modules/caffeine.py b/bumblebee/modules/caffeine.py index cc2754e..bf57be6 100644 --- a/bumblebee/modules/caffeine.py +++ b/bumblebee/modules/caffeine.py @@ -16,7 +16,7 @@ class Module(bumblebee.engine.Module): cmd=self._toggle ) - def caffeine(self): + def caffeine(self, widget): return "" def state(self, widget): @@ -33,9 +33,6 @@ class Module(bumblebee.engine.Module): return False return False - def update(self, widgets): - pass - def _toggle(self, widget): if self._active(): bumblebee.util.execute("xset s default") diff --git a/bumblebee/modules/cmus.py b/bumblebee/modules/cmus.py index 857d895..991a1a1 100644 --- a/bumblebee/modules/cmus.py +++ b/bumblebee/modules/cmus.py @@ -41,8 +41,9 @@ class Module(bumblebee.engine.Module): self._status = None self._shuffle = False self._repeat = False + self._tags = defaultdict(lambda: '') - def description(self): + def description(self, widget): return string.Formatter().vformat(self._fmt, (), self._tags) def update(self, widgets): diff --git a/bumblebee/modules/cpu.py b/bumblebee/modules/cpu.py index c5bdc06..79e9229 100644 --- a/bumblebee/modules/cpu.py +++ b/bumblebee/modules/cpu.py @@ -21,7 +21,7 @@ class Module(bumblebee.engine.Module): engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="gnome-system-monitor") - def utilization(self): + def utilization(self, widget): return "{:06.02f}%".format(self._utilization) def update(self, widgets): diff --git a/bumblebee/modules/datetime.py b/bumblebee/modules/datetime.py index 6d6b594..4141a53 100644 --- a/bumblebee/modules/datetime.py +++ b/bumblebee/modules/datetime.py @@ -29,7 +29,7 @@ class Module(bumblebee.engine.Module): ) self._fmt = self.parameter("format", default_format(self.name)) - def get_time(self): + def get_time(self, widget): return datetime.datetime.now().strftime(self._fmt) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/disk.py b/bumblebee/modules/disk.py index de7f7ed..e052b88 100644 --- a/bumblebee/modules/disk.py +++ b/bumblebee/modules/disk.py @@ -21,11 +21,13 @@ class Module(bumblebee.engine.Module): ) self._path = self.parameter("path", "/") self._perc = 0 + self._used = 0 + self._size = 0 engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="nautilus {}".format(self._path)) - def diskspace(self): + def diskspace(self, widget): return "{} {}/{} ({:05.02f}%)".format(self._path, bumblebee.util.bytefmt(self._used), bumblebee.util.bytefmt(self._size), self._perc diff --git a/bumblebee/modules/load.py b/bumblebee/modules/load.py index 7ab4f34..c44ae5a 100644 --- a/bumblebee/modules/load.py +++ b/bumblebee/modules/load.py @@ -19,6 +19,7 @@ class Module(bumblebee.engine.Module): super(Module, self).__init__(engine, config, bumblebee.output.Widget(full_text=self.load) ) + self._load = [0, 0, 0] try: self._cpus = multiprocessing.cpu_count() except multiprocessing.NotImplementedError as e: @@ -26,7 +27,7 @@ class Module(bumblebee.engine.Module): engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="gnome-system-monitor") - def load(self): + def load(self, widget): return "{:.02f}/{:.02f}/{:.02f}".format( self._load[0], self._load[1], self._load[2] ) diff --git a/bumblebee/modules/memory.py b/bumblebee/modules/memory.py index 90515df..0ab1176 100644 --- a/bumblebee/modules/memory.py +++ b/bumblebee/modules/memory.py @@ -23,7 +23,7 @@ class Module(bumblebee.engine.Module): engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="gnome-system-monitor") - def memory_usage(self): + def memory_usage(self, widget): used = self._mem.total - self._mem.available return "{}/{} ({:05.02f}%)".format( bumblebee.util.bytefmt(used), diff --git a/bumblebee/modules/ping.py b/bumblebee/modules/ping.py new file mode 100644 index 0000000..a851b0e --- /dev/null +++ b/bumblebee/modules/ping.py @@ -0,0 +1,81 @@ +# pylint: disable=C0111,R0903 + +"""Periodically checks the RTT of a configurable host using ICMP echos + +Parameters: + * ping.interval: Time in seconds between two RTT checks (defaults to 60) + * ping.address : IP address to check + * ping.timeout : Timeout for waiting for a reply (defaults to 5.0) + * ping.probes : Number of probes to send (defaults to 5) + * ping.warning : Threshold for warning state, in seconds (defaults to 1.0) + * ping.critical: Threshold for critical state, in seconds (defaults to 2.0) +""" + +import re +import time +import threading + +import bumblebee.input +import bumblebee.output +import bumblebee.engine + +def get_rtt(module, widget): + + main = None + for thread in threading.enumerate(): + if thread.name == "MainThread": + main = thread + + interval = widget.get("interval") + next_check = 0 + while main.is_alive(): + try: + if int(time.time()) < next_check: + time.sleep(1) + continue + widget.set("rtt-unreachable", False) + res = bumblebee.util.execute("ping -n -q -c {} -W {} {}".format( + widget.get("rtt-probes"), widget.get("rtt-timeout"), widget.get("address") + )) + next_check = int(time.time()) + interval + + for line in res.split("\n"): + if not line.startswith("rtt"): continue + m = re.search(r'([0-9\.]+)/([0-9\.]+)/([0-9\.]+)/([0-9\.]+)\s+(\S+)', line) + + widget.set("rtt-min", float(m.group(1))) + widget.set("rtt-avg", float(m.group(2))) + widget.set("rtt-max", float(m.group(3))) + widget.set("rtt-unit", m.group(5)) + except Exception as e: + widget.set("rtt-unreachable", True) + +class Module(bumblebee.engine.Module): + def __init__(self, engine, config): + widget = bumblebee.output.Widget(full_text=self.rtt) + super(Module, self).__init__(engine, config, widget) + + widget.set("address", self.parameter("address", "8.8.8.8")) + widget.set("interval", self.parameter("interval", 60)) + widget.set("rtt-probes", self.parameter("probes", 5)) + widget.set("rtt-timeout", self.parameter("timeout", 5.0)) + widget.set("rtt-avg", 0.0) + widget.set("rtt-unit", "") + + self._thread = threading.Thread(target=get_rtt, args=(self,widget,)) + self._thread.start() + + def rtt(self, widget): + if widget.get("rtt-unreachable"): + return "{}: unreachable".format(widget.get("address")) + return "{}: {:.1f}{}".format( + widget.get("address"), + widget.get("rtt-avg"), + widget.get("rtt-unit") + ) + + def state(self, widget): + if widget.get("rtt-unreachable"): return ["critical"] + return self.threshold_state(widget.get("rtt-avg"), 1000.0, 2000.0) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/pulseaudio.py b/bumblebee/modules/pulseaudio.py index 82629b6..b13eda3 100644 --- a/bumblebee/modules/pulseaudio.py +++ b/bumblebee/modules/pulseaudio.py @@ -42,7 +42,7 @@ class Module(bumblebee.engine.Module): return line.replace(pattern, "") return "n/a" - def volume(self): + def volume(self, widget): if int(self._mono) > 0: return "{}%".format(self._mono) elif self._left == self._right: diff --git a/bumblebee/modules/spacer.py b/bumblebee/modules/spacer.py index 729ef54..b89c233 100644 --- a/bumblebee/modules/spacer.py +++ b/bumblebee/modules/spacer.py @@ -17,10 +17,7 @@ class Module(bumblebee.engine.Module): ) self._text = self.parameter("text", "") - def text(self): + def text(self, widget): return self._text - def update(self, widgets): - pass - # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/test.py b/bumblebee/modules/test.py index ad07faa..9f7485f 100644 --- a/bumblebee/modules/test.py +++ b/bumblebee/modules/test.py @@ -10,7 +10,4 @@ class Module(bumblebee.engine.Module): bumblebee.output.Widget(full_text="test") ) - def update(self, widgets): - pass - # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/output.py b/bumblebee/output.py index 7e76cc2..974e651 100644 --- a/bumblebee/output.py +++ b/bumblebee/output.py @@ -41,7 +41,7 @@ class Widget(bumblebee.store.Store): self._full_text = value else: if callable(self._full_text): - return self._full_text() + return self._full_text(self) else: return self._full_text diff --git a/tests/modules/test_modules.py b/tests/modules/test_modules.py index 74d543a..b67baa6 100644 --- a/tests/modules/test_modules.py +++ b/tests/modules/test_modules.py @@ -35,6 +35,7 @@ class TestGenericModules(unittest.TestCase): assertWidgetAttributes(self, widget) widget.set("variable", "value") self.assertEquals(widget.get("variable", None), "value") + self.assertTrue(isinstance(widget.full_text(), str)) @mock.patch("subprocess.Popen") def test_update(self, mock_output):