2020-03-05 22:51:21 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""Custom input type parser."""
|
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
import environs
|
|
|
|
from dateutil import tz
|
|
|
|
from pytimeparse import timeparse
|
|
|
|
|
|
|
|
env = environs.Env()
|
|
|
|
|
|
|
|
|
|
|
|
def timedelta_validator(value):
|
|
|
|
"""Return the :class:`datetime.datetime.DateTime` for a time in the past.
|
|
|
|
|
|
|
|
:param value: a string containing a time format supported by
|
|
|
|
mod:`pytimeparse`
|
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
try:
|
|
|
|
_datetime_seconds_ago(timeparse.timeparse(value))
|
|
|
|
return value
|
|
|
|
except TypeError:
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
2020-03-09 00:05:17 +00:00
|
|
|
def timedelta(value, dt_format=None):
|
2020-03-05 22:51:21 +00:00
|
|
|
"""Return the :class:`datetime.datetime.DateTime` for a time in the past.
|
|
|
|
|
|
|
|
:param value: a string containing a time format supported by
|
|
|
|
mod:`pytimeparse`
|
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
return None
|
2020-03-09 00:05:17 +00:00
|
|
|
|
|
|
|
timedelta = _datetime_seconds_ago(timeparse.timeparse(value))
|
|
|
|
if dt_format:
|
|
|
|
timedelta = timedelta.strftime(dt_format)
|
|
|
|
|
|
|
|
return timedelta
|
2020-03-05 22:51:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _datetime_seconds_ago(seconds):
|
|
|
|
now = datetime.datetime.now(tz.tzutc())
|
|
|
|
return now - datetime.timedelta(seconds=seconds)
|
|
|
|
|
|
|
|
|
|
|
|
@env.parser_for("timedelta_validator")
|
|
|
|
def timedelta_parser(value):
|
|
|
|
try:
|
|
|
|
timedelta_validator(value)
|
|
|
|
return value
|
|
|
|
except TypeError as e:
|
|
|
|
raise environs.EnvError(e)
|