Somewhat fixed.

This commit is contained in:
Jeremy Fincher 2004-02-23 09:49:47 +00:00
parent 295eafcb37
commit 52230d16e1

View File

@ -43,6 +43,7 @@ import urllib
import conf import conf
import utils import utils
import webutils import webutils
import ircutils
import privmsgs import privmsgs
import registry import registry
import callbacks import callbacks
@ -53,6 +54,7 @@ unitAbbrevs['ce'] = 'celsius'
class WeatherUnit(registry.String): class WeatherUnit(registry.String):
def setValue(self, s): def setValue(self, s):
print '***', repr(s)
s = s.lower() s = s.lower()
if s not in unitAbbrevs: if s not in unitAbbrevs:
raise registry.InvalidRegistryValue,\ raise registry.InvalidRegistryValue,\
@ -68,17 +70,9 @@ class WeatherCommand(registry.String):
'Command must be one of %s' % utils.commaAndify(m) 'Command must be one of %s' % utils.commaAndify(m)
else: else:
method = getattr(Weather, s) method = getattr(Weather, s)
Weather.weather.__doc__ = method.__doc__ Weather.weather.im_func.__doc__ = method.__doc__
registry.String.setValue(self, s) registry.String.setValue(self, s)
conf.registerPlugin('Weather')
conf.registerChannelValue(conf.supybot.plugins.Weather, 'preferredUnit',
WeatherUnit('Fahrenheit', """Sets the default temperature unit to use when
reporting the weather."""))
conf.registerChannelValue(conf.supybot.plugins.Weather, 'weatherCommand',
WeatherCommand('cnn', """Sets the default command to use when retrieving
the weather."""))
class Weather(callbacks.Privmsg): class Weather(callbacks.Privmsg):
weatherCommands = ['ham', 'cnn'] weatherCommands = ['ham', 'cnn']
threaded = True threaded = True
@ -98,29 +92,30 @@ class Weather(callbacks.Privmsg):
realCommand(irc, msg, args) realCommand(irc, msg, args)
def _getTemp(self, temp, deg, unit, chan): def _getTemp(self, temp, deg, unit, chan):
default = self.registryValue('preferredUnit', chan).lower() default = self.registryValue('preferredUnit', chan)
default = unitAbbrevs[default]
unit = unit.lower() unit = unit.lower()
if unitAbbrevs[unit] == default: if unitAbbrevs[unit] == default:
# Short circuit if we're the same unit as the default.
return deg.join([temp, unit.upper()]) return deg.join([temp, unit.upper()])
try: try:
temp = int(temp) temp = int(temp)
except ValueError: except ValueError:
# Bail out if we can't even int the temp.
return deg.join([temp, unit.upper()]) return deg.join([temp, unit.upper()])
if unit == 'f': if unit == 'f':
temp = (temp - 32) * 5 / 9 temp = (temp - 32) * 5 / 9
if default == 'kelvin': if default == 'Kelvin':
temp = temp + 273.15 temp = temp + 273.15
unit = 'K' unit = 'K'
deg = ' ' deg = ' '
else: else:
unit = 'C' unit = 'C'
elif unit == 'c': elif unit == 'c':
if default == 'kelvin': if default == 'Kelvin':
temp = temp + 273.15 temp = temp + 273.15
unit = 'K' unit = 'K'
deg = ' ' deg = ' '
elif default == 'fahrenheit': elif default == 'Fahrenheit':
temp = temp * 9 / 5 + 32 temp = temp * 9 / 5 + 32
unit = 'F' unit = 'F'
return deg.join([str(temp), unit.upper()]) return deg.join([str(temp), unit.upper()])
@ -317,6 +312,14 @@ class Weather(callbacks.Privmsg):
else: else:
irc.error('Could not find weather information.') irc.error('Could not find weather information.')
conf.registerPlugin('Weather')
conf.registerChannelValue(conf.supybot.plugins.Weather, 'preferredUnit',
WeatherUnit('Fahrenheit', """Sets the default temperature unit to use when
reporting the weather."""))
conf.registerChannelValue(conf.supybot.plugins.Weather, 'weatherCommand',
WeatherCommand('cnn', """Sets the default command to use when retrieving
the weather."""))
Class = Weather Class = Weather
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: