Merge pull request #863 from Hame-daani/main

refactor contrib.persian_date module
This commit is contained in:
tobi-wan-kenobi 2022-03-10 09:51:47 +01:00 committed by GitHub
commit a8fabce14e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 26 deletions

View file

@ -10,36 +10,22 @@ Parameters:
* datetime.locale: locale to use. default: "fa_IR" * datetime.locale: locale to use. default: "fa_IR"
""" """
from __future__ import absolute_import
import jdatetime import jdatetime
import locale
import core.module import core.decorators
import core.widget from modules.core.datetime import Module as dtmodule
import core.input
class Module(core.module.Module): class Module(dtmodule):
@core.decorators.every(minutes=1)
def __init__(self, config, theme): def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.full_text)) super().__init__(config, theme, dtlibrary=jdatetime)
l = ("fa_IR", "UTF-8")
lcl = self.parameter("locale", ".".join(l))
try:
locale.setlocale(locale.LC_ALL, lcl.split("."))
except Exception as e:
locale.setlocale(locale.LC_ALL, ("fa_IR", "UTF-8"))
def default_format(self): def default_format(self):
return "%A %d %B" return "%A %d %B"
def full_text(self, widget): def default_locale(self):
enc = locale.getpreferredencoding() return ("fa_IR", "UTF-8")
fmt = self.parameter("format", self.default_format())
retval = jdatetime.datetime.now().strftime(fmt)
if hasattr(retval, "decode"):
return retval.decode(enc)
return retval
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -17,26 +17,33 @@ import core.input
class Module(core.module.Module): class Module(core.module.Module):
def __init__(self, config, theme): def __init__(self, config, theme, dtlibrary=None):
super().__init__(config, theme, core.widget.Widget(self.full_text)) super().__init__(config, theme, core.widget.Widget(self.full_text))
core.input.register(self, button=core.input.LEFT_MOUSE, cmd="calendar") core.input.register(self, button=core.input.LEFT_MOUSE, cmd="calendar")
l = locale.getdefaultlocale() self.dtlibrary = dtlibrary or datetime
def set_locale(self):
l = self.default_locale()
if not l or l == (None, None): if not l or l == (None, None):
l = ("en_US", "UTF-8") l = ("en_US", "UTF-8")
lcl = self.parameter("locale", ".".join(l)) lcl = self.parameter("locale", ".".join(l))
try: try:
locale.setlocale(locale.LC_TIME, lcl.split(".")) locale.setlocale(locale.LC_ALL, lcl.split("."))
except Exception as e: except Exception as e:
locale.setlocale(locale.LC_TIME, ("en_US", "UTF-8")) locale.setlocale(locale.LC_ALL, ("en_US", "UTF-8"))
def default_format(self): def default_format(self):
return "%x %X" return "%x %X"
def default_locale(self):
return locale.getdefaultlocale()
def full_text(self, widget): def full_text(self, widget):
self.set_locale()
enc = locale.getpreferredencoding() enc = locale.getpreferredencoding()
fmt = self.parameter("format", self.default_format()) fmt = self.parameter("format", self.default_format())
retval = datetime.datetime.now().strftime(fmt) retval = self.dtlibrary.datetime.now().strftime(fmt)
if hasattr(retval, "decode"): if hasattr(retval, "decode"):
return retval.decode(enc) return retval.decode(enc)
return retval return retval