From 65fadd56f5b519e8da37be5b770f7f53e55e46de Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 9 Oct 2019 10:46:12 +0200 Subject: [PATCH] Increasing function format_timedelta_list in lib/webhooks/xlate.py --- lib/webhooks/xlate.py | 25 ++++++++++++++--------- test/test_xlate.py | 47 ++++++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/lib/webhooks/xlate.py b/lib/webhooks/xlate.py index eb1376a..3353da2 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.0' +__version__ = '1.2.1' __me__ = Path(__file__).resolve() __module_dir__ = __me__.parent @@ -99,23 +99,30 @@ def format_timedelta_list(delta, granularity='second', locale=LC_TIME): seconds = int((delta.days * 86400) + delta.seconds) else: seconds = delta - locale = Locale.parse(locale) + loc_obj = Locale.parse(locale) part_list = [] +# LOG.debug("DOMAIN: {D!r}, LOCALE_DIR: {dir!r}, locale: {l!r}".format( +# D=DOMAIN, dir=str(LOCALE_DIR), l=[locale])) + xlator = gettext.translation( + DOMAIN, localedir=str(LOCALE_DIR), languages=[locale], fallback=True) +# LOG.debug("xlator: {c}: {d}".format( +# c=xlator.__class__.__name__, d=pp(xlator))) + def local_val(value, unit): if unit == 'second': - unit_loc = ngettext('second', 'seconds', value) + unit_loc = xlator.ngettext('second', 'seconds', value) elif unit == 'minute': - unit_loc = ngettext('minute', 'minutes', value) + unit_loc = xlator.ngettext('minute', 'minutes', value) elif unit == 'hour': - unit_loc = ngettext('hour', 'hours', value) + unit_loc = xlator.ngettext('hour', 'hours', value) elif unit == 'day': - unit_loc = ngettext('day', 'days', value) + unit_loc = xlator.ngettext('day', 'days', value) elif unit == 'month': - unit_loc = ngettext('month', 'months', value) + unit_loc = xlator.ngettext('month', 'months', value) elif unit == 'year': - unit_loc = ngettext('year', 'years', value) + unit_loc = xlator.ngettext('year', 'years', value) else: msg = _("Wrong value {v!r} for parameter {p!r} of function {f}().").format( v=unit, p='unit', f='local_val') @@ -165,7 +172,7 @@ def format_timedelta_list(delta, granularity='second', locale=LC_TIME): if not part_list: return '' part_list.reverse() - return format_list(part_list) + return format_list(part_list, locale=locale) # ============================================================================= diff --git a/test/test_xlate.py b/test/test_xlate.py index 2dc931a..b3c4fd2 100755 --- a/test/test_xlate.py +++ b/test/test_xlate.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ @author: Frank Brehm @@ -54,36 +54,41 @@ class TestXlate(WebHooksTestcase): from webhooks.xlate import format_timedelta_list test_data = ( - (1, 'second', '1 Sekunde'), - (2, 'second', '2 Sekunden'), - (65, 'second', '1 Minute und 5 Sekunden'), - (3604, 'second', '1 Stunde und 4 Sekunden'), - (3674, 'second', '1 Stunde, 1 Minute und 14 Sekunden'), - ((3600 * 7) + 4, 'second', '7 Stunden und 4 Sekunden'), - (3604 + (24 * 3600), 'second', '1 Tag, 1 Stunde und 4 Sekunden'), - (3604 + (2 * 24 * 3600), 'second', '2 Tage, 1 Stunde und 4 Sekunden'), - (3604 + (2 * 24 * 3600), 'day', '2 Tage'), - ((3600 * 12) - 1 + (2 * 24 * 3600), 'day', '2 Tage'), - ((3600 * 12) + (2 * 24 * 3600), 'day', '3 Tage'), - ((3600 * 14) + 4 + (2 * 24 * 3600), 'day', '3 Tage'), - ((3600 * 24 * 30) + (2 * 24 * 3600), 'day', '1 Monat und 2 Tage'), - ((4 * 3600 * 24 * 30) + (5 * 24 * 3600), 'day', '4 Monate und 5 Tage'), - ((3600 * 24 * 365), 'day', '1 Jahr'), + (1, 'second', '1 Sekunde', 'de_DE.utf8'), + (1, 'second', '1 second', 'en_US.utf8'), + (2, 'second', '2 Sekunden', 'de_DE.utf8'), + (2, 'second', '2 seconds', 'en_US.utf8'), + (65, 'second', '1 Minute und 5 Sekunden', 'de_DE.utf8'), + (65, 'second', '1 minute and 5 seconds', 'en_US.utf8'), + (3604, 'second', '1 Stunde und 4 Sekunden', 'de_DE.utf8'), + (3674, 'second', '1 Stunde, 1 Minute und 14 Sekunden', 'de_DE.utf8'), + (3674, 'second', '1 hour, 1 minute, and 14 seconds', 'en_US.utf8'), + ((3600 * 7) + 4, 'second', '7 Stunden und 4 Sekunden', 'de_DE.utf8'), + (3604 + (24 * 3600), 'second', '1 Tag, 1 Stunde und 4 Sekunden', 'de_DE.utf8'), + (3604 + (2 * 24 * 3600), 'second', '2 Tage, 1 Stunde und 4 Sekunden', 'de_DE.utf8'), + (3604 + (2 * 24 * 3600), 'day', '2 Tage', 'de_DE.utf8'), + ((3600 * 12) - 1 + (2 * 24 * 3600), 'day', '2 Tage', 'de_DE.utf8'), + ((3600 * 12) + (2 * 24 * 3600), 'day', '3 Tage', 'de_DE.utf8'), + ((3600 * 14) + 4 + (2 * 24 * 3600), 'day', '3 Tage', 'de_DE.utf8'), + ((3600 * 24 * 30) + (2 * 24 * 3600), 'day', '1 Monat und 2 Tage', 'de_DE.utf8'), + ((4 * 3600 * 24 * 30) + (5 * 24 * 3600), 'day', '4 Monate und 5 Tage', 'de_DE.utf8'), + ((3600 * 24 * 365), 'day', '1 Jahr', 'de_DE.utf8'), ((3600 * 24 * 365) + (4 * 3600 * 24 * 30) + (5 * 24 * 3600), - 'day', '1 Jahr, 4 Monate und 5 Tage'), + 'day', '1 Jahr, 4 Monate und 5 Tage', 'de_DE.utf8'), ((6 * 3600 * 24 * 365) + (2 * 3600 * 24 * 30) + (5 * 24 * 3600), - 'day', '6 Jahre, 2 Monate und 5 Tage'), + 'day', '6 Jahre, 2 Monate und 5 Tage', '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}.".format( - s=secs, g=gran, e=expected)) + 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, ) + result = format_timedelta_list(secs, granularity=gran, locale=locale) self.assertEqual(expected, result) -- 2.39.5