gallery.accords-library.com/server/szurubooru/util.py

62 lines
2.1 KiB
Python
Raw Normal View History

2016-04-02 12:13:26 +00:00
''' Exports miscellaneous functions and data structures. '''
import datetime
import re
from szurubooru.errors import ValidationError
class dotdict(dict): # pylint: disable=invalid-name
'''dot.notation access to dictionary attributes'''
def __getattr__(self, attr):
return self.get(attr)
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
2016-04-02 12:13:26 +00:00
def parse_time_range(value, timezone=datetime.timezone(datetime.timedelta())):
''' Returns tuple containing min/max time for given text representation. '''
one_day = datetime.timedelta(days=1)
one_second = datetime.timedelta(seconds=1)
value = value.lower()
if not value:
raise ValidationError('Empty date format.')
if value == 'today':
now = datetime.datetime.now(tz=timezone)
return (
datetime.datetime(now.year, now.month, now.day, 0, 0, 0),
datetime.datetime(now.year, now.month, now.day, 0, 0, 0)
+ one_day - one_second)
if value == 'yesterday':
now = datetime.datetime.now(tz=timezone)
return (
datetime.datetime(now.year, now.month, now.day, 0, 0, 0) - one_day,
datetime.datetime(now.year, now.month, now.day, 0, 0, 0)
- one_second)
match = re.match('^(\d{4})$', value)
if match:
year = int(match.group(1))
return (
datetime.datetime(year, 1, 1),
datetime.datetime(year + 1, 1, 1) - one_second)
match = re.match('^(\d{4})-(\d{1,2})$', value)
if match:
year = int(match.group(1))
month = int(match.group(2))
return (
datetime.datetime(year, month, 1),
datetime.datetime(year, month + 1, 1) - one_second)
match = re.match('^(\d{4})-(\d{1,2})-(\d{1,2})$', value)
if match:
year = int(match.group(1))
month = int(match.group(2))
day = int(match.group(3))
return (
datetime.datetime(year, month, day),
datetime.datetime(year, month, day + 1) - one_second)
raise ValidationError('Invalid date format: %r.' % value)