2020-03-07 14:06:01 +01:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
2020-04-02 16:30:31 +02:00
|
|
|
"""Displays the current color temperature of redshift
|
2020-03-07 14:06:01 +01:00
|
|
|
|
|
|
|
Requires the following executable:
|
|
|
|
* redshift
|
|
|
|
|
|
|
|
Parameters:
|
2020-03-07 14:13:25 +01:00
|
|
|
* redshift.location : location provider, either of 'auto' (default), 'geoclue2',
|
2020-05-06 12:57:38 +02:00
|
|
|
'ipinfo' or 'manual'
|
|
|
|
'auto' uses whatever redshift is configured to do
|
2020-03-07 14:06:18 +01:00
|
|
|
* redshift.lat : latitude if location is set to 'manual'
|
|
|
|
* redshift.lon : longitude if location is set to 'manual'
|
2020-05-07 06:44:27 +02:00
|
|
|
* redshift.transition_info: information about the transitions (x% day) defaults to True
|
2020-04-02 16:30:31 +02:00
|
|
|
"""
|
2020-03-07 14:06:01 +01:00
|
|
|
|
2020-04-12 20:36:32 +02:00
|
|
|
import re
|
2020-03-07 14:06:01 +01:00
|
|
|
import threading
|
|
|
|
|
2020-03-07 14:13:25 +01:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.input
|
2020-04-02 16:30:31 +02:00
|
|
|
import core.decorators
|
2020-03-07 14:06:01 +01:00
|
|
|
|
2020-03-07 14:13:25 +01:00
|
|
|
import util.cli
|
2020-04-15 13:26:08 +02:00
|
|
|
import util.location
|
2020-03-07 14:06:01 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
|
2020-04-12 20:28:11 +02:00
|
|
|
def get_redshift_value(module):
|
|
|
|
widget = module.widget()
|
2020-05-03 11:15:52 +02:00
|
|
|
location = module.parameter("location", "auto")
|
|
|
|
lat = module.parameter("lat", None)
|
|
|
|
lon = module.parameter("lon", None)
|
2020-04-12 20:28:11 +02:00
|
|
|
|
|
|
|
# Even if location method is set to manual, if we have no lat or lon,
|
|
|
|
# fall back to the geoclue2 method.
|
2020-05-03 11:15:52 +02:00
|
|
|
if location == "manual" and (lat is None or lon is None):
|
|
|
|
location = "geoclue2"
|
2020-04-12 20:28:11 +02:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
command = ["redshift", "-p"]
|
|
|
|
if location == "manual":
|
|
|
|
command.extend(["-l", "{}:{}".format(lat, lon)])
|
|
|
|
if location == "geoclue2":
|
|
|
|
command.extend(["-l", "geoclue2"])
|
2020-04-12 20:28:11 +02:00
|
|
|
|
|
|
|
try:
|
2020-05-03 11:15:52 +02:00
|
|
|
res = util.cli.execute(" ".join(command))
|
2020-04-12 20:28:11 +02:00
|
|
|
except Exception:
|
2020-05-03 11:15:52 +02:00
|
|
|
res = ""
|
|
|
|
widget.set("temp", "n/a")
|
|
|
|
widget.set("transition", "")
|
|
|
|
widget.set("state", "day")
|
|
|
|
for line in res.split("\n"):
|
2020-04-12 20:28:11 +02:00
|
|
|
line = line.lower()
|
2020-05-03 11:15:52 +02:00
|
|
|
if "temperature" in line:
|
|
|
|
widget.set("temp", line.split(" ")[2])
|
|
|
|
if "period" in line:
|
|
|
|
state = line.split(" ")[1]
|
|
|
|
if "day" in state:
|
|
|
|
widget.set("state", "day")
|
|
|
|
elif "night" in state:
|
|
|
|
widget.set("state", "night")
|
2020-04-12 20:28:11 +02:00
|
|
|
else:
|
2020-05-03 11:15:52 +02:00
|
|
|
widget.set("state", "transition")
|
|
|
|
match = re.search(r"(\d+)\.\d+% ([a-z]+)", line)
|
|
|
|
widget.set(
|
|
|
|
"transition", "({}% {})".format(match.group(1), match.group(2))
|
|
|
|
)
|
|
|
|
core.event.trigger("update", [widget.module.id], redraw_only=True)
|
|
|
|
|
2020-03-07 14:06:01 +01:00
|
|
|
|
2020-03-07 14:13:25 +01:00
|
|
|
class Module(core.module.Module):
|
2020-04-02 16:30:31 +02:00
|
|
|
@core.decorators.every(seconds=10)
|
2020-04-26 16:39:24 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.text))
|
2020-03-07 14:06:01 +01:00
|
|
|
|
2020-04-12 20:36:32 +02:00
|
|
|
self.__thread = None
|
2020-05-07 06:44:27 +02:00
|
|
|
self.transition_info = self.parameter("transition_info", "True")
|
2020-03-07 14:06:01 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
if self.parameter("location", "") == "ipinfo":
|
2020-04-12 20:28:11 +02:00
|
|
|
# override lon/lat with ipinfo
|
2020-03-07 14:06:01 +01:00
|
|
|
try:
|
2020-04-15 13:26:08 +02:00
|
|
|
location = util.location.coordinates()
|
2020-05-03 11:15:52 +02:00
|
|
|
self.set("lat", location[0])
|
|
|
|
self.set("lon", location[1])
|
|
|
|
self.set("location", "manual")
|
2020-03-07 14:06:01 +01:00
|
|
|
except Exception:
|
|
|
|
# Fall back to geoclue2.
|
2020-05-03 11:15:52 +02:00
|
|
|
self.set("location", "geoclue2")
|
2020-03-07 14:06:01 +01:00
|
|
|
|
2020-05-03 11:15:52 +02:00
|
|
|
self._text = ""
|
2020-03-07 14:06:01 +01:00
|
|
|
|
|
|
|
def text(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
val = widget.get("temp", "n/a")
|
|
|
|
transition = widget.get("transition", "")
|
2020-05-07 06:44:27 +02:00
|
|
|
if transition and self.transition_info=="True":
|
2020-05-03 11:15:52 +02:00
|
|
|
val = "{} {}".format(val, transition)
|
2020-04-12 20:28:11 +02:00
|
|
|
return val
|
|
|
|
|
|
|
|
def update(self):
|
2020-04-12 20:36:32 +02:00
|
|
|
if self.__thread is not None and self.__thread.isAlive():
|
2020-04-12 20:28:11 +02:00
|
|
|
return
|
2020-04-12 20:36:32 +02:00
|
|
|
self.__thread = threading.Thread(target=get_redshift_value, args=(self,))
|
2020-04-12 20:28:11 +02:00
|
|
|
self.__thread.start()
|
2020-03-07 14:06:01 +01:00
|
|
|
|
|
|
|
def state(self, widget):
|
2020-05-03 11:15:52 +02:00
|
|
|
return widget.get("state", None)
|
|
|
|
|
2020-03-07 14:06:01 +01:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|