allow wetboob to display temperatures either on celsius or on fahrenheit
Close #843
This commit is contained in:
parent
44681bb96e
commit
bfe39958bd
5 changed files with 61 additions and 21 deletions
|
|
@ -29,7 +29,7 @@ class MeteoFranceTest(BackendTest):
|
|||
|
||||
city = l[0]
|
||||
current = self.backend.get_current(city.id)
|
||||
self.assertTrue(current.temp > -20 and current.temp < 50)
|
||||
self.assertTrue(current.temp.value > -20 and current.temp.value < 50)
|
||||
|
||||
forecasts = list(self.backend.iter_forecast(city.id))
|
||||
self.assertTrue(len(forecasts) > 0)
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ class WeatherTest(BackendTest):
|
|||
self.assertTrue(len(list(paris)) == 1)
|
||||
|
||||
current = self.backend.get_current(paris[0].id)
|
||||
self.assertTrue(current.temp is float(current.temp))
|
||||
self.assertTrue(current.temp.value is float(current.temp.value))
|
||||
|
||||
forecasts = list(self.backend.iter_forecast(paris[0].id))
|
||||
self.assertTrue(len(forecasts) == 10)
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ class YahooTest(BackendTest):
|
|||
|
||||
city = l[0]
|
||||
current = self.backend.get_current(city.id)
|
||||
self.assertTrue(current.temp > -20 and current.temp < 50)
|
||||
self.assertTrue(current.temp.value > -20 and current.temp.value < 50)
|
||||
|
||||
forecasts = list(self.backend.iter_forecast(city.id))
|
||||
self.assertTrue(len(forecasts) > 0)
|
||||
|
|
|
|||
|
|
@ -26,24 +26,24 @@ from weboob.tools.application.formatters.iformatter import IFormatter, PrettyFor
|
|||
|
||||
__all__ = ['WetBoobs']
|
||||
|
||||
|
||||
class ForecastsFormatter(IFormatter):
|
||||
MANDATORY_FIELDS = ('id', 'date', 'low', 'high', 'unit')
|
||||
MANDATORY_FIELDS = ('id', 'date', 'low', 'high')
|
||||
|
||||
temperature_display = staticmethod(lambda t: u'%s' % t.value)
|
||||
|
||||
def format_obj(self, obj, alias):
|
||||
result = u'%s* %-15s%s (%s°%s - %s°%s)' % (self.BOLD, '%s:' % obj.date, self.NC, obj.low, obj.unit, obj.high, obj.unit)
|
||||
result = u'%s* %-15s%s (%s - %s)' % (self.BOLD, '%s:' % obj.date, self.NC, self.temperature_display(obj.low), self.temperature_display(obj.high))
|
||||
if hasattr(obj, 'text') and obj.text:
|
||||
result += ' %s' % obj.text
|
||||
return result
|
||||
|
||||
|
||||
class CurrentFormatter(IFormatter):
|
||||
MANDATORY_FIELDS = ('id', 'date', 'temp')
|
||||
|
||||
temperature_display = staticmethod(lambda t: u'%s' % t.value)
|
||||
|
||||
def format_obj(self, obj, alias):
|
||||
result = u'%s%s%s: %s' % (self.BOLD, obj.date, self.NC, obj.temp)
|
||||
if hasattr(obj, 'unit') and obj.unit:
|
||||
result += u'°%s' % obj.unit
|
||||
result = u'%s%s%s: %s' % (self.BOLD, obj.date, self.NC, self.temperature_display(obj.temp))
|
||||
if hasattr(obj, 'text') and obj.text:
|
||||
result += u' - %s' % obj.text
|
||||
return result
|
||||
|
|
@ -71,6 +71,10 @@ class WetBoobs(ReplApplication):
|
|||
'forecasts': 'forecasts',
|
||||
}
|
||||
|
||||
def main(self, argv):
|
||||
self.load_config()
|
||||
return ReplApplication.main(self, argv)
|
||||
|
||||
def do_cities(self, pattern):
|
||||
"""
|
||||
cities PATTERN
|
||||
|
|
@ -97,6 +101,12 @@ class WetBoobs(ReplApplication):
|
|||
city, = self.parse_command_args(line, 1, 1)
|
||||
_id, backend_name = self.parse_id(city)
|
||||
|
||||
tr = self.config.get('settings', 'temperature_display', default='C')
|
||||
if tr == 'C':
|
||||
self.formatter.temperature_display = lambda t: t.ascelsius()
|
||||
elif tr == 'F':
|
||||
self.formatter.temperature_display = lambda t: t.asfahrenheit()
|
||||
|
||||
self.start_format()
|
||||
for backend, current in self.do('get_current', _id, backends=backend_name, caps=ICapWeather):
|
||||
if current:
|
||||
|
|
@ -117,7 +127,13 @@ class WetBoobs(ReplApplication):
|
|||
city, = self.parse_command_args(line, 1, 1)
|
||||
_id, backend_name = self.parse_id(city)
|
||||
|
||||
tr = self.config.get('settings', 'temperature_display', default='C')
|
||||
if tr == 'C':
|
||||
self.formatter.temperature_display = lambda t: t.ascelsius()
|
||||
elif tr == 'F':
|
||||
self.formatter.temperature_display = lambda t: t.asfahrenheit()
|
||||
self.start_format()
|
||||
|
||||
for backend, forecast in self.do('iter_forecast', _id, backends=backend_name, caps=ICapWeather):
|
||||
self.format(forecast)
|
||||
self.flush()
|
||||
|
|
|
|||
|
|
@ -24,7 +24,35 @@ from .base import IBaseCap, CapBaseObject, Field, DateField, FloatField, \
|
|||
StringField, UserError
|
||||
|
||||
|
||||
__all__ = ['Forecast', 'Current', 'City', 'CityNotFound', 'ICapWeather']
|
||||
__all__ = ['Forecast', 'Current', 'City', 'CityNotFound', 'Temperature', 'ICapWeather']
|
||||
|
||||
|
||||
class Temperature(CapBaseObject):
|
||||
|
||||
value = FloatField('Temperature value')
|
||||
unit = StringField('Input unit')
|
||||
|
||||
def __init__(self, value, unit = u''):
|
||||
self.value = value
|
||||
if unit not in [u'C', u'F']:
|
||||
unit = u''
|
||||
self.unit = unit
|
||||
|
||||
def asfahrenheit(self):
|
||||
if not self.unit:
|
||||
return u'%s' % int(round(self.value))
|
||||
elif self.unit == 'F':
|
||||
return u'%sF' % int(round(self.value))
|
||||
else:
|
||||
return u'%s°F' % int(round((self.value * 9.0 / 5.0) + 32))
|
||||
|
||||
def ascelsius(self):
|
||||
if not self.unit:
|
||||
return u'%s' % int(round(self.value))
|
||||
elif self.unit == 'C':
|
||||
return u'%sC' % int(round(self.value))
|
||||
else:
|
||||
return u'%s°C' % int(round((self.value - 32.0) * 5.0 / 9.0))
|
||||
|
||||
|
||||
class Forecast(CapBaseObject):
|
||||
|
|
@ -32,18 +60,16 @@ class Forecast(CapBaseObject):
|
|||
Weather forecast.
|
||||
"""
|
||||
date = Field('Date for the forecast', datetime, basestring)
|
||||
low = FloatField('Low temperature')
|
||||
high = FloatField('High temperature')
|
||||
low = Field('Low temperature', Temperature)
|
||||
high = Field('High temperature', Temperature)
|
||||
text = StringField('Comment on forecast')
|
||||
unit = StringField('Unit used for temperatures')
|
||||
|
||||
def __init__(self, date, low, high, text, unit):
|
||||
CapBaseObject.__init__(self, unicode(date))
|
||||
self.date = date
|
||||
self.low = low
|
||||
self.high = high
|
||||
self.low = Temperature(low, unit)
|
||||
self.high = Temperature(high, unit)
|
||||
self.text = text
|
||||
self.unit = unit
|
||||
|
||||
class Current(CapBaseObject):
|
||||
"""
|
||||
|
|
@ -51,15 +77,13 @@ class Current(CapBaseObject):
|
|||
"""
|
||||
date = DateField('Date of measure')
|
||||
text = StringField('Comment about current weather')
|
||||
temp = FloatField('Current temperature')
|
||||
unit = StringField('Unit used for temperature')
|
||||
temp = Field('Current temperature', Temperature)
|
||||
|
||||
def __init__(self, date, temp, text, unit):
|
||||
CapBaseObject.__init__(self, unicode(date))
|
||||
self.date = date
|
||||
self.text = text
|
||||
self.temp = temp
|
||||
self.unit = unit
|
||||
self.temp = Temperature(temp, unit)
|
||||
|
||||
class City(CapBaseObject):
|
||||
"""
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue