[modules/weather] Small improvements
This commit is contained in:
parent
a7effbff78
commit
a71828f0b4
4 changed files with 21 additions and 21 deletions
|
@ -35,5 +35,3 @@
|
||||||
- themes: use colors to improve theme readability
|
- themes: use colors to improve theme readability
|
||||||
- brightness: read from CLI tools
|
- brightness: read from CLI tools
|
||||||
- input: use events?
|
- input: use events?
|
||||||
- themes: rotating icons (battery!)
|
|
||||||
- weather: improve code
|
|
||||||
|
|
|
@ -24,11 +24,8 @@ import util.format
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
try:
|
|
||||||
import requests
|
import requests
|
||||||
from requests.exceptions import RequestException
|
from requests.exceptions import RequestException
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
class Module(core.module.Module):
|
class Module(core.module.Module):
|
||||||
@core.decorators.every(minutes=15)
|
@core.decorators.every(minutes=15)
|
||||||
|
@ -50,29 +47,20 @@ class Module(core.module.Module):
|
||||||
|
|
||||||
def __next_location(self, event):
|
def __next_location(self, event):
|
||||||
self.__index = (self.__index + 1) % len(self.__location)
|
self.__index = (self.__index + 1) % len(self.__location)
|
||||||
self.update(self.widgets())
|
self.update()
|
||||||
|
|
||||||
def __prev_location(self, event):
|
def __prev_location(self, event):
|
||||||
self.__index = len(self.__location) - 1 if self.__index <= 0 else self.__index - 1
|
self.__index = len(self.__location) - 1 if self.__index <= 0 else self.__index - 1
|
||||||
self.update(self.widgets())
|
self.update()
|
||||||
|
|
||||||
def __unit_suffix(self):
|
|
||||||
if self.__unit == 'metric':
|
|
||||||
return 'C'
|
|
||||||
if self.__unit == 'kelvin':
|
|
||||||
return 'K'
|
|
||||||
if self.__unit == 'imperial':
|
|
||||||
return 'F'
|
|
||||||
return ''
|
|
||||||
|
|
||||||
def temperature(self):
|
def temperature(self):
|
||||||
return u'{}°{}'.format(self.__temperature, self.__unit_suffix())
|
return util.format.astemperature(self.__temperature, self.__unit)
|
||||||
|
|
||||||
def tempmin(self):
|
def tempmin(self):
|
||||||
return u'{}°{}'.format(self.__tempmin, self.__unit_suffix())
|
return util.format.astemperature(self.__tempmin, self.__unit)
|
||||||
|
|
||||||
def tempmax(self):
|
def tempmax(self):
|
||||||
return u'{}°{}'.format(self.__tempmax, self.__unit_suffix())
|
return util.format.astemperature(self.__tempmax, self.__unit)
|
||||||
|
|
||||||
def city(self):
|
def city(self):
|
||||||
city = re.sub('[_-]', ' ', self.__city)
|
city = re.sub('[_-]', ' ', self.__city)
|
||||||
|
@ -113,6 +101,7 @@ class Module(core.module.Module):
|
||||||
weather_url = 'http://api.openweathermap.org/data/2.5/weather?appid={}'.format(self.__apikey)
|
weather_url = 'http://api.openweathermap.org/data/2.5/weather?appid={}'.format(self.__apikey)
|
||||||
weather_url = '{}&units={}'.format(weather_url, self.__unit)
|
weather_url = '{}&units={}'.format(weather_url, self.__unit)
|
||||||
if self.__location[self.__index] == 'auto':
|
if self.__location[self.__index] == 'auto':
|
||||||
|
# TODO: make that a general "provider"
|
||||||
location_url = 'http://ipinfo.io/json'
|
location_url = 'http://ipinfo.io/json'
|
||||||
location = requests.get(location_url).json()
|
location = requests.get(location_url).json()
|
||||||
coord = location['loc'].split(',')
|
coord = location['loc'].split(',')
|
||||||
|
|
|
@ -88,4 +88,10 @@ class format(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(4*3600 + 5*60, seconds('4h5m'))
|
self.assertEqual(4*3600 + 5*60, seconds('4h5m'))
|
||||||
|
|
||||||
|
def test_temperature(self):
|
||||||
|
self.assertEqual('10°C', astemperature(10))
|
||||||
|
self.assertEqual('10°C', astemperature(10, 'metric'))
|
||||||
|
self.assertEqual('-100°F', astemperature(-100, 'imperial'))
|
||||||
|
self.assertEqual('-100°K', astemperature('-100', 'kelvin'))
|
||||||
|
|
||||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||||
|
|
|
@ -23,6 +23,13 @@ def aslist(val):
|
||||||
return val
|
return val
|
||||||
return str(val).replace(' ', '').split(',')
|
return str(val).replace(' ', '').split(',')
|
||||||
|
|
||||||
|
__UNITS = {
|
||||||
|
'metric': 'C', 'kelvin': 'K', 'imperial': 'F',
|
||||||
|
'default': 'C'
|
||||||
|
}
|
||||||
|
def astemperature(value, unit='metric'):
|
||||||
|
return u'{}°{}'.format(int(value), __UNITS.get(unit, __UNITS['default']))
|
||||||
|
|
||||||
def byte(val, fmt='{:.2f}'):
|
def byte(val, fmt='{:.2f}'):
|
||||||
for unit in ['', 'Ki', 'Mi', 'Gi']:
|
for unit in ['', 'Ki', 'Mi', 'Gi']:
|
||||||
if val < 1024.0:
|
if val < 1024.0:
|
||||||
|
|
Loading…
Reference in a new issue