A few more fixes/cleanups

This commit is contained in:
James Vega 2004-02-23 11:08:22 +00:00
parent ad1a23fcd2
commit e5ce3c8ee9

View File

@ -48,18 +48,18 @@ import privmsgs
import registry import registry
import callbacks import callbacks
unitAbbrevs = utils.abbrev(['fahrenheit', 'celsius', 'centigrade', 'kelvin']) unitAbbrevs = utils.abbrev(['Fahrenheit', 'Celsius', 'Centigrade', 'Kelvin'])
unitAbbrevs['c'] = 'celsius' unitAbbrevs['C'] = 'Celsius'
unitAbbrevs['ce'] = 'celsius' unitAbbrevs['Ce'] = 'Celsius'
class WeatherUnit(registry.String): class WeatherUnit(registry.String):
def setValue(self, s): def setValue(self, s):
print '***', repr(s) #print '***', repr(s)
s = s.lower() s = s.capitalize()
if s not in unitAbbrevs: if s not in unitAbbrevs:
raise registry.InvalidRegistryValue,\ raise registry.InvalidRegistryValue,\
'Unit must be one of Fahrenheit, Celsius, or Kelvin.' 'Unit must be one of Fahrenheit, Celsius, or Kelvin.'
s = unitAbbrevs[s].capitalize() s = unitAbbrevs[s]
registry.String.setValue(self, s) registry.String.setValue(self, s)
class WeatherCommand(registry.String): class WeatherCommand(registry.String):
@ -91,34 +91,29 @@ class Weather(callbacks.Privmsg):
realCommand = getattr(self, realCommandName) realCommand = getattr(self, realCommandName)
realCommand(irc, msg, args) 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): def _getTemp(self, temp, deg, unit, chan):
default = self.registryValue('preferredUnit', chan) assert unit == unit.upper()
unit = unit.lower() assert temp == int(temp)
default = self.registryValue('temperatureUnit', chan)
if unitAbbrevs[unit] == default: if unitAbbrevs[unit] == default:
# Short circuit if we're the same unit as the default. # Short circuit if we're the same unit as the default.
return deg.join([temp, unit.upper()]) return '%s%s%s' % (temp, deg, unit)
try: temp = self._toCelsius(temp, unit)
temp = int(temp) unit = 'C'
except ValueError: if default == 'Kelvin':
# Bail out if we can't even int the temp. temp = temp + 273.15
return deg.join([temp, unit.upper()]) unit = 'K'
if unit == 'f': deg = ' '
temp = (temp - 32) * 5 / 9 elif default == 'Fahrenheit':
if default == 'Kelvin': temp = temp * 9 / 5 + 32
temp = temp + 273.15 unit = 'F'
unit = 'K' return '%s%s%s' % (temp, deg, unit)
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()])
_cityregex = re.compile( _cityregex = re.compile(
r'<td><font size="4" face="arial"><b>' r'<td><font size="4" face="arial"><b>'
@ -131,13 +126,13 @@ class Weather(callbacks.Privmsg):
r'<font face="arial">([^<]+)</font></strong></td>', re.I) r'<font face="arial">([^<]+)</font></strong></td>', re.I)
_tempregex = re.compile( _tempregex = re.compile(
r'<td valign="top" align="right"><strong><font face="arial">' r'<td valign="top" align="right"><strong><font face="arial">'
r'([^<]+)</font></strong></td>', re.I) r'(-?\d+)(.*?)(F|C)</font></strong></td>', re.I)
_chillregex = re.compile( _chillregex = re.compile(
r'Wind Chill</font></strong>:</small></a></td>\s+<td align="right">' r'Wind Chill</font></strong>:</small></td>\s+<td align="right">'
r'<small><font face="arial">([^N][^<]+)</font></small></td>', r'<small><font face="arial">([^N][^<]+)</font></small></td>',
re.I | re.S) re.I | re.S)
_heatregex = re.compile( _heatregex = re.compile(
r'Heat Index</font></strong>:</small></a></td>\s+<td align="right">' r'Heat Index</font></strong>:</small></td>\s+<td align="right">'
r'<small><font face="arial">([^N][^<]+)</font></small></td>', r'<small><font face="arial">([^N][^<]+)</font></small></td>',
re.I | re.S) re.I | re.S)
# States # States
@ -225,9 +220,8 @@ class Weather(callbacks.Privmsg):
state = state.strip() state = state.strip()
temp = self._tempregex.search(html) temp = self._tempregex.search(html)
if temp is not None: if temp is not None:
temp = temp.group(1) (temp, deg, unit) = temp.groups()
(temp, deg, unit) = (temp[:-2], temp[-2], temp[-1]) temp = self._getTemp(int(temp), deg, unit, msg.args[0])
temp = self._getTemp(temp, deg, unit, msg.args[0])
conds = self._condregex.search(html) conds = self._condregex.search(html)
if conds is not None: if conds is not None:
conds = conds.group(1) conds = conds.group(1)
@ -236,11 +230,13 @@ class Weather(callbacks.Privmsg):
if chill is not None: if chill is not None:
#self.log.warning(chill.groups()) #self.log.warning(chill.groups())
chill = chill.group(1) chill = chill.group(1)
chill = utils.htmlToText(chill)
if int(chill[:-2]) < int(temp[:-2]): if int(chill[:-2]) < int(temp[:-2]):
index = ' (Wind Chill: %s)' % chill index = ' (Wind Chill: %s)' % chill
heat = self._heatregex.search(html) heat = self._heatregex.search(html)
if heat is not None: if heat is not None:
heat = heat.group(1) heat = heat.group(1)
heat = utils.htmlToText(heat)
if int(heat[:-2]) > int(temp[:-2]): if int(heat[:-2]) > int(temp[:-2]):
index = ' (Heat Index: %s)' % heat index = ' (Heat Index: %s)' % heat
if temp and conds and city and state: if temp and conds and city and state:
@ -298,7 +294,7 @@ class Weather(callbacks.Privmsg):
location = location.group(1) location = location.group(1)
location = location.split('-')[-1].strip() location = location.split('-')[-1].strip()
(temp, deg, unit) = temp.groups() (temp, deg, unit) = temp.groups()
temp = self._getTemp(temp, deg, unit, msg.args[0]) temp = self._getTemp(int(temp), deg, unit, msg.args[0])
resp = 'The current temperature in %s is %s.' % (location, temp) resp = 'The current temperature in %s is %s.' % (location, temp)
resp = [resp] resp = [resp]
if conds is not None: if conds is not None:
@ -313,7 +309,7 @@ class Weather(callbacks.Privmsg):
irc.error('Could not find weather information.') irc.error('Could not find weather information.')
conf.registerPlugin('Weather') conf.registerPlugin('Weather')
conf.registerChannelValue(conf.supybot.plugins.Weather, 'preferredUnit', conf.registerChannelValue(conf.supybot.plugins.Weather, 'temperatureUnit',
WeatherUnit('Fahrenheit', """Sets the default temperature unit to use when WeatherUnit('Fahrenheit', """Sets the default temperature unit to use when
reporting the weather.""")) reporting the weather."""))
conf.registerChannelValue(conf.supybot.plugins.Weather, 'weatherCommand', conf.registerChannelValue(conf.supybot.plugins.Weather, 'weatherCommand',