koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame^] | 1 | """ |
| 2 | This module contains interfaces for storage classes |
| 3 | """ |
| 4 | |
| 5 | import abc |
| 6 | from typing import Any, Iterable, TypeVar, Type, IO |
| 7 | |
| 8 | |
| 9 | class IStorable(metaclass=abc.ABCMeta): |
| 10 | """Interface for type, which can be stored""" |
| 11 | @abc.abstractmethod |
| 12 | def __getstate__(self) -> Any: |
| 13 | pass |
| 14 | |
| 15 | @abc.abstractmethod |
| 16 | def __setstate__(self, Any): |
| 17 | pass |
| 18 | |
| 19 | |
| 20 | # all builtin types can be stored |
| 21 | IStorable.register(list) # type: ignore |
| 22 | IStorable.register(dict) # type: ignore |
| 23 | IStorable.register(tuple) # type: ignore |
| 24 | IStorable.register(set) # type: ignore |
| 25 | IStorable.register(None) # type: ignore |
| 26 | IStorable.register(int) # type: ignore |
| 27 | IStorable.register(str) # type: ignore |
| 28 | IStorable.register(bytes) # type: ignore |
| 29 | IStorable.register(bool) # type: ignore |
| 30 | |
| 31 | |
| 32 | ObjClass = TypeVar('ObjClass') |
| 33 | |
| 34 | |
| 35 | class IStorage(metaclass=abc.ABCMeta): |
| 36 | """interface for storage""" |
| 37 | @abc.abstractmethod |
| 38 | def __init__(self, path: str, existing_storage: bool = False) -> None: |
| 39 | pass |
| 40 | |
| 41 | @abc.abstractmethod |
| 42 | def __setitem__(self, path: str, value: IStorable) -> None: |
| 43 | pass |
| 44 | |
| 45 | @abc.abstractmethod |
| 46 | def __getitem__(self, path: str) -> IStorable: |
| 47 | pass |
| 48 | |
| 49 | @abc.abstractmethod |
| 50 | def __contains__(self, path: str) -> bool: |
| 51 | pass |
| 52 | |
| 53 | @abc.abstractmethod |
| 54 | def list(self, path: str) -> Iterable[str]: |
| 55 | pass |
| 56 | |
| 57 | @abc.abstractmethod |
| 58 | def load(self, path: str, obj_class: Type[ObjClass]) -> ObjClass: |
| 59 | pass |
| 60 | |
| 61 | @abc.abstractmethod |
| 62 | def get_stream(self, path: str) -> IO: |
| 63 | pass |
| 64 | |
| 65 | |
| 66 | class ISimpleStorage(metaclass=abc.ABCMeta): |
| 67 | """interface for low-level storage, which doesn't support serialization |
| 68 | and can operate only on bytes""" |
| 69 | |
| 70 | @abc.abstractmethod |
| 71 | def __init__(self, path: str) -> None: |
| 72 | pass |
| 73 | |
| 74 | @abc.abstractmethod |
| 75 | def __setitem__(self, path: str, value: bytes) -> None: |
| 76 | pass |
| 77 | |
| 78 | @abc.abstractmethod |
| 79 | def __getitem__(self, path: str) -> bytes: |
| 80 | pass |
| 81 | |
| 82 | @abc.abstractmethod |
| 83 | def __contains__(self, path: str) -> bool: |
| 84 | pass |
| 85 | |
| 86 | @abc.abstractmethod |
| 87 | def list(self, path: str) -> Iterable[str]: |
| 88 | pass |
| 89 | |
| 90 | @abc.abstractmethod |
| 91 | def get_stream(self, path: str) -> IO: |
| 92 | pass |
| 93 | |
| 94 | |
| 95 | class ISerializer(metaclass=abc.ABCMeta): |
| 96 | """Interface for serialization class""" |
| 97 | @abc.abstractmethod |
| 98 | def pack(self, value: IStorable) -> bytes: |
| 99 | pass |
| 100 | |
| 101 | @abc.abstractmethod |
| 102 | def unpack(self, data: bytes) -> IStorable: |
| 103 | pass |
| 104 | |
| 105 | |
| 106 | # TODO(koder): this is concrete storage and serializer classes to be implemented |
| 107 | class FSStorage(IStorage): |
| 108 | """Store all data in files on FS""" |
| 109 | |
| 110 | @abc.abstractmethod |
| 111 | def __init__(self, root_path: str, serializer: ISerializer, existing: bool = False) -> None: |
| 112 | pass |
| 113 | |
| 114 | |
| 115 | class YAMLSerializer(ISerializer): |
| 116 | """Serialize data to yaml""" |
| 117 | pass |
| 118 | |
| 119 | |
| 120 | def make_storage(url: str, existing: bool = False) -> IStorage: |
| 121 | return FSStorage(url, YAMLSerializer(), existing) |
| 122 | |