From 87fb82965f557ed5a3d1717957dc6cade18078ec Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Sun, 19 Apr 2020 10:04:01 +0200 Subject: [PATCH] [modules/sun] Update to latest API --- modules/contrib/sun.py | 113 +++++++++++++++++------------------------ 1 file changed, 48 insertions(+), 65 deletions(-) diff --git a/modules/contrib/sun.py b/modules/contrib/sun.py index 79e6c57..f77ffcc 100644 --- a/modules/contrib/sun.py +++ b/modules/contrib/sun.py @@ -11,107 +11,90 @@ Parameters: * cpu.lon : Longitude of your location """ -try: - from suntime import Sun, SunTimeException -except ImportError: - pass -try: - import requests -except ImportError: - pass -try: - from dateutil.tz import tzlocal -except ImportError: - pass +from suntime import Sun, SunTimeException +import requests +from dateutil.tz import tzlocal import datetime -import bumblebee.input -import bumblebee.output -import bumblebee.engine +import core.module +import core.widget +import core.decorators +import util.location -class Module(bumblebee.engine.Module): - def __init__(self, engine, config): - super(Module, self).__init__( - engine, config, - bumblebee.output.Widget(full_text=self.suntimes) - ) - self.interval(3600) - self._lat = self.parameter('lat', None) - self._lon = self.parameter('lon', None) - try: - if not self._lat or not self._lon: - location_url = 'http://ipinfo.io/json' - location = requests.get(location_url).json() - self._lat, self._lon = location['loc'].split(',') - self._lat = float(self._lat) - self._lon = float(self._lon) - except Exception: - pass - self.update(None) +class Module(core.module.Module): + @core.decorators.every(hours=1) + def __init__(self, config): + super().__init__(config, core.widget.Widget(self.suntimes)) + + self.__lat = self.parameter('lat', None) + self.__lon = self.parameter('lon', None) + + if not self.__lat or not self.__lon: + self.__lat, self.__lon = util.location.coordinates() def suntimes(self, _): - if self._sunset and self._sunrise: - if self._isup: + if self.__sunset and self.__sunrise: + if self.__isup: return u'\u21A7{} \u21A5{}'.format( - self._sunset.strftime('%H:%M'), - self._sunrise.strftime('%H:%M')) - return u'\u21A5{} \u21A7{}'.format(self._sunrise.strftime('%H:%M'), - self._sunset.strftime('%H:%M')) - return '?' + self.__sunset.strftime('%H:%M'), + self.__sunrise.strftime('%H:%M')) + return u'\u21A5{} \u21A7{}'.format(self.__sunrise.strftime('%H:%M'), + self.__sunset.strftime('%H:%M')) + return 'n/a' - def _calculate_times(self): - self._isup = False + def __calculate_times(self): + self.__isup = False try: - if not self._lat or not self._lon: + if not self.__lat or not self.__lon: raise() - sun = Sun(self._lat, self._lon) + sun = Sun(float(self.__lat), float(self.__lon)) except Exception: - self._sunrise = None - self._sunset = None + self.__sunrise = None + self.__sunset = None return order_matters = True try: - self._sunrise = sun.get_local_sunrise_time() + self.__sunrise = sun.get_local_sunrise_time() except SunTimeException: - self._sunrise = 'no sunrise' + self.__sunrise = 'no sunrise' order_matters = False try: - self._sunset = sun.get_local_sunset_time() + self.__sunset = sun.get_local_sunset_time() except SunTimeException: - self._sunset = 'no sunset' + self.__sunset = 'no sunset' order_matters = False if not order_matters: return now = datetime.datetime.now(tz=tzlocal()) - if now > self._sunset: + if now > self.__sunset: tomorrow = (now + datetime.timedelta(days=1)).date() try: - self._sunrise = sun.get_local_sunrise_time(tomorrow) - self._sunset = sun.get_local_sunset_time(tomorrow) + self.__sunrise = sun.get_local_sunrise_time(tomorrow) + self.__sunset = sun.get_local_sunset_time(tomorrow) except SunTimeException: - self._sunrise = 'no sunrise' - self._sunset = 'no sunset' + self.__sunrise = 'no sunrise' + self.__sunset = 'no sunset' - elif now > self._sunrise: + elif now > self.__sunrise: tomorrow = (now + datetime.timedelta(days=1)).date() try: - self._sunrise = sun.get_local_sunrise_time(tomorrow) + self.__sunrise = sun.get_local_sunrise_time(tomorrow) except SunTimeException: - self._sunrise = 'no sunrise' + self.__sunrise = 'no sunrise' return - self._isup = True + self.__isup = True - def update(self, widgets): - if not self._lat or not self._lon: - self._sunrise = None - self._sunset = None - self._calculate_times() + def update(self ): + if not self.__lat or not self.__lon: + self.__sunrise = None + self.__sunset = None + self.__calculate_times() # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4