[modules/redshift] Simplify structure
This commit is contained in:
parent
e36694c03d
commit
d87b18a824
1 changed files with 50 additions and 78 deletions
|
@ -28,52 +28,44 @@ import core.decorators
|
||||||
|
|
||||||
import util.cli
|
import util.cli
|
||||||
|
|
||||||
def is_terminated():
|
def get_redshift_value(module):
|
||||||
for thread in threading.enumerate():
|
widget = module.widget()
|
||||||
if thread.name == 'MainThread' and not thread.is_alive():
|
location = module.parameter('location', 'auto')
|
||||||
return True
|
lat = module.parameter('lat', None)
|
||||||
return False
|
lon = module.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 location == 'manual' and (lat is None or lon is None):
|
||||||
|
location = 'geoclue2'
|
||||||
|
|
||||||
def get_redshift_value(widget, location, lat, lon):
|
command = ['redshift', '-p']
|
||||||
while True:
|
if location == 'manual':
|
||||||
if is_terminated():
|
command.extend(['-l', '{}:{}'.format(lat, lon)])
|
||||||
return
|
if location == 'geoclue2':
|
||||||
widget.get('condition').acquire()
|
command.extend(['-l', 'geoclue2'])
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
widget.get('condition').wait(1)
|
|
||||||
except RuntimeError:
|
|
||||||
continue
|
|
||||||
break
|
|
||||||
widget.get('condition').release()
|
|
||||||
|
|
||||||
command = ['redshift', '-p']
|
try:
|
||||||
if location == 'manual':
|
res = util.cli.execute(' '.join(command))
|
||||||
command.extend(['-l', '{}:{}'.format(lat, lon)])
|
except Exception:
|
||||||
if location == 'geoclue2':
|
res = ''
|
||||||
command.extend(['-l', 'geoclue2'])
|
widget.set('temp', 'n/a')
|
||||||
|
widget.set('transition', None)
|
||||||
try:
|
widget.set('state', 'day')
|
||||||
res = util.cli.execute(' '.join(command))
|
for line in res.split('\n'):
|
||||||
except Exception:
|
line = line.lower()
|
||||||
res = ''
|
if 'temperature' in line:
|
||||||
widget.set('temp', 'n/a')
|
widget.set('temp', line.split(' ')[2])
|
||||||
widget.set('transition', None)
|
if 'period' in line:
|
||||||
widget.set('state', 'day')
|
state = line.split(' ')[1]
|
||||||
for line in res.split('\n'):
|
if 'day' in state:
|
||||||
line = line.lower()
|
widget.set('state', 'day')
|
||||||
if 'temperature' in line:
|
elif 'night' in state:
|
||||||
widget.set('temp', line.split(' ')[2])
|
widget.set('state', 'night')
|
||||||
if 'period' in line:
|
else:
|
||||||
state = line.split(' ')[1]
|
widget.set('state', 'transition')
|
||||||
if 'day' in state:
|
widget.set('transition', ' '.join(line.split(' ')[2:]))
|
||||||
widget.set('state', 'day')
|
core.event.trigger('update', [ widget.module().id ], redraw_only=True)
|
||||||
elif 'night' in state:
|
|
||||||
widget.set('state', 'night')
|
|
||||||
else:
|
|
||||||
widget.set('state', 'transition')
|
|
||||||
widget.set('transition', ' '.join(line.split(' ')[2:]))
|
|
||||||
|
|
||||||
class Module(core.module.Module):
|
class Module(core.module.Module):
|
||||||
@core.decorators.every(seconds=10)
|
@core.decorators.every(seconds=10)
|
||||||
|
@ -81,53 +73,33 @@ class Module(core.module.Module):
|
||||||
widget = core.widget.Widget(self.text)
|
widget = core.widget.Widget(self.text)
|
||||||
super().__init__(config, widget)
|
super().__init__(config, widget)
|
||||||
|
|
||||||
self._location = self.parameter('location', 'auto')
|
self.__thread = threading.Thread(target=get_redshift_value, args=(self,))
|
||||||
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,
|
if self.parameter('location', '') == 'ipinfo':
|
||||||
# fall back to the geoclue2 method.
|
# override lon/lat with ipinfo
|
||||||
#
|
|
||||||
if self._location == 'manual' and (self._lat is None
|
|
||||||
or self._lon is None):
|
|
||||||
self._location == 'geoclue2'
|
|
||||||
|
|
||||||
if self._location == 'ipinfo':
|
|
||||||
try:
|
try:
|
||||||
location_url = 'http://ipinfo.io/json'
|
location_url = 'http://ipinfo.io/json'
|
||||||
location = requests.get(location_url).json()
|
location = requests.get(location_url).json()
|
||||||
self._lat, self._lon = location['loc'].split(',')
|
self.parameter('lat', location['loc'].split(',')[0])
|
||||||
self._lat = str(float(self._lat))
|
self.parameter('lon', location['loc'].split(',')[1])
|
||||||
self._lon = str(float(self._lon))
|
self.parameter('location', 'manual')
|
||||||
self._location = 'manual'
|
|
||||||
except Exception:
|
except Exception:
|
||||||
# Fall back to geoclue2.
|
# Fall back to geoclue2.
|
||||||
self._location = 'geoclue2'
|
self.parameter('location', 'geoclue2')
|
||||||
|
|
||||||
self._text = ''
|
self._text = ''
|
||||||
self._condition = threading.Condition()
|
|
||||||
widget.set('condition', self._condition)
|
|
||||||
self._thread = threading.Thread(target=get_redshift_value,
|
|
||||||
args=(widget, self._location,
|
|
||||||
self._lat, self._lon))
|
|
||||||
self._thread.start()
|
|
||||||
self._condition.acquire()
|
|
||||||
self._condition.notify()
|
|
||||||
self._condition.release()
|
|
||||||
|
|
||||||
def text(self, widget):
|
def text(self, widget):
|
||||||
return '{}'.format(self._text)
|
val = widget.get('temp', 'n/a')
|
||||||
|
|
||||||
def update(self):
|
|
||||||
widget = self.widget()
|
|
||||||
self._condition.acquire()
|
|
||||||
self._condition.notify()
|
|
||||||
self._condition.release()
|
|
||||||
temp = widget.get('temp', 'n/a')
|
|
||||||
self._text = temp
|
|
||||||
transition = widget.get('transition', None)
|
transition = widget.get('transition', None)
|
||||||
if transition:
|
if transition:
|
||||||
self._text = '{} {}'.format(temp, transition)
|
val = '{} {}'.format(val, transition)
|
||||||
|
return val
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
if self.__thread.isAlive():
|
||||||
|
return
|
||||||
|
self.__thread.start()
|
||||||
|
|
||||||
def state(self, widget):
|
def state(self, widget):
|
||||||
return widget.get('state', None)
|
return widget.get('state', None)
|
||||||
|
|
Loading…
Reference in a new issue