Merge pull request #424 from basvdheuvel/redshift_location_options

Add location options to the redshift module
This commit is contained in:
tobi-wan-kenobi 2019-08-22 18:37:54 +02:00 committed by GitHub
commit c2c195bf9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,21 +4,30 @@
Requires the following executable: Requires the following executable:
* redshift * redshift
Parameters:
* redshift.location : location provider, either of "geoclue2" (default), \
"ipinfo" (requires the requests package), or "manual"
* redshift.lat : latitude if location is set to "manual"
* redshift.lon : longitude if location is set to "manual"
""" """
import threading import threading
import requests
import bumblebee.input import bumblebee.input
import bumblebee.output import bumblebee.output
import bumblebee.engine import bumblebee.engine
def is_terminated(): def is_terminated():
for thread in threading.enumerate(): for thread in threading.enumerate():
if thread.name == "MainThread" and not thread.is_alive(): if thread.name == "MainThread" and not thread.is_alive():
return True return True
return False return False
def get_redshift_value(widget):
def get_redshift_value(widget, location, lat, lon):
while True: while True:
if is_terminated(): if is_terminated():
return return
@ -31,8 +40,14 @@ def get_redshift_value(widget):
break break
widget.get("condition").release() widget.get("condition").release()
command = ["redshift", "-p", "-l"]
if location == "manual":
command.append(lat + ":" + lon)
else:
command.append("geoclue2")
try: try:
res = bumblebee.util.execute("redshift -p") res = bumblebee.util.execute(" ".join(command))
except Exception: except Exception:
res = "" res = ""
widget.set("temp", "n/a") widget.set("temp", "n/a")
@ -52,14 +67,40 @@ def get_redshift_value(widget):
widget.set("state", "transition") widget.set("state", "transition")
widget.set("transition", " ".join(line.split(" ")[2:])) widget.set("transition", " ".join(line.split(" ")[2:]))
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
widget = bumblebee.output.Widget(full_text=self.text) widget = bumblebee.output.Widget(full_text=self.text)
super(Module, self).__init__(engine, config, widget) super(Module, self).__init__(engine, config, widget)
self._location = self.parameter("location", "geoclue2")
self._lat = self.parameter("lat", None)
self._lon = self.parameter("lon", None)
# Even if location method is set to manual, if we have no lat or lon,
# fall back to the geoclue2 method.
if self._location == "manual" and (self._lat is None
or self._lon is None):
self._location == "geoclue2"
if self._location == "ipinfo":
try:
location_url = "http://ipinfo.io/json"
location = requests.get(location_url).json()
self._lat, self._lon = location["loc"].split(",")
self._lat = str(float(self._lat))
self._lon = str(float(self._lon))
self._location = "manual"
except Exception:
# Fall back to geoclue2.
self._location = "geoclue2"
self._text = "" self._text = ""
self._condition = threading.Condition() self._condition = threading.Condition()
widget.set("condition", self._condition) widget.set("condition", self._condition)
self._thread = threading.Thread(target=get_redshift_value, args=(widget,)) self._thread = threading.Thread(target=get_redshift_value,
args=(widget, self._location,
self._lat, self._lon))
self._thread.start() self._thread.start()
self._condition.acquire() self._condition.acquire()
self._condition.notify() self._condition.notify()