From f9132bd7fd9035c7e78958b752ccb1ce2df28e00 Mon Sep 17 00:00:00 2001 From: Bas van den Heuvel Date: Wed, 21 Aug 2019 18:26:59 +0200 Subject: [PATCH 1/2] PEP8 --- bumblebee/modules/redshift.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/bumblebee/modules/redshift.py b/bumblebee/modules/redshift.py index 47b9cc4..2ed0b87 100644 --- a/bumblebee/modules/redshift.py +++ b/bumblebee/modules/redshift.py @@ -12,12 +12,14 @@ 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(): @@ -52,6 +54,7 @@ def get_redshift_value(widget): widget.set("state", "transition") widget.set("transition", " ".join(line.split(" ")[2:])) + class Module(bumblebee.engine.Module): def __init__(self, engine, config): widget = bumblebee.output.Widget(full_text=self.text) @@ -59,7 +62,8 @@ class Module(bumblebee.engine.Module): self._text = "" self._condition = threading.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._thread.start() self._condition.acquire() self._condition.notify() From ced31c3a22cab850ef623f2fa8c3534815ae8508 Mon Sep 17 00:00:00 2001 From: Bas van den Heuvel Date: Wed, 21 Aug 2019 19:12:58 +0200 Subject: [PATCH 2/2] Add location options to redshift module --- bumblebee/modules/redshift.py | 43 ++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/bumblebee/modules/redshift.py b/bumblebee/modules/redshift.py index 2ed0b87..1db598f 100644 --- a/bumblebee/modules/redshift.py +++ b/bumblebee/modules/redshift.py @@ -4,9 +4,16 @@ Requires the following executable: * 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 requests import bumblebee.input import bumblebee.output @@ -20,7 +27,7 @@ def is_terminated(): return False -def get_redshift_value(widget): +def get_redshift_value(widget, location, lat, lon): while True: if is_terminated(): return @@ -33,8 +40,14 @@ def get_redshift_value(widget): break widget.get("condition").release() + command = ["redshift", "-p", "-l"] + if location == "manual": + command.append(lat + ":" + lon) + else: + command.append("geoclue2") + try: - res = bumblebee.util.execute("redshift -p") + res = bumblebee.util.execute(" ".join(command)) except Exception: res = "" widget.set("temp", "n/a") @@ -59,11 +72,35 @@ 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._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._condition = threading.Condition() widget.set("condition", self._condition) self._thread = threading.Thread(target=get_redshift_value, - args=(widget,)) + args=(widget, self._location, + self._lat, self._lon)) self._thread.start() self._condition.acquire() self._condition.notify()