Redshift module fixes

Small rewrite in redshift module, removing regex usage in favor of
split()/strip(). I removed the function declaration inside function
declaration too, since it was strange.

Fix some typos in documentation too.
This commit is contained in:
Thiago Kenji Okada 2017-01-13 21:33:27 -02:00 committed by enkore
parent ec67429456
commit 0cc39bc9d6

View File

@ -1,5 +1,4 @@
import os import os
import re
import signal import signal
import threading import threading
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
@ -17,9 +16,6 @@ class RedshiftController(threading.Thread):
threading.Thread.__init__(self) threading.Thread.__init__(self)
# Regex to parse output
self._regex = re.compile(r'([\w ]+): (.+)')
# Initialize state variables # Initialize state variables
self._inhibited = False self._inhibited = False
self._temperature = 0 self._temperature = 0
@ -45,37 +41,27 @@ class RedshiftController(threading.Thread):
def parse_output(self, line): def parse_output(self, line):
"""Convert output to key value pairs""" """Convert output to key value pairs"""
if line: try:
m = self._regex.match(line) key, value = line.split(":")
if m: self.update_value(key, value.strip())
key = m.group(1) except ValueError:
value = m.group(2) pass
self.update_value(key, value)
def update_value(self, key, value): def update_value(self, key, value):
"""Parse key value pairs to update their values""" """Parse key value pairs to update their values"""
def parse_coord(s): if key == "Status":
"""Parse coordinate like `42.0 N` or `91.5 W`""" self._inhibited = value != "Enabled"
v, d = s.split(' ') elif key == "Color temperature":
return float(v) * (1 if d in 'NE' else -1) self._temperature = int(value.rstrip("K"), 10)
elif key == "Period":
if key == 'Status': self._period = value
new_inhibited = value != 'Enabled' elif key == "Location":
if new_inhibited != self._inhibited: location = []
self._inhibited = new_inhibited for x in value.split(", "):
elif key == 'Color temperature': v, d = x.split(" ")
new_temperature = int(value.rstrip('K'), 10) location.append(float(v) * (1 if d in "NE" else -1))
if new_temperature != self._temperature: self._location = (location)
self._temperature = new_temperature
elif key == 'Period':
new_period = value
if new_period != self._period:
self._period = new_period
elif key == 'Location':
new_location = tuple(parse_coord(x) for x in value.split(', '))
if new_location != self._location:
self._location = new_location
@property @property
def inhibited(self): def inhibited(self):
@ -115,7 +101,7 @@ class Redshift(IntervalModule):
""" """
Show status and control redshift - http://jonls.dk/redshift/. Show status and control redshift - http://jonls.dk/redshift/.
This module runs a instace of redshift by itself, since it needs to parse This module runs an instance of redshift by itself, since it needs to parse
its output, so you should remove redshift/redshift-gtk from your i3 config its output, so you should remove redshift/redshift-gtk from your i3 config
before using this module. before using this module.