misc: use a single word for each time unit (#88822)
gitea/wcs/pipeline/head This commit looks good Details

This commit is contained in:
Frédéric Péters 2024-03-28 14:13:50 +01:00
parent dc473b7378
commit c8ffe45311
2 changed files with 18 additions and 16 deletions

View File

@ -20,7 +20,7 @@ from wcs.fields import StringField
from wcs.qommon import evalutils, force_str
from wcs.qommon.form import FileSizeWidget
from wcs.qommon.http_request import HTTPRequest
from wcs.qommon.humantime import humanduration2seconds, seconds2humanduration
from wcs.qommon.humantime import humanduration2seconds, seconds2humanduration, timewords
from wcs.qommon.misc import (
_http_request,
date_format,
@ -108,6 +108,10 @@ def test_humantime_short(seconds, expected):
assert seconds2humanduration(seconds, short=True) == expected
def test_humantime_timewords():
assert timewords() == ['day(s)', 'hour(s)', 'minute(s)', 'second(s)', 'month(s)', 'year(s)']
def test_parse_mimetypes():
assert FileTypesDirectory.parse_mimetypes('application/pdf') == ['application/pdf']
assert FileTypesDirectory.parse_mimetypes('.pdf') == ['application/pdf']

View File

@ -34,21 +34,20 @@ def list2human(stringlist):
_humandurations = (
((_('day'), _('days')), _day),
((_('hour'), _('hours')), _hour),
((_('month'), _('months')), _month),
((_('year'), _('years')), _year),
((_('minute'), _('minutes')), _minute),
((_('second'), _('seconds')), 1),
((_('day'), _('days'), _('day(s)')), _day),
((_('hour'), _('hours'), _('hour(s)')), _hour),
((_('minute'), _('minutes'), _('minute(s)')), _minute),
((_('second'), _('seconds'), _('second(s)')), 1),
((_('month'), _('months'), _('month(s)')), _month),
((_('year'), _('years'), _('year(s)')), _year),
)
def timewords():
'''List of words one can use to specify durations'''
result = []
for words, dummy in _humandurations:
for word in words:
result.append(str(word)) # str() to force translation
for (dummy, dummy, word), dummy in _humandurations:
result.append(str(word)) # str() to force translation
return result
@ -56,12 +55,11 @@ def humanduration2seconds(humanduration):
if not humanduration:
raise ValueError()
seconds = 0
for words, quantity in _humandurations:
for word in words:
m = re.search(r'(\d+)\s*\b%s\b' % word, humanduration)
if m:
seconds = seconds + int(m.group(1)) * quantity
break
for (word1, word2, dummy), quantity in _humandurations:
# look for number then singular or plural forms of unit
m = re.search(r'(\d+)\s*\b(%s|%s)\b' % (word1, word2), humanduration)
if m:
seconds = seconds + int(m.group(1)) * quantity
return seconds