mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
time_filter: improve hints in error message (#368)
This commit is contained in:
parent
6fca49f445
commit
7b2ad8a80c
1 changed files with 10 additions and 19 deletions
|
@ -23,29 +23,17 @@ UNIT_FACTORS = {
|
||||||
|
|
||||||
UNIT_TO_SHORT_FORM = {
|
UNIT_TO_SHORT_FORM = {
|
||||||
'millisecond': 'ms',
|
'millisecond': 'ms',
|
||||||
'milliseconds': 'ms',
|
|
||||||
'msec': 'ms',
|
'msec': 'ms',
|
||||||
'msecs': 'ms',
|
|
||||||
'msecond': 'ms',
|
'msecond': 'ms',
|
||||||
'mseconds': 'ms',
|
|
||||||
'sec': 's',
|
'sec': 's',
|
||||||
'secs': 's',
|
|
||||||
'second': 's',
|
'second': 's',
|
||||||
'seconds': 's',
|
|
||||||
'hour': 'h',
|
'hour': 'h',
|
||||||
'hours': 'h',
|
|
||||||
'min': 'm',
|
'min': 'm',
|
||||||
'mins': 'm',
|
|
||||||
'minute': 'm',
|
'minute': 'm',
|
||||||
'minutes': 'm',
|
|
||||||
'day': 'd',
|
'day': 'd',
|
||||||
'days': 'd',
|
|
||||||
'week': 'w',
|
'week': 'w',
|
||||||
'weeks': 'w',
|
|
||||||
'month': 'mo',
|
'month': 'mo',
|
||||||
'months': 'mo',
|
|
||||||
'year': 'y',
|
'year': 'y',
|
||||||
'years': 'y',
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -58,13 +46,16 @@ def multiply(factors):
|
||||||
|
|
||||||
def to_time_unit(human_time, unit='ms', **kwargs):
|
def to_time_unit(human_time, unit='ms', **kwargs):
|
||||||
''' Return a time unit from a human readable string '''
|
''' Return a time unit from a human readable string '''
|
||||||
unit = UNIT_TO_SHORT_FORM.get(unit, unit)
|
unit_to_short_form = UNIT_TO_SHORT_FORM
|
||||||
if unit not in UNIT_FACTORS:
|
|
||||||
available_units = sorted(list(UNIT_FACTORS.keys()) + list(UNIT_TO_SHORT_FORM.keys()))
|
|
||||||
raise AnsibleFilterError("to_time_unit() can not convert to the following unit: %s. "
|
|
||||||
"Available units: %s" % (unit, ', '.join(available_units)))
|
|
||||||
|
|
||||||
unit_factors = UNIT_FACTORS
|
unit_factors = UNIT_FACTORS
|
||||||
|
|
||||||
|
unit = unit_to_short_form.get(unit.rstrip('s'), unit)
|
||||||
|
if unit not in unit_factors:
|
||||||
|
raise AnsibleFilterError("to_time_unit() can not convert to the following unit: %s. "
|
||||||
|
"Available units (singular or plural): %s. "
|
||||||
|
"Available short units: %s"
|
||||||
|
% (unit, ', '.join(unit_to_short_form.keys()), ', '.join(unit_factors.keys())))
|
||||||
|
|
||||||
if 'year' in kwargs:
|
if 'year' in kwargs:
|
||||||
unit_factors['y'] = unit_factors['y'][:-1] + [kwargs.pop('year')]
|
unit_factors['y'] = unit_factors['y'][:-1] + [kwargs.pop('year')]
|
||||||
if 'month' in kwargs:
|
if 'month' in kwargs:
|
||||||
|
@ -83,7 +74,7 @@ def to_time_unit(human_time, unit='ms', **kwargs):
|
||||||
h_time_int = int(res.group(1))
|
h_time_int = int(res.group(1))
|
||||||
h_time_unit = res.group(2)
|
h_time_unit = res.group(2)
|
||||||
|
|
||||||
h_time_unit = UNIT_TO_SHORT_FORM.get(h_time_unit, h_time_unit)
|
h_time_unit = unit_to_short_form.get(h_time_unit.rstrip('s'), h_time_unit)
|
||||||
if h_time_unit not in unit_factors:
|
if h_time_unit not in unit_factors:
|
||||||
raise AnsibleFilterError(
|
raise AnsibleFilterError(
|
||||||
"to_time_unit() can not interpret following string: %s" % human_time)
|
"to_time_unit() can not interpret following string: %s" % human_time)
|
||||||
|
|
Loading…
Reference in a new issue