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 | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 6 | import re |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 7 | import abc |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 8 | import shutil |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 9 | import sqlite3 |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 10 | import logging |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 11 | from typing import Any, TypeVar, Type, IO, Tuple, cast, List, Dict, Iterable, Iterator |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 12 | |
| 13 | import yaml |
| 14 | try: |
| 15 | from yaml import CLoader as Loader, CDumper as Dumper # type: ignore |
| 16 | except ImportError: |
| 17 | from yaml import Loader, Dumper # type: ignore |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 18 | import numpy |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 19 | |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 20 | from .common_types import IStorable |
| 21 | |
| 22 | |
| 23 | logger = logging.getLogger("wally") |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 24 | |
| 25 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 26 | class ISimpleStorage(metaclass=abc.ABCMeta): |
| 27 | """interface for low-level storage, which doesn't support serialization |
| 28 | and can operate only on bytes""" |
| 29 | |
| 30 | @abc.abstractmethod |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 31 | def put(self, value: bytes, path: str) -> None: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 32 | pass |
| 33 | |
| 34 | @abc.abstractmethod |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 35 | def get(self, path: str) -> bytes: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 36 | pass |
| 37 | |
| 38 | @abc.abstractmethod |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 39 | def rm(self, path: str) -> None: |
| 40 | pass |
| 41 | |
| 42 | @abc.abstractmethod |
| 43 | def sync(self) -> None: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 44 | pass |
| 45 | |
| 46 | @abc.abstractmethod |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 47 | def __contains__(self, path: str) -> bool: |
| 48 | pass |
| 49 | |
| 50 | @abc.abstractmethod |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 51 | def get_fd(self, path: str, mode: str = "rb+") -> IO: |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 52 | pass |
| 53 | |
| 54 | @abc.abstractmethod |
| 55 | def sub_storage(self, path: str) -> 'ISimpleStorage': |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 56 | pass |
| 57 | |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 58 | @abc.abstractmethod |
| 59 | def list(self, path: str) -> Iterator[Tuple[bool, str]]: |
| 60 | pass |
| 61 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 62 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 63 | class ITSStorage(metaclass=abc.ABCMeta): |
| 64 | """interface for low-level storage, which doesn't support serialization |
| 65 | and can operate only on bytes""" |
| 66 | |
| 67 | @abc.abstractmethod |
| 68 | def put(self, value: bytes, path: str) -> None: |
| 69 | pass |
| 70 | |
| 71 | @abc.abstractmethod |
| 72 | def get(self, path: str) -> bytes: |
| 73 | pass |
| 74 | |
| 75 | @abc.abstractmethod |
| 76 | def rm(self, path: str) -> None: |
| 77 | pass |
| 78 | |
| 79 | @abc.abstractmethod |
| 80 | def sync(self) -> None: |
| 81 | pass |
| 82 | |
| 83 | @abc.abstractmethod |
| 84 | def __contains__(self, path: str) -> bool: |
| 85 | pass |
| 86 | |
| 87 | @abc.abstractmethod |
| 88 | def get_fd(self, path: str, mode: str = "rb+") -> IO: |
| 89 | pass |
| 90 | |
| 91 | @abc.abstractmethod |
| 92 | def sub_storage(self, path: str) -> 'ISimpleStorage': |
| 93 | pass |
| 94 | |
| 95 | @abc.abstractmethod |
| 96 | def list(self, path: str) -> Iterator[Tuple[bool, str]]: |
| 97 | pass |
| 98 | |
| 99 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 100 | class ISerializer(metaclass=abc.ABCMeta): |
| 101 | """Interface for serialization class""" |
| 102 | @abc.abstractmethod |
koder aka kdanilov | f286517 | 2016-12-30 03:35:11 +0200 | [diff] [blame] | 103 | def pack(self, value: IStorable) -> bytes: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 104 | pass |
| 105 | |
| 106 | @abc.abstractmethod |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 107 | def unpack(self, data: bytes) -> Any: |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 108 | pass |
| 109 | |
| 110 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 111 | class FSStorage(ISimpleStorage): |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 112 | """Store all data in files on FS""" |
| 113 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 114 | def __init__(self, root_path: str, existing: bool) -> None: |
| 115 | self.root_path = root_path |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 116 | self.existing = existing |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 117 | self.ignored = {'.', '..'} |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 118 | |
| 119 | def j(self, path: str) -> str: |
| 120 | return os.path.join(self.root_path, path) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 121 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 122 | def put(self, value: bytes, path: str) -> None: |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 123 | jpath = self.j(path) |
| 124 | os.makedirs(os.path.dirname(jpath), exist_ok=True) |
| 125 | with open(jpath, "wb") as fd: |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 126 | fd.write(value) |
| 127 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 128 | def get(self, path: str) -> bytes: |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 129 | try: |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 130 | with open(self.j(path), "rb") as fd: |
| 131 | return fd.read() |
| 132 | except FileNotFoundError as exc: |
| 133 | raise KeyError(path) from exc |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 134 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 135 | def rm(self, path: str) -> None: |
| 136 | if os.path.isdir(path): |
| 137 | shutil.rmtree(path, ignore_errors=True) |
| 138 | elif os.path.exists(path): |
| 139 | os.unlink(path) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 140 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 141 | def __contains__(self, path: str) -> bool: |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 142 | return os.path.exists(self.j(path)) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 143 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 144 | def get_fd(self, path: str, mode: str = "rb+") -> IO[bytes]: |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 145 | jpath = self.j(path) |
| 146 | |
| 147 | if "cb" == mode: |
| 148 | create_on_fail = True |
| 149 | mode = "rb+" |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 150 | os.makedirs(os.path.dirname(jpath), exist_ok=True) |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 151 | elif "ct" == mode: |
| 152 | create_on_fail = True |
| 153 | mode = "rt+" |
| 154 | os.makedirs(os.path.dirname(jpath), exist_ok=True) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 155 | else: |
| 156 | create_on_fail = False |
| 157 | |
| 158 | try: |
| 159 | fd = open(jpath, mode) |
| 160 | except IOError: |
| 161 | if not create_on_fail: |
| 162 | raise |
koder aka kdanilov | a732a60 | 2017-02-01 20:29:56 +0200 | [diff] [blame] | 163 | |
| 164 | if 't' in mode: |
| 165 | fd = open(jpath, "wt") |
| 166 | else: |
| 167 | fd = open(jpath, "wb") |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 168 | |
| 169 | return cast(IO[bytes], fd) |
| 170 | |
| 171 | def sub_storage(self, path: str) -> 'FSStorage': |
| 172 | return self.__class__(self.j(path), self.existing) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 173 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 174 | def sync(self): |
| 175 | pass |
koder aka kdanilov | 23e6bdf | 2016-12-24 02:18:54 +0200 | [diff] [blame] | 176 | |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 177 | def list(self, path: str) -> Iterator[Tuple[bool, str]]: |
koder aka kdanilov | f286517 | 2016-12-30 03:35:11 +0200 | [diff] [blame] | 178 | path = self.j(path) |
| 179 | |
| 180 | if not os.path.exists(path): |
| 181 | return |
| 182 | |
| 183 | if not os.path.isdir(path): |
| 184 | raise OSError("{!r} is not a directory".format(path)) |
| 185 | |
| 186 | for fobj in os.scandir(path): |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 187 | if fobj.path not in self.ignored: |
| 188 | if fobj.is_dir(): |
| 189 | yield False, fobj.name |
| 190 | else: |
| 191 | yield True, fobj.name |
| 192 | |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 193 | |
| 194 | class YAMLSerializer(ISerializer): |
| 195 | """Serialize data to yaml""" |
koder aka kdanilov | f286517 | 2016-12-30 03:35:11 +0200 | [diff] [blame] | 196 | def pack(self, value: IStorable) -> bytes: |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 197 | try: |
| 198 | return yaml.dump(value, Dumper=Dumper, encoding="utf8") |
| 199 | except Exception as exc: |
| 200 | raise ValueError("Can't pickle object {!r} to yaml".format(type(value))) from exc |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 201 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 202 | def unpack(self, data: bytes) -> Any: |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 203 | return yaml.load(data, Loader=Loader) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 204 | |
| 205 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 206 | class SAFEYAMLSerializer(ISerializer): |
| 207 | """Serialize data to yaml""" |
koder aka kdanilov | f286517 | 2016-12-30 03:35:11 +0200 | [diff] [blame] | 208 | def pack(self, value: IStorable) -> bytes: |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 209 | try: |
| 210 | return yaml.safe_dump(value, encoding="utf8") |
| 211 | except Exception as exc: |
| 212 | raise ValueError("Can't pickle object {!r} to yaml".format(type(value))) from exc |
| 213 | |
| 214 | def unpack(self, data: bytes) -> Any: |
| 215 | return yaml.safe_load(data) |
| 216 | |
| 217 | |
| 218 | ObjClass = TypeVar('ObjClass', bound=IStorable) |
| 219 | |
| 220 | |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 221 | class _Raise: |
| 222 | pass |
| 223 | |
| 224 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 225 | class Storage: |
| 226 | """interface for storage""" |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 227 | |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 228 | def __init__(self, sstorage: ISimpleStorage, serializer: ISerializer) -> None: |
| 229 | self.sstorage = sstorage |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 230 | self.serializer = serializer |
| 231 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 232 | def sub_storage(self, *path: str) -> 'Storage': |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 233 | fpath = "/".join(path) |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 234 | return self.__class__(self.sstorage.sub_storage(fpath), self.serializer) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 235 | |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 236 | def put(self, value: Any, *path: str) -> None: |
| 237 | dct_value = cast(IStorable, value).raw() if isinstance(value, IStorable) else value |
| 238 | serialized = self.serializer.pack(dct_value) # type: ignore |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 239 | fpath = "/".join(path) |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 240 | self.sstorage.put(serialized, fpath) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 241 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 242 | def put_list(self, value: Iterable[IStorable], *path: str) -> None: |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 243 | serialized = self.serializer.pack([obj.raw() for obj in value]) # type: ignore |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 244 | fpath = "/".join(path) |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 245 | self.sstorage.put(serialized, fpath) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 246 | |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 247 | def get(self, path: str, default: Any = _Raise) -> Any: |
| 248 | try: |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 249 | vl = self.sstorage.get(path) |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 250 | except: |
| 251 | if default is _Raise: |
| 252 | raise |
| 253 | return default |
| 254 | |
| 255 | return self.serializer.unpack(vl) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 256 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 257 | def rm(self, *path: str) -> None: |
| 258 | fpath = "/".join(path) |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 259 | self.sstorage.rm(fpath) |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 260 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 261 | def __contains__(self, path: str) -> bool: |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 262 | return path in self.sstorage |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 263 | |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 264 | def put_raw(self, val: bytes, *path: str) -> str: |
| 265 | fpath = "/".join(path) |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 266 | self.sstorage.put(val, fpath) |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 267 | # TODO: dirty hack |
| 268 | return self.resolve_raw(fpath) |
| 269 | |
| 270 | def resolve_raw(self, fpath) -> str: |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 271 | return cast(FSStorage, self.sstorage).j(fpath) |
koder aka kdanilov | 3af3c33 | 2016-12-19 17:12:34 +0200 | [diff] [blame] | 272 | |
| 273 | def get_raw(self, *path: str) -> bytes: |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 274 | return self.sstorage.get("/".join(path)) |
koder aka kdanilov | 3af3c33 | 2016-12-19 17:12:34 +0200 | [diff] [blame] | 275 | |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 276 | def append_raw(self, value: bytes, *path: str) -> None: |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 277 | with self.sstorage.get_fd("/".join(path), "rb+") as fd: |
koder aka kdanilov | f286517 | 2016-12-30 03:35:11 +0200 | [diff] [blame] | 278 | fd.seek(0, os.SEEK_END) |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 279 | fd.write(value) |
| 280 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 281 | def get_fd(self, path: str, mode: str = "r") -> IO: |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 282 | return self.sstorage.get_fd(path, mode) |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 283 | |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 284 | def load_list(self, obj_class: Type[ObjClass], *path: str) -> List[ObjClass]: |
| 285 | path_s = "/".join(path) |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 286 | raw_val = cast(List[Dict[str, Any]], self.get(path_s)) |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 287 | assert isinstance(raw_val, list) |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 288 | return [cast(ObjClass, obj_class.fromraw(val)) for val in raw_val] |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 289 | |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 290 | def load(self, obj_class: Type[ObjClass], *path: str) -> ObjClass: |
| 291 | path_s = "/".join(path) |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 292 | return cast(ObjClass, obj_class.fromraw(self.get(path_s))) |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 293 | |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 294 | def sync(self) -> None: |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 295 | self.sstorage.sync() |
koder aka kdanilov | 7308462 | 2016-11-16 21:51:08 +0200 | [diff] [blame] | 296 | |
koder aka kdanilov | 39e449e | 2016-12-17 15:15:26 +0200 | [diff] [blame] | 297 | def __enter__(self) -> 'Storage': |
| 298 | return self |
| 299 | |
| 300 | def __exit__(self, x: Any, y: Any, z: Any) -> None: |
koder aka kdanilov | 7f59d56 | 2016-12-26 01:34:23 +0200 | [diff] [blame] | 301 | self.sync() |
koder aka kdanilov | 7022706 | 2016-11-26 23:23:21 +0200 | [diff] [blame] | 302 | |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 303 | def list(self, *path: str) -> Iterator[Tuple[bool, str]]: |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 304 | return self.sstorage.list("/".join(path)) |
koder aka kdanilov | ffaf48d | 2016-12-27 02:25:29 +0200 | [diff] [blame] | 305 | |
koder aka kdanilov | 108ac36 | 2017-01-19 20:17:16 +0200 | [diff] [blame] | 306 | def _iter_paths(self, |
| 307 | root: str, |
| 308 | path_parts: List[str], |
| 309 | groups: Dict[str, str]) -> Iterator[Tuple[bool, str, Dict[str, str]]]: |
| 310 | |
| 311 | curr = path_parts[0] |
| 312 | rest = path_parts[1:] |
| 313 | |
| 314 | for is_file, name in self.list(root): |
| 315 | if rest and is_file: |
| 316 | continue |
| 317 | |
| 318 | rr = re.match(pattern=curr + "$", string=name) |
| 319 | if rr: |
| 320 | if root: |
| 321 | path = root + "/" + name |
| 322 | else: |
| 323 | path = name |
| 324 | |
| 325 | new_groups = rr.groupdict().copy() |
| 326 | new_groups.update(groups) |
| 327 | |
| 328 | if rest: |
| 329 | yield from self._iter_paths(path, rest, new_groups) |
| 330 | else: |
| 331 | yield is_file, path, new_groups |
| 332 | |
koder aka kdanilov | 3d2bc4f | 2016-11-12 18:31:18 +0200 | [diff] [blame] | 333 | |
| 334 | def make_storage(url: str, existing: bool = False) -> Storage: |
kdanylov aka koder | 0e0cfcb | 2017-03-27 22:19:09 +0300 | [diff] [blame^] | 335 | return Storage(FSStorage(url, existing), SAFEYAMLSerializer()) |
koder aka kdanilov | 22d134e | 2016-11-08 11:33:19 +0200 | [diff] [blame] | 336 | |