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