From 403a5616716133ff72a09a57c4e19c7991feacf3 Mon Sep 17 00:00:00 2001 From: James Vega Date: Wed, 14 Apr 2004 00:26:08 +0000 Subject: [PATCH] Add supybot.plugins.Weather.convert, which determines weather or not whether will convert temperature to the configured unit. --- plugins/Weather.py | 35 +++++++++++++++++++++++++---------- test/test_Weather.py | 22 ++++++++++++++++++++++ 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/plugins/Weather.py b/plugins/Weather.py index ec911ca81..1442e9ff7 100644 --- a/plugins/Weather.py +++ b/plugins/Weather.py @@ -226,9 +226,13 @@ class Weather(callbacks.Privmsg): city = city.strip() state = state.strip() temp = self._tempregex.search(html) + convert = self.registryValue('convert', msg.args[0]) if temp is not None: (temp, deg, unit) = temp.groups() - temp = self._getTemp(int(temp), deg, unit, msg.args[0]) + if convert: + temp = self._getTemp(int(temp), deg, unit, msg.args[0]) + else: + temp = deg.join((temp, unit)) conds = self._condregex.search(html) if conds is not None: conds = conds.group(1) @@ -237,20 +241,23 @@ class Weather(callbacks.Privmsg): if chill is not None: chill = chill.group(1) chill = utils.htmlToText(chill) - tempsplit = self._temp.search(chill) - if tempsplit: - (chill, deg, unit) = tempsplit.groups() - chill = self._getTemp(int(chill), deg, unit,msg.args[0]) + if convert: + tempsplit = self._temp.search(chill) + if tempsplit: + (chill, deg, unit) = tempsplit.groups() + chill = self._getTemp(int(chill), deg, unit,msg.args[0]) if float(chill[:-2]) < float(temp[:-2]): index = ' (Wind Chill: %s)' % chill heat = self._heatregex.search(html) if heat is not None: heat = heat.group(1) heat = utils.htmlToText(heat) - tempsplit = self._temp.search(heat) - if tempsplit: - (heat, deg, unit) = tempsplit.groups() - heat = self._getTemp(int(heat), deg, unit,msg.args[0]) + if convert: + tempsplit = self._temp.search(heat) + if tempsplit: + (heat, deg, unit) = tempsplit.groups() + if convert: + heat = self._getTemp(int(heat), deg, unit,msg.args[0]) if float(heat[:-2]) > float(temp[:-2]): index = ' (Heat Index: %s)' % heat if temp and conds and city and state: @@ -304,11 +311,15 @@ class Weather(callbacks.Privmsg): conds = self._conds.search(text) humidity = self._humidity.search(text) wind = self._wind.search(text) + convert = self.registryValue('convert', msg.args[0]) if location and temp: location = location.group(1) location = location.split('-')[-1].strip() (temp, deg, unit) = temp.groups() - temp = self._getTemp(int(temp), deg, unit, msg.args[0]) + if convert: + temp = self._getTemp(int(temp), deg, unit, msg.args[0]) + else: + temp = deg.join((temp, unit)) resp = 'The current temperature in %s is %s.' % (location, temp) resp = [resp] if conds is not None: @@ -330,6 +341,10 @@ conf.registerChannelValue(conf.supybot.plugins.Weather, 'command', WeatherCommand('cnn', """Sets the default command to use when retrieving the weather. Command must be one of %s.""" % utils.commaAndify(Weather.weatherCommands, And='or'))) +conf.registerChannelValue(conf.supybot.plugins.Weather, 'convert', + registry.Boolean(True, """Determines whether the weather commands will + automatically convert weather units to the unit specified in + supybot.plugins.Weather.temperatureUnit.""")) Class = Weather diff --git a/test/test_Weather.py b/test/test_Weather.py index 413a2952b..6d92b0018 100644 --- a/test/test_Weather.py +++ b/test/test_Weather.py @@ -73,5 +73,27 @@ if network: def testNoEscapingWebError(self): self.assertNotRegexp('ham "buenos aires"', 'WebError') + def testConvertConfig(self): + try: + convert = conf.supybot.plugins.Weather.convert() + unit = conf.supybot.plugins.Weather.temperatureUnit() + conf.supybot.plugins.Weather.convert.setValue(False) + conf.supybot.plugins.Weather.temperatureUnit.setValue('C') + self.assertRegexp('ham london, gb', r'-?\d+.C') + self.assertRegexp('ham 02115', r'-?\d+.F') + conf.supybot.plugins.Weather.temperatureUnit.setValue('F') + self.assertRegexp('ham london, gb', r'-?\d+.C') + self.assertRegexp('ham 02115', r'-?\d+.F') + conf.supybot.plugins.Weather.convert.setValue(True) + conf.supybot.plugins.Weather.temperatureUnit.setValue('C') + self.assertRegexp('ham london, gb', r'-?\d+.C') + self.assertRegexp('ham 02115', r'-?\d+.C') + conf.supybot.plugins.Weather.temperatureUnit.setValue('F') + self.assertRegexp('ham london, gb', r'-?\d+.F') + self.assertRegexp('ham 02115', r'-?\d+.F') + finally: + conf.supybot.plugins.Weather.convert.setValue(convert) + conf.supybot.plugins.Weather.temperatureUnit.setValue(unit) + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: