LOG = logging.getLogger(__name__)
-__version__ = '1.2.1'
+__version__ = '1.2.2'
__me__ = Path(__file__).resolve()
__module_dir__ = __me__.parent
# =============================================================================
-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)
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 = []
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))
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__":
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 <frank.brehm@pixelpark.com>\n"
"Language: de_DE\n"
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}"
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 <frank.brehm@pixelpark.com>\n"
"Language: en_US\n"
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 ""
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 <frank.brehm@pixelpark.com>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
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 ""
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)
+
# =============================================================================