diff --git a/plugins/Weather.py b/plugins/Weather.py index 0eb49e941..c63004463 100644 --- a/plugins/Weather.py +++ b/plugins/Weather.py @@ -48,18 +48,18 @@ import privmsgs import registry import callbacks -unitAbbrevs = utils.abbrev(['fahrenheit', 'celsius', 'centigrade', 'kelvin']) -unitAbbrevs['c'] = 'celsius' -unitAbbrevs['ce'] = 'celsius' +unitAbbrevs = utils.abbrev(['Fahrenheit', 'Celsius', 'Centigrade', 'Kelvin']) +unitAbbrevs['C'] = 'Celsius' +unitAbbrevs['Ce'] = 'Celsius' class WeatherUnit(registry.String): def setValue(self, s): - print '***', repr(s) - s = s.lower() + #print '***', repr(s) + s = s.capitalize() if s not in unitAbbrevs: raise registry.InvalidRegistryValue,\ 'Unit must be one of Fahrenheit, Celsius, or Kelvin.' - s = unitAbbrevs[s].capitalize() + s = unitAbbrevs[s] registry.String.setValue(self, s) class WeatherCommand(registry.String): @@ -91,34 +91,29 @@ class Weather(callbacks.Privmsg): realCommand = getattr(self, realCommandName) realCommand(irc, msg, args) + def _toCelsius(self, temp, unit): + if unit == 'K': + return temp - 273.15 + else: + return (temp - 32) * 5 /9 + def _getTemp(self, temp, deg, unit, chan): - default = self.registryValue('preferredUnit', chan) - unit = unit.lower() + assert unit == unit.upper() + assert temp == int(temp) + default = self.registryValue('temperatureUnit', chan) if unitAbbrevs[unit] == default: # Short circuit if we're the same unit as the default. - return deg.join([temp, unit.upper()]) - try: - temp = int(temp) - except ValueError: - # Bail out if we can't even int the temp. - return deg.join([temp, unit.upper()]) - if unit == 'f': - temp = (temp - 32) * 5 / 9 - if default == 'Kelvin': - temp = temp + 273.15 - unit = 'K' - deg = ' ' - else: - unit = 'C' - elif unit == 'c': - if default == 'Kelvin': - temp = temp + 273.15 - unit = 'K' - deg = ' ' - elif default == 'Fahrenheit': - temp = temp * 9 / 5 + 32 - unit = 'F' - return deg.join([str(temp), unit.upper()]) + return '%s%s%s' % (temp, deg, unit) + temp = self._toCelsius(temp, unit) + unit = 'C' + if default == 'Kelvin': + temp = temp + 273.15 + unit = 'K' + deg = ' ' + elif default == 'Fahrenheit': + temp = temp * 9 / 5 + 32 + unit = 'F' + return '%s%s%s' % (temp, deg, unit) _cityregex = re.compile( r'