From 57c8658adf1b71104f67bb87c8bc90bc8e00ee16 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 30 Dec 2018 21:28:50 -0800 Subject: [PATCH] NuWeather: make temperature display units configurable --- NuWeather/config.py | 8 ++++++++ NuWeather/plugin.py | 22 ++++++++++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/NuWeather/config.py b/NuWeather/config.py index aff286b..2d7af08 100644 --- a/NuWeather/config.py +++ b/NuWeather/config.py @@ -48,6 +48,14 @@ def configure(advanced): NuWeather = conf.registerPlugin('NuWeather') conf.registerGroup(NuWeather, 'apikeys') +conf.registerGroup(NuWeather, 'units') + +class NuWeatherTemperatureDisplayMode(registry.OnlySomeStrings): + validStrings = ('F/C', 'C/F', 'F', 'C') + +conf.registerChannelValue(NuWeather.units, 'temperature', + NuWeatherTemperatureDisplayMode('F/C', _("""Determines how temperatures will be displayed. + F/C means show "50F/10C", C means display only Celsius, and so on."""))) BACKENDS = ('apixu',) class NuWeatherBackend(registry.OnlySomeStrings): diff --git a/NuWeather/plugin.py b/NuWeather/plugin.py index fda6c0e..f3f9937 100644 --- a/NuWeather/plugin.py +++ b/NuWeather/plugin.py @@ -56,8 +56,7 @@ class NuWeather(callbacks.Plugin): self.db.flush() super().die() - @staticmethod - def _format_temp(f, c=None): + def _format_temp(self, f, c=None, msg=None): f = float(f) if f < 10: color = 'light blue' @@ -75,9 +74,24 @@ class NuWeather(callbacks.Plugin): color = 'orange' else: color = 'red' + # Round to nearest tenth for display purposes if c is None: - c = round((f - 32) * 5/9) - string = '%sC/%sF' % (c, f) + c = round((f - 32) * 5/9, 1) + else: + c = round(c, 1) + f = round(f, 1) + + displaymode = self.registryValue('units.temperature', msg.args[0] if msg else None) + if displaymode == 'F/C': + string = '%sF/%sC' % (f, c) + elif displaymode == 'C/F': + string = '%sC/%sF' % (c, f) + elif displaymode == 'F': + string = '%sF' % f + elif displaymode == 'C': + string = '%sC' % c + else: + raise ValueError("Unknown display mode for temperature.") return ircutils.mircColor(string, color) @staticmethod