From 25b82ae44cf0f3262fd416b185867ebdb5670223 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 9 Oct 2019 11:28:09 +0200 Subject: [PATCH] Adding parameter add_direction to function format_timedelta_list() --- lib/webhooks/xlate.py | 22 +++++++++--- .../LC_MESSAGES/puppetmaster_webhooks.po | 34 ++++++++++++------- .../LC_MESSAGES/puppetmaster_webhooks.po | 34 ++++++++++++------- locale/puppetmaster_webhooks.pot | 34 ++++++++++++------- test/test_xlate.py | 20 +++++++++++ 5 files changed, 100 insertions(+), 44 deletions(-) diff --git a/lib/webhooks/xlate.py b/lib/webhooks/xlate.py index 3353da2..24951b6 100644 --- a/lib/webhooks/xlate.py +++ b/lib/webhooks/xlate.py @@ -32,7 +32,7 @@ DOMAIN = 'puppetmaster_webhooks' LOG = logging.getLogger(__name__) -__version__ = '1.2.1' +__version__ = '1.2.2' __me__ = Path(__file__).resolve() __module_dir__ = __me__.parent @@ -86,7 +86,7 @@ ngettext = XLATOR.ngettext # ============================================================================= -def format_timedelta_list(delta, granularity='second', locale=LC_TIME): +def format_timedelta_list(delta, granularity='second', add_direction=False, locale=LC_TIME): """Return a time delta according to the rules of the given locale.""" units = map(lambda x: x[0], TIMEDELTA_UNITS) @@ -99,6 +99,10 @@ def format_timedelta_list(delta, granularity='second', locale=LC_TIME): seconds = int((delta.days * 86400) + delta.seconds) else: seconds = delta + in_past = False + if seconds < 0: + in_past = True + seconds = abs(seconds) loc_obj = Locale.parse(locale) part_list = [] @@ -147,8 +151,8 @@ def format_timedelta_list(delta, granularity='second', locale=LC_TIME): if granularity == unit: include = True continue - value = int(abs(cur_value) / subunits) - rest = int(abs(cur_value) % subunits) + value = int(cur_value / subunits) + rest = int(cur_value % subunits) # LOG.debug("Value: {v!r}, rest: {r!r}.".format(v=value, r=rest)) last_unit = TIMEDELTA_UNITS[i-2][0] # LOG.debug("Last unit: {!r}".format(last_unit)) @@ -172,8 +176,16 @@ def format_timedelta_list(delta, granularity='second', locale=LC_TIME): if not part_list: return '' part_list.reverse() - return format_list(part_list, locale=locale) + ret = format_list(part_list, locale=locale) + if not add_direction: + return ret + + if in_past: + ret = xlator.gettext("{} ago").format(ret) + else: + ret = xlator.gettext("in {}").format(ret) + return ret # ============================================================================= if __name__ == "__main__": diff --git a/locale/de_DE/LC_MESSAGES/puppetmaster_webhooks.po b/locale/de_DE/LC_MESSAGES/puppetmaster_webhooks.po index 323c990..b617980 100644 --- a/locale/de_DE/LC_MESSAGES/puppetmaster_webhooks.po +++ b/locale/de_DE/LC_MESSAGES/puppetmaster_webhooks.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: puppetmaster_webhooks 1.5.4\n" "Report-Msgid-Bugs-To: frank.brehm@pixelpark.com\n" -"POT-Creation-Date: 2019-10-09 11:06+0200\n" +"POT-Creation-Date: 2019-10-09 11:26+0200\n" "PO-Revision-Date: 2019-10-07 18:40+0100\n" "Last-Translator: Frank Brehm \n" "Language: de_DE\n" @@ -992,63 +992,71 @@ msgid_plural "Found total {sb}{nr} modules{eb}." msgstr[0] "Insgesamt {sb}ein Modul{eb} gefunden." msgstr[1] "Insgesamt {sb}{nr} Module{eb} gefunden." -#: lib/webhooks/xlate.py:94 lib/webhooks/xlate.py:127 +#: lib/webhooks/xlate.py:94 lib/webhooks/xlate.py:131 msgid "Wrong value {v!r} for parameter {p!r} of function {f}()." msgstr "Falscher Wert {v!r} für Parameter {p!r} der Funktion {f}()." -#: lib/webhooks/xlate.py:115 +#: lib/webhooks/xlate.py:119 msgid "second" msgid_plural "seconds" msgstr[0] "Sekunde" msgstr[1] "Sekunden" -#: lib/webhooks/xlate.py:117 +#: lib/webhooks/xlate.py:121 msgid "minute" msgid_plural "minutes" msgstr[0] "Minute" msgstr[1] "Minuten" -#: lib/webhooks/xlate.py:119 +#: lib/webhooks/xlate.py:123 msgid "hour" msgid_plural "hours" msgstr[0] "Stunde" msgstr[1] "Stunden" -#: lib/webhooks/xlate.py:121 +#: lib/webhooks/xlate.py:125 msgid "day" msgid_plural "days" msgstr[0] "Tag" msgstr[1] "Tage" -#: lib/webhooks/xlate.py:123 +#: lib/webhooks/xlate.py:127 msgid "month" msgid_plural "months" msgstr[0] "Monat" msgstr[1] "Monate" -#: lib/webhooks/xlate.py:125 +#: lib/webhooks/xlate.py:129 msgid "year" msgid_plural "years" msgstr[0] "Jahr" msgstr[1] "Jahre" -#: lib/webhooks/xlate.py:181 +#: lib/webhooks/xlate.py:185 +msgid "{} ago" +msgstr "vor {}" + +#: lib/webhooks/xlate.py:187 +msgid "in {}" +msgstr "in {}" + +#: lib/webhooks/xlate.py:193 msgid "Module directory: {!r}" msgstr "Modul-Verzeichnis: {!r}" -#: lib/webhooks/xlate.py:182 +#: lib/webhooks/xlate.py:194 msgid "Base directory: {!r}" msgstr "Basis-Verzeichnis: {!r}" -#: lib/webhooks/xlate.py:183 +#: lib/webhooks/xlate.py:195 msgid "Locale directory: {!r}" msgstr "Verzeichnis für Locales: {!r}" -#: lib/webhooks/xlate.py:184 +#: lib/webhooks/xlate.py:196 msgid "Locale domain: {!r}" msgstr "Locale-Domäne: {!r}" -#: lib/webhooks/xlate.py:185 +#: lib/webhooks/xlate.py:197 msgid "Found .mo-file: {!r}" msgstr "Gefundene .mo-Datei: {!r}" diff --git a/locale/en_US/LC_MESSAGES/puppetmaster_webhooks.po b/locale/en_US/LC_MESSAGES/puppetmaster_webhooks.po index a309fc4..58b6e61 100644 --- a/locale/en_US/LC_MESSAGES/puppetmaster_webhooks.po +++ b/locale/en_US/LC_MESSAGES/puppetmaster_webhooks.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: puppetmaster_webhooks 1.3.1\n" "Report-Msgid-Bugs-To: frank.brehm@pixelpark.com\n" -"POT-Creation-Date: 2019-10-09 11:06+0200\n" +"POT-Creation-Date: 2019-10-09 11:26+0200\n" "PO-Revision-Date: 2019-10-09 11:20+0100\n" "Last-Translator: Frank Brehm \n" "Language: en_US\n" @@ -959,63 +959,71 @@ msgid_plural "Found total {sb}{nr} modules{eb}." msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:94 lib/webhooks/xlate.py:127 +#: lib/webhooks/xlate.py:94 lib/webhooks/xlate.py:131 msgid "Wrong value {v!r} for parameter {p!r} of function {f}()." msgstr "" -#: lib/webhooks/xlate.py:115 +#: lib/webhooks/xlate.py:119 msgid "second" msgid_plural "seconds" msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:117 +#: lib/webhooks/xlate.py:121 msgid "minute" msgid_plural "minutes" msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:119 +#: lib/webhooks/xlate.py:123 msgid "hour" msgid_plural "hours" msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:121 +#: lib/webhooks/xlate.py:125 msgid "day" msgid_plural "days" msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:123 +#: lib/webhooks/xlate.py:127 msgid "month" msgid_plural "months" msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:125 +#: lib/webhooks/xlate.py:129 msgid "year" msgid_plural "years" msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:181 +#: lib/webhooks/xlate.py:185 +msgid "{} ago" +msgstr "" + +#: lib/webhooks/xlate.py:187 +msgid "in {}" +msgstr "" + +#: lib/webhooks/xlate.py:193 msgid "Module directory: {!r}" msgstr "" -#: lib/webhooks/xlate.py:182 +#: lib/webhooks/xlate.py:194 msgid "Base directory: {!r}" msgstr "" -#: lib/webhooks/xlate.py:183 +#: lib/webhooks/xlate.py:195 msgid "Locale directory: {!r}" msgstr "" -#: lib/webhooks/xlate.py:184 +#: lib/webhooks/xlate.py:196 msgid "Locale domain: {!r}" msgstr "" -#: lib/webhooks/xlate.py:185 +#: lib/webhooks/xlate.py:197 msgid "Found .mo-file: {!r}" msgstr "" diff --git a/locale/puppetmaster_webhooks.pot b/locale/puppetmaster_webhooks.pot index 53e857b..bec6a36 100644 --- a/locale/puppetmaster_webhooks.pot +++ b/locale/puppetmaster_webhooks.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: puppetmaster_webhooks 1.7.3\n" "Report-Msgid-Bugs-To: frank.brehm@pixelpark.com\n" -"POT-Creation-Date: 2019-10-09 11:06+0200\n" +"POT-Creation-Date: 2019-10-09 11:26+0200\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -958,63 +958,71 @@ msgid_plural "Found total {sb}{nr} modules{eb}." msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:94 lib/webhooks/xlate.py:127 +#: lib/webhooks/xlate.py:94 lib/webhooks/xlate.py:131 msgid "Wrong value {v!r} for parameter {p!r} of function {f}()." msgstr "" -#: lib/webhooks/xlate.py:115 +#: lib/webhooks/xlate.py:119 msgid "second" msgid_plural "seconds" msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:117 +#: lib/webhooks/xlate.py:121 msgid "minute" msgid_plural "minutes" msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:119 +#: lib/webhooks/xlate.py:123 msgid "hour" msgid_plural "hours" msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:121 +#: lib/webhooks/xlate.py:125 msgid "day" msgid_plural "days" msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:123 +#: lib/webhooks/xlate.py:127 msgid "month" msgid_plural "months" msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:125 +#: lib/webhooks/xlate.py:129 msgid "year" msgid_plural "years" msgstr[0] "" msgstr[1] "" -#: lib/webhooks/xlate.py:181 +#: lib/webhooks/xlate.py:185 +msgid "{} ago" +msgstr "" + +#: lib/webhooks/xlate.py:187 +msgid "in {}" +msgstr "" + +#: lib/webhooks/xlate.py:193 msgid "Module directory: {!r}" msgstr "" -#: lib/webhooks/xlate.py:182 +#: lib/webhooks/xlate.py:194 msgid "Base directory: {!r}" msgstr "" -#: lib/webhooks/xlate.py:183 +#: lib/webhooks/xlate.py:195 msgid "Locale directory: {!r}" msgstr "" -#: lib/webhooks/xlate.py:184 +#: lib/webhooks/xlate.py:196 msgid "Locale domain: {!r}" msgstr "" -#: lib/webhooks/xlate.py:185 +#: lib/webhooks/xlate.py:197 msgid "Found .mo-file: {!r}" msgstr "" diff --git a/test/test_xlate.py b/test/test_xlate.py index b3c4fd2..becc65c 100755 --- a/test/test_xlate.py +++ b/test/test_xlate.py @@ -91,6 +91,26 @@ class TestXlate(WebHooksTestcase): result = format_timedelta_list(secs, granularity=gran, locale=locale) self.assertEqual(expected, result) + test_data = ( + (3600, 'second', 'in 1 hour', 'en_US.utf8'), + (3600, 'second', 'in 1 Stunde', 'de_DE.utf8'), + (-3600, 'second', '1 hour ago', 'en_US.utf8'), + (-3600, 'second', 'vor 1 Stunde', 'de_DE.utf8'), + ) + + for pair in test_data: + secs = pair[0] + gran = pair[1] + expected = pair[2] + locale = pair[3] + + LOG.debug("Testing {s} secs, granularity {g!r} => {e!r}, locale {l!r}.".format( + s=secs, g=gran, e=expected, l=locale)) + + result = format_timedelta_list( + secs, granularity=gran, add_direction=True, locale=locale) + self.assertEqual(expected, result) + # ============================================================================= -- 2.39.5