[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
This commit is contained in:
parent
10df79ce9a
commit
2a95e9fcc2
1 changed files with 56 additions and 21 deletions
|
@ -6,43 +6,78 @@ Requires the following executable:
|
||||||
* redshift
|
* redshift
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import threading
|
||||||
|
|
||||||
import bumblebee.input
|
import bumblebee.input
|
||||||
import bumblebee.output
|
import bumblebee.output
|
||||||
import bumblebee.engine
|
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):
|
class Module(bumblebee.engine.Module):
|
||||||
def __init__(self, engine, config):
|
def __init__(self, engine, config):
|
||||||
super(Module, self).__init__(engine, config,
|
widget = bumblebee.output.Widget(full_text=self.text)
|
||||||
bumblebee.output.Widget(full_text=self.text)
|
super(Module, self).__init__(engine, config, widget)
|
||||||
)
|
|
||||||
self._text = ""
|
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):
|
def text(self, widget):
|
||||||
return "{}".format(self._text)
|
return "{}".format(self._text)
|
||||||
|
|
||||||
def update(self, widgets):
|
def update(self, widgets):
|
||||||
result = bumblebee.util.execute("redshift -p")
|
widget = widgets[0]
|
||||||
|
self._condition.acquire()
|
||||||
temp = ""
|
self._condition.notify()
|
||||||
transition = ""
|
self._condition.release()
|
||||||
for line in result.split("\n"):
|
temp = widget.get("temp", "n/a")
|
||||||
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:])
|
|
||||||
self._text = temp
|
self._text = temp
|
||||||
|
transition = widget.get("transition", None)
|
||||||
if transition:
|
if transition:
|
||||||
self._text = "{} {}".format(temp, transition)
|
self._text = "{} {}".format(temp, transition)
|
||||||
|
|
||||||
def state(self, widget):
|
def state(self, widget):
|
||||||
return self._state
|
return widget.get("state", None)
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
Loading…
Reference in a new issue