2020-05-18 09:54:23 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright (c) 2020, René Moser <mail@renemoser.net>
|
2022-08-05 13:17:19 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-05-18 09:54:23 +02:00
|
|
|
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
import re
|
|
|
|
from ansible.errors import AnsibleFilterError
|
|
|
|
|
|
|
|
|
|
|
|
UNIT_FACTORS = {
|
|
|
|
'ms': [],
|
|
|
|
's': [1000],
|
|
|
|
'm': [1000, 60],
|
|
|
|
'h': [1000, 60, 60],
|
|
|
|
'd': [1000, 60, 60, 24],
|
|
|
|
'w': [1000, 60, 60, 24, 7],
|
|
|
|
'mo': [1000, 60, 60, 24, 30],
|
|
|
|
'y': [1000, 60, 60, 24, 365],
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UNIT_TO_SHORT_FORM = {
|
|
|
|
'millisecond': 'ms',
|
|
|
|
'msec': 'ms',
|
|
|
|
'msecond': 'ms',
|
|
|
|
'sec': 's',
|
|
|
|
'second': 's',
|
|
|
|
'hour': 'h',
|
|
|
|
'min': 'm',
|
|
|
|
'minute': 'm',
|
|
|
|
'day': 'd',
|
|
|
|
'week': 'w',
|
|
|
|
'month': 'mo',
|
|
|
|
'year': 'y',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def multiply(factors):
|
|
|
|
result = 1
|
|
|
|
for factor in factors:
|
|
|
|
result = result * factor
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2020-05-19 11:18:49 +02:00
|
|
|
def to_time_unit(human_time, unit='ms', **kwargs):
|
2020-05-18 09:54:23 +02:00
|
|
|
''' Return a time unit from a human readable string '''
|
2022-05-04 07:38:16 +02:00
|
|
|
|
|
|
|
# No need to handle 0
|
|
|
|
if human_time == "0":
|
|
|
|
return 0
|
|
|
|
|
2020-05-20 09:22:56 +02:00
|
|
|
unit_to_short_form = UNIT_TO_SHORT_FORM
|
|
|
|
unit_factors = UNIT_FACTORS
|
|
|
|
|
|
|
|
unit = unit_to_short_form.get(unit.rstrip('s'), unit)
|
|
|
|
if unit not in unit_factors:
|
2020-05-18 09:54:23 +02:00
|
|
|
raise AnsibleFilterError("to_time_unit() can not convert to the following unit: %s. "
|
2020-05-20 09:22:56 +02:00
|
|
|
"Available units (singular or plural): %s. "
|
|
|
|
"Available short units: %s"
|
|
|
|
% (unit, ', '.join(unit_to_short_form.keys()), ', '.join(unit_factors.keys())))
|
2020-05-19 11:18:49 +02:00
|
|
|
|
|
|
|
if 'year' in kwargs:
|
|
|
|
unit_factors['y'] = unit_factors['y'][:-1] + [kwargs.pop('year')]
|
|
|
|
if 'month' in kwargs:
|
|
|
|
unit_factors['mo'] = unit_factors['mo'][:-1] + [kwargs.pop('month')]
|
|
|
|
|
|
|
|
if kwargs:
|
|
|
|
raise AnsibleFilterError('to_time_unit() got unknown keyword arguments: %s' % ', '.join(kwargs.keys()))
|
|
|
|
|
2020-05-18 09:54:23 +02:00
|
|
|
result = 0
|
|
|
|
for h_time_string in human_time.split():
|
|
|
|
res = re.match(r'(-?\d+)(\w+)', h_time_string)
|
|
|
|
if not res:
|
|
|
|
raise AnsibleFilterError(
|
|
|
|
"to_time_unit() can not interpret following string: %s" % human_time)
|
|
|
|
|
|
|
|
h_time_int = int(res.group(1))
|
|
|
|
h_time_unit = res.group(2)
|
|
|
|
|
2020-05-20 09:22:56 +02:00
|
|
|
h_time_unit = unit_to_short_form.get(h_time_unit.rstrip('s'), h_time_unit)
|
2020-05-19 11:18:49 +02:00
|
|
|
if h_time_unit not in unit_factors:
|
2020-05-18 09:54:23 +02:00
|
|
|
raise AnsibleFilterError(
|
|
|
|
"to_time_unit() can not interpret following string: %s" % human_time)
|
|
|
|
|
2020-05-19 11:18:49 +02:00
|
|
|
time_in_milliseconds = h_time_int * multiply(unit_factors[h_time_unit])
|
2020-05-18 09:54:23 +02:00
|
|
|
result += time_in_milliseconds
|
2020-05-19 11:18:49 +02:00
|
|
|
return round(result / multiply(unit_factors[unit]), 12)
|
2020-05-18 09:54:23 +02:00
|
|
|
|
|
|
|
|
2020-05-19 11:18:49 +02:00
|
|
|
def to_milliseconds(human_time, **kwargs):
|
2020-05-18 09:54:23 +02:00
|
|
|
''' Return milli seconds from a human readable string '''
|
2020-05-19 11:18:49 +02:00
|
|
|
return to_time_unit(human_time, 'ms', **kwargs)
|
2020-05-18 09:54:23 +02:00
|
|
|
|
|
|
|
|
2020-05-19 11:18:49 +02:00
|
|
|
def to_seconds(human_time, **kwargs):
|
2020-05-18 09:54:23 +02:00
|
|
|
''' Return seconds from a human readable string '''
|
2020-05-19 11:18:49 +02:00
|
|
|
return to_time_unit(human_time, 's', **kwargs)
|
2020-05-18 09:54:23 +02:00
|
|
|
|
|
|
|
|
2020-05-19 11:18:49 +02:00
|
|
|
def to_minutes(human_time, **kwargs):
|
2020-05-18 09:54:23 +02:00
|
|
|
''' Return minutes from a human readable string '''
|
2020-05-19 11:18:49 +02:00
|
|
|
return to_time_unit(human_time, 'm', **kwargs)
|
2020-05-18 09:54:23 +02:00
|
|
|
|
|
|
|
|
2020-05-19 11:18:49 +02:00
|
|
|
def to_hours(human_time, **kwargs):
|
2020-05-18 09:54:23 +02:00
|
|
|
''' Return hours from a human readable string '''
|
2020-05-19 11:18:49 +02:00
|
|
|
return to_time_unit(human_time, 'h', **kwargs)
|
2020-05-18 09:54:23 +02:00
|
|
|
|
|
|
|
|
2020-05-19 11:18:49 +02:00
|
|
|
def to_days(human_time, **kwargs):
|
2020-05-18 09:54:23 +02:00
|
|
|
''' Return days from a human readable string '''
|
2020-05-19 11:18:49 +02:00
|
|
|
return to_time_unit(human_time, 'd', **kwargs)
|
2020-05-18 09:54:23 +02:00
|
|
|
|
|
|
|
|
2020-05-19 11:18:49 +02:00
|
|
|
def to_weeks(human_time, **kwargs):
|
2020-05-18 09:54:23 +02:00
|
|
|
''' Return weeks from a human readable string '''
|
2020-05-19 11:18:49 +02:00
|
|
|
return to_time_unit(human_time, 'w', **kwargs)
|
2020-05-18 09:54:23 +02:00
|
|
|
|
|
|
|
|
2020-05-19 11:18:49 +02:00
|
|
|
def to_months(human_time, **kwargs):
|
2020-05-18 09:54:23 +02:00
|
|
|
''' Return months from a human readable string '''
|
2020-05-19 11:18:49 +02:00
|
|
|
return to_time_unit(human_time, 'mo', **kwargs)
|
2020-05-18 09:54:23 +02:00
|
|
|
|
|
|
|
|
2020-05-19 11:18:49 +02:00
|
|
|
def to_years(human_time, **kwargs):
|
2020-05-18 09:54:23 +02:00
|
|
|
''' Return years from a human readable string '''
|
2020-05-19 11:18:49 +02:00
|
|
|
return to_time_unit(human_time, 'y', **kwargs)
|
2020-05-18 09:54:23 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FilterModule(object):
|
|
|
|
''' Ansible time jinja2 filters '''
|
|
|
|
|
|
|
|
def filters(self):
|
|
|
|
filters = {
|
|
|
|
'to_time_unit': to_time_unit,
|
|
|
|
'to_milliseconds': to_milliseconds,
|
|
|
|
'to_seconds': to_seconds,
|
|
|
|
'to_minutes': to_minutes,
|
|
|
|
'to_hours': to_hours,
|
|
|
|
'to_days': to_days,
|
|
|
|
'to_weeks': to_weeks,
|
|
|
|
'to_months': to_months,
|
|
|
|
'to_years': to_years,
|
|
|
|
}
|
|
|
|
|
|
|
|
return filters
|