platypush/platypush/schemas/__init__.py

24 lines
593 B
Python
Raw Normal View History

2021-07-22 01:02:15 +02:00
from datetime import datetime
from typing import Optional
2021-07-28 01:09:09 +02:00
from marshmallow import fields
2021-09-17 00:47:33 +02:00
class StrippedString(fields.Function): # lgtm [py/missing-call-to-init]
2021-07-28 01:09:09 +02:00
def __init__(self, *args, **kwargs):
kwargs['serialize'] = self._strip
kwargs['deserialize'] = self._strip
super().__init__(*args, **kwargs)
@staticmethod
def _strip(value: str):
return value.strip()
2021-07-22 01:02:15 +02:00
def normalize_datetime(dt: str) -> Optional[datetime]:
if not dt:
return
if dt.endswith('Z'):
dt = dt[:-1] + '+00:00'
return datetime.fromisoformat(dt)