From 2f86ef3b45867bdd0286b189a60a1760a341babe Mon Sep 17 00:00:00 2001 From: tobi-wan-kenobi Date: Sat, 25 Apr 2020 10:48:36 +0200 Subject: [PATCH] [modules/datetimetz] Update to latest API --- modules/contrib/datetimetz.py | 38 +++++++++++++++++++---------------- modules/contrib/datetz.py | 21 +++++++++++++++++++ modules/contrib/timetz.py | 21 +++++++++++++++++++ 3 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 modules/contrib/datetz.py create mode 100644 modules/contrib/timetz.py diff --git a/modules/contrib/datetimetz.py b/modules/contrib/datetimetz.py index be7f569..08c6631 100644 --- a/modules/contrib/datetimetz.py +++ b/modules/contrib/datetimetz.py @@ -18,14 +18,14 @@ from __future__ import absolute_import import datetime import locale import logging -try: - import pytz - import tzlocal -except: - pass -import bumblebee.input -import bumblebee.output -import bumblebee.engine +import pytz +import tzlocal + +import core.module +import core.widget +import core.input + +import util.format def default_format(module): default = '%x %X %Z' @@ -35,20 +35,21 @@ def default_format(module): default = '%X %Z' return default -class Module(bumblebee.engine.Module): - def __init__(self, engine, config): - super(Module, self).__init__(engine, config, - bumblebee.output.Widget(full_text=self.get_time)) - engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd=self.next_tz) - engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, cmd=self.prev_tz) - self._fmt = self.parameter('format', default_format(self.name)) +class Module(core.module.Module): + def __init__(self, config): + super().__init__(config, core.widget.Widget(self.get_time)) + + core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.next_tz) + core.input.register(self, button=core.input.RIGHT_MOUSE, cmd=self.prev_tz) + self.__fmt = self.parameter('format', self.default_format()) + default_timezone = '' try: default_timezone = tzlocal.get_localzone().zone except Exception as e: logging.error('unable to get default timezone: {}'.format(str(e))) try: - self._timezones = self.parameter('timezone', default_timezone).split(',') + self._timezones = util.format.aslist(self.parameter('timezone', default_timezone)) except: self._timezones = [default_timezone] self._current_tz = 0 @@ -62,11 +63,14 @@ class Module(bumblebee.engine.Module): except Exception: locale.setlocale(locale.LC_TIME, ('en_US', 'UTF-8')) + def default_format(self): + return '%x %X %Z' + def get_time(self, widget): try: try: tz = pytz.timezone(self._timezones[self._current_tz].strip()) - retval = datetime.datetime.now(tz=tzlocal.get_localzone()).astimezone(tz).strftime(self._fmt) + retval = datetime.datetime.now(tz=tzlocal.get_localzone()).astimezone(tz).strftime(self.__fmt) except pytz.exceptions.UnknownTimeZoneError: retval = '[Unknown timezone: {}]'.format(self._timezones[self._current_tz].strip()) except Exception as e: diff --git a/modules/contrib/datetz.py b/modules/contrib/datetz.py new file mode 100644 index 0000000..48541dc --- /dev/null +++ b/modules/contrib/datetz.py @@ -0,0 +1,21 @@ +# pylint: disable=C0111,R0903 + +"""Displays the current date and time. + +Parameters: + * date.format: strftime()-compatible formatting string + * date.locale: locale to use rather than the system default +""" + +import core.decorators +from .datetimetz import Module + +class Module(Module): + @core.decorators.every(hours=1) + def __init__(self, config): + super().__init__(config) + + def default_format(self): + return '%x %Z' + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/modules/contrib/timetz.py b/modules/contrib/timetz.py new file mode 100644 index 0000000..aeaa6f6 --- /dev/null +++ b/modules/contrib/timetz.py @@ -0,0 +1,21 @@ +# pylint: disable=C0111,R0903 + +"""Displays the current date and time. + +Parameters: + * time.format: strftime()-compatible formatting string + * time.locale: locale to use rather than the system default +""" + +import core.decorators +from .datetimetz import Module + +class Module(Module): + @core.decorators.every(seconds=59) # ensures one update per minute + def __init__(self, config): + super().__init__(config) + + def default_format(self): + return '%X %Z' + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4