[modules/datetimetz] Update to latest API

This commit is contained in:
tobi-wan-kenobi 2020-04-25 10:48:36 +02:00
parent 80fed80847
commit 2f86ef3b45
3 changed files with 63 additions and 17 deletions

View file

@ -18,14 +18,14 @@ from __future__ import absolute_import
import datetime import datetime
import locale import locale
import logging import logging
try:
import pytz import pytz
import tzlocal import tzlocal
except:
pass import core.module
import bumblebee.input import core.widget
import bumblebee.output import core.input
import bumblebee.engine
import util.format
def default_format(module): def default_format(module):
default = '%x %X %Z' default = '%x %X %Z'
@ -35,20 +35,21 @@ def default_format(module):
default = '%X %Z' default = '%X %Z'
return default return default
class Module(bumblebee.engine.Module): class Module(core.module.Module):
def __init__(self, engine, config): def __init__(self, config):
super(Module, self).__init__(engine, config, super().__init__(config, core.widget.Widget(self.get_time))
bumblebee.output.Widget(full_text=self.get_time))
engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd=self.next_tz) core.input.register(self, button=core.input.LEFT_MOUSE, cmd=self.next_tz)
engine.input.register_callback(self, button=bumblebee.input.RIGHT_MOUSE, cmd=self.prev_tz) core.input.register(self, button=core.input.RIGHT_MOUSE, cmd=self.prev_tz)
self._fmt = self.parameter('format', default_format(self.name)) self.__fmt = self.parameter('format', self.default_format())
default_timezone = '' default_timezone = ''
try: try:
default_timezone = tzlocal.get_localzone().zone default_timezone = tzlocal.get_localzone().zone
except Exception as e: except Exception as e:
logging.error('unable to get default timezone: {}'.format(str(e))) logging.error('unable to get default timezone: {}'.format(str(e)))
try: try:
self._timezones = self.parameter('timezone', default_timezone).split(',') self._timezones = util.format.aslist(self.parameter('timezone', default_timezone))
except: except:
self._timezones = [default_timezone] self._timezones = [default_timezone]
self._current_tz = 0 self._current_tz = 0
@ -62,11 +63,14 @@ class Module(bumblebee.engine.Module):
except Exception: except Exception:
locale.setlocale(locale.LC_TIME, ('en_US', 'UTF-8')) locale.setlocale(locale.LC_TIME, ('en_US', 'UTF-8'))
def default_format(self):
return '%x %X %Z'
def get_time(self, widget): def get_time(self, widget):
try: try:
try: try:
tz = pytz.timezone(self._timezones[self._current_tz].strip()) 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: except pytz.exceptions.UnknownTimeZoneError:
retval = '[Unknown timezone: {}]'.format(self._timezones[self._current_tz].strip()) retval = '[Unknown timezone: {}]'.format(self._timezones[self._current_tz].strip())
except Exception as e: except Exception as e:

21
modules/contrib/datetz.py Normal file
View file

@ -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

21
modules/contrib/timetz.py Normal file
View file

@ -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