From 2a95e9fcc2d6cedeb39bd7a3cd3f3d1b77ac3878 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Fri, 29 Dec 2017 11:18:07 +0100 Subject: [PATCH] [modules/redshift] Update in separate thread Move updating of redshift information into a separate thread so that errors like missing network connectivity do not block the whole status bar. fixes #61 --- bumblebee/modules/redshift.py | 77 +++++++++++++++++++++++++---------- 1 file changed, 56 insertions(+), 21 deletions(-) diff --git a/bumblebee/modules/redshift.py b/bumblebee/modules/redshift.py index 5dce616..058c9b5 100644 --- a/bumblebee/modules/redshift.py +++ b/bumblebee/modules/redshift.py @@ -6,43 +6,78 @@ Requires the following executable: * redshift """ +import threading + import bumblebee.input import bumblebee.output import bumblebee.engine +def is_terminated(): + for thread in threading.enumerate(): + if thread.name == "MainThread" and not thread.is_alive(): + return True + return False + +def get_redshift_value(widget): + while True: + if is_terminated(): + return + widget.get("condition").acquire() + while True: + try: + widget.get("condition").wait(1) + except RuntimeError: + continue + break + + try: + res = bumblebee.util.execute("redshift -p") + except Exception: + res = "" + widget.set("temp", "n/a") + widget.set("transition", None) + for line in res.split("\n"): + if "temperature" in line.lower(): + widget.set("temp", line.split(" ")[2]) + if "period" in line.lower(): + state = line.split(" ")[1].lower() + if "day" in state: + widget.set("state", "day") + elif "night" in state: + widget.set("state", "night") + else: + widget.set("state", "transition") + widget.set("transition", " ".join(line.split(" ")[2:])) + widget.get("condition").release() + class Module(bumblebee.engine.Module): def __init__(self, engine, config): - super(Module, self).__init__(engine, config, - bumblebee.output.Widget(full_text=self.text) - ) + widget = bumblebee.output.Widget(full_text=self.text) + super(Module, self).__init__(engine, config, widget) self._text = "" - self._state = "transition" + self._condition = threading.Condition() + widget.set("condition", self._condition) + self._thread = threading.Thread(target=get_redshift_value, args=(widget,)) + self._thread.start() + self._condition.acquire() + self._condition.notify() + self._condition.release() def text(self, widget): return "{}".format(self._text) def update(self, widgets): - result = bumblebee.util.execute("redshift -p") - - temp = "" - transition = "" - for line in result.split("\n"): - if "temperature" in line.lower(): - temp = line.split(" ")[2] - if "period" in line.lower(): - state = line.split(" ")[1].lower() - if "day" in state: - self._state = "day" - elif "night" in state: - self._state = "night" - else: - self._state = "transition" - transition = " ".join(line.split(" ")[2:]) + widget = widgets[0] + self._condition.acquire() + self._condition.notify() + self._condition.release() + temp = widget.get("temp", "n/a") self._text = temp + transition = widget.get("transition", None) if transition: self._text = "{} {}".format(temp, transition) def state(self, widget): - return self._state + return widget.get("state", None) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4