koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 1 | """ |
| 2 | This module contains interfaces for storage classes |
| 3 | """ |
| 4 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 5 | import os |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 6 | import abc |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 7 | from typing import Any, Iterable, TypeVar, Type, IO, Tuple, cast, List |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 8 | |
| 9 | |
| 10 | class IStorable(metaclass=abc.ABCMeta): |
| 11 | """Interface for type, which can be stored""" |
| 12 | @abc.abstractmethod |
| 13 | def __getstate__(self) -> Any: |
| 14 | pass |
| 15 | |
| 16 | @abc.abstractmethod |
| 17 | def __setstate__(self, Any): |
| 18 | pass |
| 19 | |
| 20 | |
| 21 | # all builtin types can be stored |
| 22 | IStorable.register(list) # type: ignore |
| 23 | IStorable.register(dict) # type: ignore |
| 24 | IStorable.register(tuple) # type: ignore |
| 25 | IStorable.register(set) # type: ignore |
| 26 | IStorable.register(None) # type: ignore |
| 27 | IStorable.register(int) # type: ignore |
| 28 | IStorable.register(str) # type: ignore |
| 29 | IStorable.register(bytes) # type: ignore |
| 30 | IStorable.register(bool) # type: ignore |
| 31 | |
| 32 | |
| 33 | ObjClass = TypeVar('ObjClass') |
| 34 | |
| 35 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 36 | class ISimpleStorage(metaclass=abc.ABCMeta): |
| 37 | """interface for low-level storage, which doesn't support serialization |
| 38 | and can operate only on bytes""" |
| 39 | |
| 40 | @abc.abstractmethod |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 41 | def __setitem__(self, path: str, value: bytes) -> None: |
| 42 | pass |
| 43 | |
| 44 | @abc.abstractmethod |
| 45 | def __getitem__(self, path: str) -> bytes: |
| 46 | pass |
| 47 | |
| 48 | @abc.abstractmethod |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 49 | def __delitem__(self, path: str) -> None: |
| 50 | pass |
| 51 | |
| 52 | @abc.abstractmethod |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 53 | def __contains__(self, path: str) -> bool: |
| 54 | pass |
| 55 | |
| 56 | @abc.abstractmethod |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 57 | def list(self, path: str) -> Iterable[Tuple[bool, str]]: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 58 | pass |
| 59 | |
| 60 | @abc.abstractmethod |
| 61 | def get_stream(self, path: str) -> IO: |
| 62 | pass |
| 63 | |
| 64 | |
| 65 | class ISerializer(metaclass=abc.ABCMeta): |
| 66 | """Interface for serialization class""" |
| 67 | @abc.abstractmethod |
| 68 | def pack(self, value: IStorable) -> bytes: |
| 69 | pass |
| 70 | |
| 71 | @abc.abstractmethod |
| 72 | def unpack(self, data: bytes) -> IStorable: |
| 73 | pass |
| 74 | |
| 75 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 76 | class FSStorage(ISimpleStorage): |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 77 | """Store all data in files on FS""" |
| 78 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 79 | def __init__(self, root_path: str, existing: bool) -> None: |
| 80 | self.root_path = root_path |
| 81 | if existing: |
| 82 | if not os.path.isdir(self.root_path): |
| 83 | raise ValueError("No storage found at {!r}".format(root_path)) |
| 84 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 85 | def __setitem__(self, path: str, value: bytes) -> None: |
| 86 | path = os.path.join(self.root_path, path) |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 87 | os.makedirs(os.path.dirname(path), exist_ok=True) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 88 | with open(path, "wb") as fd: |
| 89 | fd.write(value) |
| 90 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 91 | def __delitem__(self, path: str) -> None: |
| 92 | try: |
| 93 | os.unlink(path) |
| 94 | except FileNotFoundError: |
| 95 | pass |
| 96 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 97 | def __getitem__(self, path: str) -> bytes: |
| 98 | path = os.path.join(self.root_path, path) |
| 99 | with open(path, "rb") as fd: |
| 100 | return fd.read() |
| 101 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 102 | def __contains__(self, path: str) -> bool: |
| 103 | path = os.path.join(self.root_path, path) |
| 104 | return os.path.exists(path) |
| 105 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 106 | def list(self, path: str) -> Iterable[Tuple[bool, str]]: |
| 107 | path = os.path.join(self.root_path, path) |
| 108 | for entry in os.scandir(path): |
| 109 | if not entry.name in ('..', '.'): |
| 110 | yield entry.is_file(), entry.name |
| 111 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 112 | def get_stream(self, path: str, mode: str = "rb") -> IO: |
| 113 | path = os.path.join(self.root_path, path) |
| 114 | return open(path, mode) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 115 | |
| 116 | |
| 117 | class YAMLSerializer(ISerializer): |
| 118 | """Serialize data to yaml""" |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 119 | def pack(self, value: IStorable) -> bytes: |
| 120 | raise NotImplementedError() |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 121 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 122 | def unpack(self, data: bytes) -> IStorable: |
| 123 | raise NotImplementedError() |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 124 | |
| 125 | |
| 126 | class Storage: |
| 127 | """interface for storage""" |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 128 | def __init__(self, storage: ISimpleStorage, serializer: ISerializer) -> None: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 129 | self.storage = storage |
| 130 | self.serializer = serializer |
| 131 | |
| 132 | def __setitem__(self, path: str, value: IStorable) -> None: |
| 133 | self.storage[path] = self.serializer.pack(value) |
| 134 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 135 | def __getitem__(self, path: str) -> IStorable: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 136 | return self.serializer.unpack(self.storage[path]) |
| 137 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 138 | def __delitem__(self, path: str) -> None: |
| 139 | del self.storage[path] |
| 140 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 141 | def __contains__(self, path: str) -> bool: |
| 142 | return path in self.storage |
| 143 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 144 | def list(self, path: str) -> Iterable[Tuple[bool, str]]: |
| 145 | return self.storage.list(path) |
| 146 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 147 | def construct(self, path: str, raw_val: IStorable, obj_class: Type[ObjClass]) -> ObjClass: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 148 | if obj_class in (int, str, dict, list, None): |
| 149 | if not isinstance(raw_val, obj_class): |
| 150 | raise ValueError("Can't load path {!r} into type {}. Real type is {}" |
| 151 | .format(path, obj_class, type(raw_val))) |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 152 | return cast(ObjClass, raw_val) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 153 | |
| 154 | if not isinstance(raw_val, dict): |
| 155 | raise ValueError("Can't load path {!r} into python type. Raw value not dict".format(path)) |
| 156 | |
| 157 | if not all(isinstance(str, key) for key in raw_val.keys): |
| 158 | raise ValueError("Can't load path {!r} into python type.".format(path) + |
| 159 | "Raw not all keys in raw value is strings") |
| 160 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 161 | obj = obj_class.__new__(obj_class) # type: ObjClass |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 162 | obj.__dict__.update(raw_val) |
| 163 | return obj |
| 164 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 165 | def load_list(self, path: str, obj_class: Type[ObjClass]) -> List[ObjClass]: |
| 166 | raw_val = self[path] |
| 167 | assert isinstance(raw_val, list) |
| 168 | return [self.construct(path, val, obj_class) for val in cast(list, raw_val)] |
| 169 | |
| 170 | def load(self, path: str, obj_class: Type[ObjClass]) -> ObjClass: |
| 171 | return self.construct(path, self[path], obj_class) |
| 172 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 173 | def get_stream(self, path: str) -> IO: |
| 174 | return self.storage.get_stream(path) |
| 175 | |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame^] | 176 | def get(self, path: str, default: Any = None) -> Any: |
| 177 | try: |
| 178 | return self[path] |
| 179 | except KeyError: |
| 180 | return default |
| 181 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 182 | |
| 183 | def make_storage(url: str, existing: bool = False) -> Storage: |
| 184 | return Storage(FSStorage(url, existing), YAMLSerializer()) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 185 | |