2017-01-21 11:23:29 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays the current color temperature of redshift
|
|
|
|
|
|
|
|
Requires the following executable:
|
|
|
|
* redshift
|
|
|
|
"""
|
|
|
|
|
2017-12-29 11:18:07 +01:00
|
|
|
import threading
|
|
|
|
|
2017-01-21 11:23:29 +01:00
|
|
|
import bumblebee.input
|
|
|
|
import bumblebee.output
|
|
|
|
import bumblebee.engine
|
|
|
|
|
2017-12-29 11:18:07 +01:00
|
|
|
def is_terminated():
|
|
|
|
for thread in threading.enumerate():
|
|
|
|
if thread.name == "MainThread" and not thread.is_alive():
|
|
|
|
return True
|
|
|
|
return False
|
2017-01-21 11:23:29 +01:00
|
|
|
|
2017-12-29 11:18:07 +01:00
|
|
|
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
|
2017-01-21 11:23:29 +01:00
|
|
|
|
2017-12-29 11:18:07 +01:00
|
|
|
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"):
|
2017-01-21 11:23:29 +01:00
|
|
|
if "temperature" in line.lower():
|
2017-12-29 11:18:07 +01:00
|
|
|
widget.set("temp", line.split(" ")[2])
|
2017-01-21 11:23:29 +01:00
|
|
|
if "period" in line.lower():
|
|
|
|
state = line.split(" ")[1].lower()
|
|
|
|
if "day" in state:
|
2017-12-29 11:18:07 +01:00
|
|
|
widget.set("state", "day")
|
2017-01-21 11:23:29 +01:00
|
|
|
elif "night" in state:
|
2017-12-29 11:18:07 +01:00
|
|
|
widget.set("state", "night")
|
2017-01-21 11:23:29 +01:00
|
|
|
else:
|
2017-12-29 11:18:07 +01:00
|
|
|
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):
|
|
|
|
widget = bumblebee.output.Widget(full_text=self.text)
|
|
|
|
super(Module, self).__init__(engine, config, widget)
|
|
|
|
self._text = ""
|
|
|
|
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):
|
|
|
|
widget = widgets[0]
|
|
|
|
self._condition.acquire()
|
|
|
|
self._condition.notify()
|
|
|
|
self._condition.release()
|
|
|
|
temp = widget.get("temp", "n/a")
|
2017-09-20 08:59:23 +02:00
|
|
|
self._text = temp
|
2017-12-29 11:18:07 +01:00
|
|
|
transition = widget.get("transition", None)
|
2017-09-20 08:59:23 +02:00
|
|
|
if transition:
|
|
|
|
self._text = "{} {}".format(temp, transition)
|
2017-01-21 11:23:29 +01:00
|
|
|
|
|
|
|
def state(self, widget):
|
2017-12-29 11:18:07 +01:00
|
|
|
return widget.get("state", None)
|
2017-01-21 11:23:29 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|