blob: 467f901b5131a48ddd97433f224e90a12fabb981 [file] [log] [blame]
koder aka kdanilovf2865172016-12-30 03:35:11 +02001import abc
2from typing import Any, Union, List, Dict
3
4
5class IStorable(metaclass=abc.ABCMeta):
6 """Interface for type, which can be stored"""
7
8 @abc.abstractmethod
9 def raw(self) -> Dict[str, Any]:
10 pass
11
12 @abc.abstractclassmethod
13 def fromraw(cls, data: Dict[str, Any]) -> 'IStorable':
14 pass
15
16
17class Storable(IStorable):
18 """Default implementation"""
19
20 def raw(self) -> Dict[str, Any]:
21 return self.__dict__
22
23 @classmethod
24 def fromraw(cls, data: Dict[str, Any]) -> 'IStorable':
25 obj = cls.__new__(cls)
26 obj.__dict__.update(data)
27 return obj
28
29
30Basic = Union[int, str, bytes, bool, None]
31StorableType = Union[IStorable, Dict[str, Any], List[Any], int, str, bytes, bool, None]