2018-04-17 12:18:40 +00:00
|
|
|
"""OpenAPI core schemas util module"""
|
2019-03-07 21:55:27 +00:00
|
|
|
from base64 import b64decode
|
2018-08-17 04:25:12 +00:00
|
|
|
import datetime
|
2018-04-17 12:18:40 +00:00
|
|
|
from distutils.util import strtobool
|
2018-07-28 20:57:49 +00:00
|
|
|
from json import dumps
|
2019-03-07 21:55:27 +00:00
|
|
|
from six import string_types, text_type
|
2018-09-12 14:09:10 +00:00
|
|
|
import strict_rfc3339
|
2019-02-28 21:25:22 +00:00
|
|
|
from uuid import UUID
|
2018-04-17 12:18:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
def forcebool(val):
|
2018-07-15 21:21:15 +00:00
|
|
|
if isinstance(val, string_types):
|
2018-04-17 12:18:40 +00:00
|
|
|
val = strtobool(val)
|
|
|
|
|
|
|
|
return bool(val)
|
2018-07-28 20:57:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def dicthash(d):
|
|
|
|
return hash(dumps(d, sort_keys=True))
|
2018-08-17 04:25:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_date(value):
|
|
|
|
return datetime.datetime.strptime(value, '%Y-%m-%d').date()
|
2018-08-22 10:51:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_datetime(value):
|
2018-09-12 14:09:10 +00:00
|
|
|
timestamp = strict_rfc3339.rfc3339_to_timestamp(value)
|
2019-01-28 13:36:48 +00:00
|
|
|
return datetime.datetime.utcfromtimestamp(timestamp)
|
2019-02-28 21:25:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_uuid(value):
|
|
|
|
if isinstance(value, UUID):
|
|
|
|
return value
|
|
|
|
return UUID(value)
|
2019-03-07 21:55:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def format_byte(value, encoding='utf8'):
|
|
|
|
return text_type(b64decode(value), encoding)
|