From 23d7d53fca29da72fc3fcee619078fe805ba96b1 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sun, 11 Dec 2016 08:25:54 +0100 Subject: [PATCH] [modules] critical/warning threshold refactoring Quite a lot of modules use the "if higher X -> critical, if higher Y -> warning" idiom now, so extracted that into a common function for reuse. see #23 --- bumblebee/engine.py | 7 +++++++ bumblebee/modules/cmus.py | 12 ++++-------- bumblebee/modules/cpu.py | 6 +----- bumblebee/modules/disk.py | 6 +----- bumblebee/modules/load.py | 5 +---- 5 files changed, 14 insertions(+), 22 deletions(-) diff --git a/bumblebee/engine.py b/bumblebee/engine.py index 4645cf8..7b06160 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -51,6 +51,13 @@ class Module(object): name = "{}.{}".format(self.name, name) return self._config["config"].get(name, default) + def threshold_state(self, value, warn, crit): + if value > float(self.parameter("critical", crit)): + return "critical" + if value > float(self.parameter("warning", warn)): + return "warning" + return None + class Engine(object): """Engine for driving the application diff --git a/bumblebee/modules/cmus.py b/bumblebee/modules/cmus.py index 0505294..857d895 100644 --- a/bumblebee/modules/cmus.py +++ b/bumblebee/modules/cmus.py @@ -72,14 +72,10 @@ class Module(bumblebee.engine.Module): if line.startswith("tag"): key, value = line.split(" ", 2)[1:] self._tags.update({ key: value }) - if line.startswith("duration"): - self._tags.update({ - "duration": bumblebee.util.durationfmt(int(line.split(" ")[1])) - }) - if line.startswith("position"): - self._tags.update({ - "position": bumblebee.util.durationfmt(int(line.split(" ")[1])) - }) + for key in ["duration", "position"]: + if line.startswith(key): + dur = int(line.split(" ")[1]) + self._tags.update({key:bumblebee.util.durationfmt(dur)}) if line.startswith("set repeat "): self._repeat = False if "false" in line else True if line.startswith("set shuffle "): diff --git a/bumblebee/modules/cpu.py b/bumblebee/modules/cpu.py index 214b205..c5bdc06 100644 --- a/bumblebee/modules/cpu.py +++ b/bumblebee/modules/cpu.py @@ -28,10 +28,6 @@ class Module(bumblebee.engine.Module): self._utilization = psutil.cpu_percent(percpu=False) def state(self, widget): - if self._utilization > int(self.parameter("critical", 80)): - return "critical" - if self._utilization > int(self.parameter("warning", 70)): - return "warning" - return None + return self.threshold_state(self._utilization, 70, 80) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/disk.py b/bumblebee/modules/disk.py index a0b6aad..de7f7ed 100644 --- a/bumblebee/modules/disk.py +++ b/bumblebee/modules/disk.py @@ -38,10 +38,6 @@ class Module(bumblebee.engine.Module): self._perc = 100.0*self._used/self._size def state(self, widget): - if self._perc > float(self.parameter("critical", 90)): - return "critical" - if self._perc > float(self.parameter("warning", 80)): - return "warning" - return None + return self.threshold_state(self._perc, 80, 90) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/load.py b/bumblebee/modules/load.py index 8c51e9f..7ab4f34 100644 --- a/bumblebee/modules/load.py +++ b/bumblebee/modules/load.py @@ -35,9 +35,6 @@ class Module(bumblebee.engine.Module): self._load = os.getloadavg() def state(self, widget): - if self._load[0] > float(self.parameter("critical", self._cpus*0.8)): - return "critical" - if self._load[0] > float(self.parameter("warning", self._cpus*0.7)): - return "warning" + return self.threshold_state(self._load[0], self._cpus*0.7, self._cpus*0.8) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4