bagofholding 0.post0.dev1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bagofholding/__init__.py +20 -0
- bagofholding/_version.py +716 -0
- bagofholding/bag.py +266 -0
- bagofholding/content.py +897 -0
- bagofholding/exceptions.py +44 -0
- bagofholding/h5/__init__.py +0 -0
- bagofholding/h5/bag.py +183 -0
- bagofholding/h5/content.py +29 -0
- bagofholding/h5/context.py +58 -0
- bagofholding/h5/dtypes.py +73 -0
- bagofholding/h5/triebag.py +322 -0
- bagofholding/metadata.py +197 -0
- bagofholding/retrieve.py +52 -0
- bagofholding/trie.py +178 -0
- bagofholding/widget.py +109 -0
- bagofholding-0.post0.dev1.dist-info/METADATA +204 -0
- bagofholding-0.post0.dev1.dist-info/RECORD +20 -0
- bagofholding-0.post0.dev1.dist-info/WHEEL +5 -0
- bagofholding-0.post0.dev1.dist-info/licenses/LICENSE +28 -0
- bagofholding-0.post0.dev1.dist-info/top_level.txt +1 -0
bagofholding/content.py
ADDED
|
@@ -0,0 +1,897 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Content controls the decomposition of python objects into their component parts, along
|
|
3
|
+
with a record of what type of object they are, and gathering any relevant metadata.
|
|
4
|
+
|
|
5
|
+
Content is not, of itself, concerned with what you _do_ with this decompositon.
|
|
6
|
+
That is handled via the "packer" protocol (for us, the relevant implementation of this
|
|
7
|
+
is "bags").
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import abc
|
|
13
|
+
import collections.abc
|
|
14
|
+
import dataclasses
|
|
15
|
+
import operator
|
|
16
|
+
import pickle
|
|
17
|
+
import types
|
|
18
|
+
from collections.abc import Callable, Iterable, Iterator, Sized
|
|
19
|
+
from typing import (
|
|
20
|
+
Any,
|
|
21
|
+
ClassVar,
|
|
22
|
+
Generic,
|
|
23
|
+
Protocol,
|
|
24
|
+
Self,
|
|
25
|
+
SupportsIndex,
|
|
26
|
+
TypeAlias,
|
|
27
|
+
TypeVar,
|
|
28
|
+
cast,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
import bidict
|
|
32
|
+
import h5py
|
|
33
|
+
|
|
34
|
+
from bagofholding.exceptions import (
|
|
35
|
+
ModuleForbiddenError,
|
|
36
|
+
NoVersionError,
|
|
37
|
+
PickleProtocolError,
|
|
38
|
+
)
|
|
39
|
+
from bagofholding.metadata import (
|
|
40
|
+
Metadata,
|
|
41
|
+
VersionScrapingMap,
|
|
42
|
+
VersionValidatorType,
|
|
43
|
+
get_module,
|
|
44
|
+
get_qualname,
|
|
45
|
+
get_version,
|
|
46
|
+
validate_version,
|
|
47
|
+
)
|
|
48
|
+
from bagofholding.retrieve import (
|
|
49
|
+
get_importable_string_from_string_reduction,
|
|
50
|
+
import_from_string,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
PackingMemoAlias: TypeAlias = bidict.bidict[int, str]
|
|
54
|
+
ReferencesAlias: TypeAlias = list[object]
|
|
55
|
+
UnpackingMemoAlias: TypeAlias = dict[str, Any]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
PackingType = TypeVar("PackingType", bound=Any)
|
|
59
|
+
UnpackingType = TypeVar("UnpackingType", bound=Any)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class HasContents(Sized, Iterable[str], Protocol): ...
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class Packer(Protocol):
|
|
66
|
+
def join(self, *paths: str) -> str: ...
|
|
67
|
+
|
|
68
|
+
def pack_metadata(self, metadata: Metadata, path: str) -> None: ...
|
|
69
|
+
def unpack_metadata(self, path: str) -> Metadata: ...
|
|
70
|
+
|
|
71
|
+
def pack_empty(self, path: str) -> None: ...
|
|
72
|
+
|
|
73
|
+
def pack_string(self, obj: str, path: str) -> None: ...
|
|
74
|
+
def unpack_string(self, path: str) -> str: ...
|
|
75
|
+
|
|
76
|
+
def pack_bool(self, obj: bool, path: str) -> None: ...
|
|
77
|
+
def unpack_bool(self, path: str) -> bool: ...
|
|
78
|
+
|
|
79
|
+
def pack_long(self, obj: int, path: str) -> None: ...
|
|
80
|
+
def unpack_long(self, path: str) -> int: ...
|
|
81
|
+
|
|
82
|
+
def pack_float(self, obj: float, path: str) -> None: ...
|
|
83
|
+
def unpack_float(self, path: str) -> float: ...
|
|
84
|
+
|
|
85
|
+
def pack_complex(self, obj: complex, path: str) -> None: ...
|
|
86
|
+
def unpack_complex(self, path: str) -> complex: ...
|
|
87
|
+
|
|
88
|
+
def pack_bytes(self, obj: bytes, path: str) -> None: ...
|
|
89
|
+
def unpack_bytes(self, path: str) -> bytes: ...
|
|
90
|
+
|
|
91
|
+
def pack_bytearray(self, obj: bytearray, path: str) -> None: ...
|
|
92
|
+
def unpack_bytearray(self, path: str) -> bytearray: ...
|
|
93
|
+
|
|
94
|
+
def create_group(self, path: str) -> None: ...
|
|
95
|
+
def open_group(self, path: str) -> HasContents: ...
|
|
96
|
+
|
|
97
|
+
def get_bespoke_content_class(
|
|
98
|
+
self, obj: object
|
|
99
|
+
) -> type[BespokeItem[Any, Self]] | None: ...
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@dataclasses.dataclass
|
|
103
|
+
class PackingArguments:
|
|
104
|
+
memo: PackingMemoAlias
|
|
105
|
+
references: ReferencesAlias
|
|
106
|
+
require_versions: bool
|
|
107
|
+
forbidden_modules: list[str] | tuple[str, ...]
|
|
108
|
+
version_scraping: VersionScrapingMap | None
|
|
109
|
+
_pickle_protocol: SupportsIndex
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@dataclasses.dataclass
|
|
113
|
+
class UnpackingArguments:
|
|
114
|
+
memo: UnpackingMemoAlias
|
|
115
|
+
version_validator: VersionValidatorType
|
|
116
|
+
version_scraping: VersionScrapingMap | None
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class NotData:
|
|
120
|
+
pass
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
PackerType = TypeVar("PackerType", bound=Packer)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class Content(Generic[PackingType, UnpackingType, PackerType], abc.ABC):
|
|
127
|
+
|
|
128
|
+
_rich_metadata: ClassVar[bool] = False
|
|
129
|
+
|
|
130
|
+
@classmethod
|
|
131
|
+
@abc.abstractmethod
|
|
132
|
+
def pack(
|
|
133
|
+
cls,
|
|
134
|
+
obj: PackingType,
|
|
135
|
+
packer: PackerType,
|
|
136
|
+
path: str,
|
|
137
|
+
packing: PackingArguments,
|
|
138
|
+
) -> None: ...
|
|
139
|
+
|
|
140
|
+
@classmethod
|
|
141
|
+
@abc.abstractmethod
|
|
142
|
+
def unpack(
|
|
143
|
+
cls, packer: PackerType, path: str, unpacking: UnpackingArguments
|
|
144
|
+
) -> UnpackingType: ...
|
|
145
|
+
|
|
146
|
+
@classmethod
|
|
147
|
+
def _get_metadata(cls, obj: PackingType, packing: PackingArguments) -> Metadata:
|
|
148
|
+
if cls._rich_metadata:
|
|
149
|
+
module = get_module(obj)
|
|
150
|
+
if module == "builtins":
|
|
151
|
+
return Metadata(cls.full_name())
|
|
152
|
+
else:
|
|
153
|
+
if module.split(".")[0] in packing.forbidden_modules:
|
|
154
|
+
raise ModuleForbiddenError(
|
|
155
|
+
f"Module '{module}' is forbidden as a source of stored objects. Change "
|
|
156
|
+
f"the `forbidden_modules` or move this object to an allowed module."
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
version = get_version(module, packing.version_scraping)
|
|
160
|
+
if packing.require_versions and version is None:
|
|
161
|
+
raise NoVersionError(
|
|
162
|
+
f"Could not find a version for {module}. Either disable "
|
|
163
|
+
f"`require_versions`, use `version_scraping` to find an existing "
|
|
164
|
+
f"version for this package, or add versioning to the unversioned "
|
|
165
|
+
f"package."
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
return Metadata(
|
|
169
|
+
cls.full_name(),
|
|
170
|
+
qualname=get_qualname(obj),
|
|
171
|
+
module=module,
|
|
172
|
+
version=version,
|
|
173
|
+
meta=(
|
|
174
|
+
str(obj.__metadata__) if hasattr(obj, "__metadata__") else None
|
|
175
|
+
),
|
|
176
|
+
)
|
|
177
|
+
else:
|
|
178
|
+
return Metadata(cls.full_name())
|
|
179
|
+
|
|
180
|
+
@classmethod
|
|
181
|
+
def full_name(cls) -> str:
|
|
182
|
+
return cls.__module__ + "." + cls.__name__
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
class Item(
|
|
186
|
+
Content[PackingType, UnpackingType, PackerType],
|
|
187
|
+
Generic[PackingType, UnpackingType, PackerType],
|
|
188
|
+
abc.ABC,
|
|
189
|
+
):
|
|
190
|
+
@classmethod
|
|
191
|
+
def pack(
|
|
192
|
+
cls,
|
|
193
|
+
obj: PackingType,
|
|
194
|
+
packer: PackerType,
|
|
195
|
+
path: str,
|
|
196
|
+
packing: PackingArguments,
|
|
197
|
+
) -> None:
|
|
198
|
+
cls._pack_item(obj, packer, path)
|
|
199
|
+
packer.pack_metadata(cls._get_metadata(obj, packing), path)
|
|
200
|
+
|
|
201
|
+
@classmethod
|
|
202
|
+
@abc.abstractmethod
|
|
203
|
+
def _pack_item(cls, obj: PackingType, packer: PackerType, path: str) -> None: ...
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
class Reference(Item[str, Any, Packer]):
|
|
207
|
+
@classmethod
|
|
208
|
+
def _pack_item(cls, obj: str, packer: Packer, path: str) -> None:
|
|
209
|
+
packer.pack_string(obj, path)
|
|
210
|
+
|
|
211
|
+
@classmethod
|
|
212
|
+
def unpack(cls, packer: Packer, path: str, unpacking: UnpackingArguments) -> Any:
|
|
213
|
+
reference = packer.unpack_string(path)
|
|
214
|
+
from_memo = unpacking.memo.get(reference, NotData)
|
|
215
|
+
if from_memo is not NotData:
|
|
216
|
+
return from_memo
|
|
217
|
+
else:
|
|
218
|
+
return unpack(
|
|
219
|
+
packer,
|
|
220
|
+
reference,
|
|
221
|
+
unpacking.memo,
|
|
222
|
+
version_validator=unpacking.version_validator,
|
|
223
|
+
version_scraping=unpacking.version_scraping,
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
GlobalType: TypeAlias = type[type] | types.FunctionType | str
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
class Global(Item[GlobalType, Any, Packer]):
|
|
231
|
+
_rich_metadata = True
|
|
232
|
+
|
|
233
|
+
@classmethod
|
|
234
|
+
def _pack_item(cls, obj: GlobalType, packer: Packer, path: str) -> None:
|
|
235
|
+
value: str
|
|
236
|
+
if isinstance(obj, str):
|
|
237
|
+
value = "builtins." + obj if "." not in obj else obj
|
|
238
|
+
else:
|
|
239
|
+
value = obj.__module__ + "." + obj.__qualname__
|
|
240
|
+
packer.pack_string(value, path)
|
|
241
|
+
|
|
242
|
+
@classmethod
|
|
243
|
+
def unpack(cls, packer: Packer, path: str, unpacking: UnpackingArguments) -> Any:
|
|
244
|
+
import_string = packer.unpack_string(path)
|
|
245
|
+
return import_from_string(import_string)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
class NoneItem(Item[type[None], None, Packer]):
|
|
249
|
+
@classmethod
|
|
250
|
+
def _pack_item(cls, obj: type[None], packer: Packer, path: str) -> None:
|
|
251
|
+
packer.pack_empty(path)
|
|
252
|
+
|
|
253
|
+
@classmethod
|
|
254
|
+
def unpack(cls, packer: Packer, path: str, unpacking: UnpackingArguments) -> None:
|
|
255
|
+
return None
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
ItemType = TypeVar("ItemType", bound=Any)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
class ReflexiveItem(
|
|
262
|
+
Item[ItemType, ItemType, PackerType], Generic[ItemType, PackerType], abc.ABC
|
|
263
|
+
): ...
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
BuiltinItemType = TypeVar(
|
|
267
|
+
"BuiltinItemType",
|
|
268
|
+
str,
|
|
269
|
+
bytes,
|
|
270
|
+
bytearray,
|
|
271
|
+
bool,
|
|
272
|
+
int,
|
|
273
|
+
float,
|
|
274
|
+
complex,
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
class BuiltinItem(
|
|
279
|
+
ReflexiveItem[BuiltinItemType, Packer], Generic[BuiltinItemType], abc.ABC
|
|
280
|
+
): ...
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
class Str(BuiltinItem[str]):
|
|
284
|
+
@classmethod
|
|
285
|
+
def _pack_item(cls, obj: str, packer: Packer, path: str) -> None:
|
|
286
|
+
packer.pack_string(obj, path)
|
|
287
|
+
|
|
288
|
+
@classmethod
|
|
289
|
+
def unpack(cls, packer: Packer, path: str, unpacking: UnpackingArguments) -> str:
|
|
290
|
+
return packer.unpack_string(path)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
class Bool(BuiltinItem[bool]):
|
|
294
|
+
@classmethod
|
|
295
|
+
def _pack_item(cls, obj: bool, packer: Packer, path: str) -> None:
|
|
296
|
+
packer.pack_bool(obj, path)
|
|
297
|
+
|
|
298
|
+
@classmethod
|
|
299
|
+
def unpack(cls, packer: Packer, path: str, unpacking: UnpackingArguments) -> bool:
|
|
300
|
+
return packer.unpack_bool(path)
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
class Long(BuiltinItem[int]):
|
|
304
|
+
@classmethod
|
|
305
|
+
def _pack_item(cls, obj: int, packer: Packer, path: str) -> None:
|
|
306
|
+
packer.pack_long(obj, path)
|
|
307
|
+
|
|
308
|
+
@classmethod
|
|
309
|
+
def unpack(cls, packer: Packer, path: str, unpacking: UnpackingArguments) -> int:
|
|
310
|
+
return packer.unpack_long(path)
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
class Float(BuiltinItem[float]):
|
|
314
|
+
@classmethod
|
|
315
|
+
def _pack_item(cls, obj: float, packer: Packer, path: str) -> None:
|
|
316
|
+
packer.pack_float(obj, path)
|
|
317
|
+
|
|
318
|
+
@classmethod
|
|
319
|
+
def unpack(cls, packer: Packer, path: str, unpacking: UnpackingArguments) -> float:
|
|
320
|
+
return packer.unpack_float(path)
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
class Complex(BuiltinItem[complex]):
|
|
324
|
+
@classmethod
|
|
325
|
+
def _pack_item(cls, obj: complex, packer: Packer, path: str) -> None:
|
|
326
|
+
packer.pack_complex(obj, path)
|
|
327
|
+
|
|
328
|
+
@classmethod
|
|
329
|
+
def unpack(
|
|
330
|
+
cls, packer: Packer, path: str, unpacking: UnpackingArguments
|
|
331
|
+
) -> complex:
|
|
332
|
+
return packer.unpack_complex(path)
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
class Bytes(BuiltinItem[bytes]):
|
|
336
|
+
@classmethod
|
|
337
|
+
def _pack_item(cls, obj: bytes, packer: Packer, path: str) -> None:
|
|
338
|
+
packer.pack_bytes(obj, path)
|
|
339
|
+
|
|
340
|
+
@classmethod
|
|
341
|
+
def unpack(cls, packer: Packer, path: str, unpacking: UnpackingArguments) -> bytes:
|
|
342
|
+
return packer.unpack_bytes(path)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
class Bytearray(BuiltinItem[bytearray]):
|
|
346
|
+
@classmethod
|
|
347
|
+
def _pack_item(cls, obj: bytearray, packer: Packer, path: str) -> None:
|
|
348
|
+
packer.pack_bytearray(obj, path)
|
|
349
|
+
|
|
350
|
+
@classmethod
|
|
351
|
+
def unpack(
|
|
352
|
+
cls, packer: Packer, path: str, unpacking: UnpackingArguments
|
|
353
|
+
) -> bytearray:
|
|
354
|
+
return packer.unpack_bytearray(path)
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
class BespokeItem(
|
|
358
|
+
ReflexiveItem[ItemType, PackerType], Generic[ItemType, PackerType], abc.ABC
|
|
359
|
+
):
|
|
360
|
+
_rich_metadata = True
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
class Group(
|
|
364
|
+
Content[PackingType, UnpackingType, Packer],
|
|
365
|
+
Generic[PackingType, UnpackingType],
|
|
366
|
+
abc.ABC,
|
|
367
|
+
): ...
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
GroupType = TypeVar("GroupType", bound=Any) # Bind to container?
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
class ReflexiveGroup(Group[GroupType, GroupType], Generic[GroupType], abc.ABC): ...
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
# __reduce__ return values
|
|
377
|
+
# per https://docs.python.org/3/library/pickle.html#object.__reduce__
|
|
378
|
+
ConstructorType: TypeAlias = Callable[..., object]
|
|
379
|
+
ConstructorArgsType: TypeAlias = tuple[object, ...]
|
|
380
|
+
StateType: TypeAlias = object
|
|
381
|
+
ListItemsType: TypeAlias = Iterator[object]
|
|
382
|
+
DictItemsType: TypeAlias = Iterator[tuple[object, object]]
|
|
383
|
+
SetStateCallableType: TypeAlias = Callable[[object, object], None]
|
|
384
|
+
ReduceReturnType: TypeAlias = (
|
|
385
|
+
tuple[ConstructorType, ConstructorArgsType]
|
|
386
|
+
| tuple[ConstructorType, ConstructorArgsType, StateType | None]
|
|
387
|
+
| tuple[
|
|
388
|
+
ConstructorType, ConstructorArgsType, StateType | None, ListItemsType | None
|
|
389
|
+
]
|
|
390
|
+
| tuple[
|
|
391
|
+
ConstructorType,
|
|
392
|
+
ConstructorArgsType,
|
|
393
|
+
StateType | None,
|
|
394
|
+
ListItemsType | None,
|
|
395
|
+
DictItemsType | None,
|
|
396
|
+
]
|
|
397
|
+
| tuple[
|
|
398
|
+
ConstructorType | None,
|
|
399
|
+
ConstructorArgsType | None,
|
|
400
|
+
StateType | None,
|
|
401
|
+
ListItemsType | None,
|
|
402
|
+
DictItemsType | None,
|
|
403
|
+
SetStateCallableType | None,
|
|
404
|
+
]
|
|
405
|
+
)
|
|
406
|
+
PickleHint: TypeAlias = str | tuple[Any, ...]
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
class Reducible(ReflexiveGroup[object]):
|
|
410
|
+
_rich_metadata = True
|
|
411
|
+
reduction_fields: ClassVar[tuple[str, str, str, str, str, str]] = (
|
|
412
|
+
"constructor",
|
|
413
|
+
"args",
|
|
414
|
+
"state",
|
|
415
|
+
"item_iterator",
|
|
416
|
+
"kv_iterator",
|
|
417
|
+
"setter",
|
|
418
|
+
)
|
|
419
|
+
|
|
420
|
+
@classmethod
|
|
421
|
+
def pack(
|
|
422
|
+
cls,
|
|
423
|
+
obj: object,
|
|
424
|
+
packer: Packer,
|
|
425
|
+
path: str,
|
|
426
|
+
packing: PackingArguments,
|
|
427
|
+
rv: ReduceReturnType | None = None,
|
|
428
|
+
) -> None:
|
|
429
|
+
reduced_value = (
|
|
430
|
+
obj.__reduce_ex__(packing._pickle_protocol) if rv is None else rv
|
|
431
|
+
)
|
|
432
|
+
packer.create_group(path)
|
|
433
|
+
packer.pack_metadata(cls._get_metadata(obj, packing), path)
|
|
434
|
+
for subpath, value in zip(cls.reduction_fields, reduced_value, strict=False):
|
|
435
|
+
pack(
|
|
436
|
+
value,
|
|
437
|
+
packer,
|
|
438
|
+
packer.join(path, subpath),
|
|
439
|
+
packing.memo,
|
|
440
|
+
packing.references,
|
|
441
|
+
packing.require_versions,
|
|
442
|
+
packing.forbidden_modules,
|
|
443
|
+
packing.version_scraping,
|
|
444
|
+
_pickle_protocol=packing._pickle_protocol,
|
|
445
|
+
)
|
|
446
|
+
|
|
447
|
+
@classmethod
|
|
448
|
+
def unpack(cls, packer: Packer, path: str, unpacking: UnpackingArguments) -> object:
|
|
449
|
+
constructor = cast(
|
|
450
|
+
ConstructorType,
|
|
451
|
+
unpack(
|
|
452
|
+
packer,
|
|
453
|
+
packer.join(path, "constructor"),
|
|
454
|
+
unpacking.memo,
|
|
455
|
+
version_validator=unpacking.version_validator,
|
|
456
|
+
version_scraping=unpacking.version_scraping,
|
|
457
|
+
),
|
|
458
|
+
)
|
|
459
|
+
constructor_args = cast(
|
|
460
|
+
ConstructorArgsType,
|
|
461
|
+
unpack(
|
|
462
|
+
packer,
|
|
463
|
+
packer.join(path, "args"),
|
|
464
|
+
unpacking.memo,
|
|
465
|
+
version_validator=unpacking.version_validator,
|
|
466
|
+
version_scraping=unpacking.version_scraping,
|
|
467
|
+
),
|
|
468
|
+
)
|
|
469
|
+
obj: object = constructor(*constructor_args)
|
|
470
|
+
unpacking.memo[path] = obj
|
|
471
|
+
rv = (constructor, constructor_args) + tuple(
|
|
472
|
+
unpack(
|
|
473
|
+
packer,
|
|
474
|
+
packer.join(path, k),
|
|
475
|
+
unpacking.memo,
|
|
476
|
+
version_validator=unpacking.version_validator,
|
|
477
|
+
version_scraping=unpacking.version_scraping,
|
|
478
|
+
)
|
|
479
|
+
for k in cls.reduction_fields[2 : len(packer.open_group(path))]
|
|
480
|
+
)
|
|
481
|
+
n_items = len(rv)
|
|
482
|
+
if n_items >= 3 and rv[2] is not None:
|
|
483
|
+
if n_items == 6 and rv[5] is not None:
|
|
484
|
+
cast(SetStateCallableType, rv[5])(obj, rv[2])
|
|
485
|
+
elif hasattr(obj, "__setstate__"):
|
|
486
|
+
obj.__setstate__(rv[2])
|
|
487
|
+
else:
|
|
488
|
+
# "If the object has no such method then, the value must be a dictionary"
|
|
489
|
+
obj.__dict__.update(cast(dict[Any, Any], rv[2]))
|
|
490
|
+
if n_items >= 4 and rv[3] is not None:
|
|
491
|
+
if hasattr(obj, "append"):
|
|
492
|
+
for item in cast(ListItemsType, rv[3]):
|
|
493
|
+
obj.append(item)
|
|
494
|
+
elif hasattr(obj, "extend"):
|
|
495
|
+
obj.extend(list(cast(ListItemsType, rv[3])))
|
|
496
|
+
# TODO: look into efficiency choices for optional usage of extend even
|
|
497
|
+
# when append exists
|
|
498
|
+
else:
|
|
499
|
+
raise AttributeError(f"{obj} has neither append nor extend methods")
|
|
500
|
+
if n_items >= 5 and rv[4] is not None and hasattr(obj, "__setitem__"):
|
|
501
|
+
for k, v in cast(DictItemsType, rv[4]):
|
|
502
|
+
obj[k] = v
|
|
503
|
+
|
|
504
|
+
return obj
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
BuiltinGroupType = TypeVar(
|
|
508
|
+
"BuiltinGroupType",
|
|
509
|
+
dict[Any, Any],
|
|
510
|
+
dict[str, Any],
|
|
511
|
+
types.UnionType,
|
|
512
|
+
tuple[Any, ...],
|
|
513
|
+
list[Any],
|
|
514
|
+
set[Any],
|
|
515
|
+
frozenset[Any],
|
|
516
|
+
)
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
class BuiltinGroup(
|
|
520
|
+
ReflexiveGroup[BuiltinGroupType], Generic[BuiltinGroupType], abc.ABC
|
|
521
|
+
):
|
|
522
|
+
@classmethod
|
|
523
|
+
def pack(
|
|
524
|
+
cls,
|
|
525
|
+
obj: PackingType,
|
|
526
|
+
packer: Packer,
|
|
527
|
+
path: str,
|
|
528
|
+
packing: PackingArguments,
|
|
529
|
+
) -> None:
|
|
530
|
+
packer.create_group(path)
|
|
531
|
+
packer.pack_metadata(cls._get_metadata(obj, packing), path)
|
|
532
|
+
cls._pack_subcontent(obj, packer, path, packing)
|
|
533
|
+
|
|
534
|
+
@classmethod
|
|
535
|
+
@abc.abstractmethod
|
|
536
|
+
def _pack_subcontent(
|
|
537
|
+
cls,
|
|
538
|
+
obj: PackingType,
|
|
539
|
+
packer: Packer,
|
|
540
|
+
path: str,
|
|
541
|
+
packing: PackingArguments,
|
|
542
|
+
) -> h5py.Group: ...
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
class Dict(BuiltinGroup[dict[Any, Any]]):
|
|
546
|
+
@classmethod
|
|
547
|
+
def _pack_subcontent(
|
|
548
|
+
cls,
|
|
549
|
+
obj: dict[Any, Any],
|
|
550
|
+
packer: Packer,
|
|
551
|
+
path: str,
|
|
552
|
+
packing: PackingArguments,
|
|
553
|
+
) -> None:
|
|
554
|
+
pack(
|
|
555
|
+
tuple(obj.keys()),
|
|
556
|
+
packer,
|
|
557
|
+
packer.join(path, "keys"),
|
|
558
|
+
packing.memo,
|
|
559
|
+
packing.references,
|
|
560
|
+
packing.require_versions,
|
|
561
|
+
packing.forbidden_modules,
|
|
562
|
+
packing.version_scraping,
|
|
563
|
+
_pickle_protocol=packing._pickle_protocol,
|
|
564
|
+
)
|
|
565
|
+
pack(
|
|
566
|
+
tuple(obj.values()),
|
|
567
|
+
packer,
|
|
568
|
+
packer.join(path, "values"),
|
|
569
|
+
packing.memo,
|
|
570
|
+
packing.references,
|
|
571
|
+
packing.require_versions,
|
|
572
|
+
packing.forbidden_modules,
|
|
573
|
+
packing.version_scraping,
|
|
574
|
+
_pickle_protocol=packing._pickle_protocol,
|
|
575
|
+
)
|
|
576
|
+
|
|
577
|
+
@classmethod
|
|
578
|
+
def unpack(
|
|
579
|
+
cls, packer: Packer, path: str, unpacking: UnpackingArguments
|
|
580
|
+
) -> dict[Any, Any]:
|
|
581
|
+
return dict(
|
|
582
|
+
zip(
|
|
583
|
+
cast(
|
|
584
|
+
tuple[Any],
|
|
585
|
+
unpack(
|
|
586
|
+
packer,
|
|
587
|
+
packer.join(path, "keys"),
|
|
588
|
+
unpacking.memo,
|
|
589
|
+
version_validator=unpacking.version_validator,
|
|
590
|
+
version_scraping=unpacking.version_scraping,
|
|
591
|
+
),
|
|
592
|
+
),
|
|
593
|
+
cast(
|
|
594
|
+
tuple[Any],
|
|
595
|
+
unpack(
|
|
596
|
+
packer,
|
|
597
|
+
packer.join(path, "values"),
|
|
598
|
+
unpacking.memo,
|
|
599
|
+
version_validator=unpacking.version_validator,
|
|
600
|
+
version_scraping=unpacking.version_scraping,
|
|
601
|
+
),
|
|
602
|
+
),
|
|
603
|
+
strict=True,
|
|
604
|
+
)
|
|
605
|
+
)
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
class StrKeyDict(BuiltinGroup[dict[str, Any]]):
|
|
609
|
+
@classmethod
|
|
610
|
+
def _pack_subcontent(
|
|
611
|
+
cls,
|
|
612
|
+
obj: dict[str, Any],
|
|
613
|
+
packer: Packer,
|
|
614
|
+
path: str,
|
|
615
|
+
packing: PackingArguments,
|
|
616
|
+
) -> None:
|
|
617
|
+
for k, v in obj.items():
|
|
618
|
+
pack(
|
|
619
|
+
v,
|
|
620
|
+
packer,
|
|
621
|
+
packer.join(path, k),
|
|
622
|
+
packing.memo,
|
|
623
|
+
packing.references,
|
|
624
|
+
packing.require_versions,
|
|
625
|
+
packing.forbidden_modules,
|
|
626
|
+
packing.version_scraping,
|
|
627
|
+
_pickle_protocol=packing._pickle_protocol,
|
|
628
|
+
)
|
|
629
|
+
|
|
630
|
+
@classmethod
|
|
631
|
+
def unpack(
|
|
632
|
+
cls, packer: Packer, path: str, unpacking: UnpackingArguments
|
|
633
|
+
) -> dict[str, Any]:
|
|
634
|
+
return {
|
|
635
|
+
k: unpack(
|
|
636
|
+
packer,
|
|
637
|
+
packer.join(path, k),
|
|
638
|
+
unpacking.memo,
|
|
639
|
+
version_validator=unpacking.version_validator,
|
|
640
|
+
version_scraping=unpacking.version_scraping,
|
|
641
|
+
)
|
|
642
|
+
for k in packer.open_group(path)
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
class Union(BuiltinGroup[types.UnionType]):
|
|
647
|
+
"""
|
|
648
|
+
:class:`types.UnionType` has no :meth:`__reduce__` method. Pickle actually gets
|
|
649
|
+
around this with bespoke logic, and so we need to too.
|
|
650
|
+
"""
|
|
651
|
+
|
|
652
|
+
@classmethod
|
|
653
|
+
def _pack_subcontent(
|
|
654
|
+
cls,
|
|
655
|
+
obj: types.UnionType,
|
|
656
|
+
packer: Packer,
|
|
657
|
+
path: str,
|
|
658
|
+
packing: PackingArguments,
|
|
659
|
+
) -> None:
|
|
660
|
+
for i, v in enumerate(obj.__args__):
|
|
661
|
+
pack(
|
|
662
|
+
v,
|
|
663
|
+
packer,
|
|
664
|
+
packer.join(path, f"i{i}"),
|
|
665
|
+
packing.memo,
|
|
666
|
+
packing.references,
|
|
667
|
+
packing.require_versions,
|
|
668
|
+
packing.forbidden_modules,
|
|
669
|
+
packing.version_scraping,
|
|
670
|
+
_pickle_protocol=packing._pickle_protocol,
|
|
671
|
+
)
|
|
672
|
+
|
|
673
|
+
@staticmethod
|
|
674
|
+
def _recursive_or(args: collections.abc.Iterable[object]) -> types.UnionType:
|
|
675
|
+
it = iter(args)
|
|
676
|
+
try:
|
|
677
|
+
first = next(it)
|
|
678
|
+
second = next(it)
|
|
679
|
+
except StopIteration:
|
|
680
|
+
raise ValueError("Expected at least two elements for a UnionType") from None
|
|
681
|
+
|
|
682
|
+
union: types.UnionType = operator.or_(first, second)
|
|
683
|
+
|
|
684
|
+
for arg in it:
|
|
685
|
+
union = operator.or_(union, arg)
|
|
686
|
+
|
|
687
|
+
return union
|
|
688
|
+
|
|
689
|
+
@classmethod
|
|
690
|
+
def unpack(
|
|
691
|
+
cls, packer: Packer, path: str, unpacking: UnpackingArguments
|
|
692
|
+
) -> types.UnionType:
|
|
693
|
+
return cls._recursive_or(
|
|
694
|
+
unpack(
|
|
695
|
+
packer,
|
|
696
|
+
packer.join(path, f"i{i}"),
|
|
697
|
+
unpacking.memo,
|
|
698
|
+
version_validator=unpacking.version_validator,
|
|
699
|
+
version_scraping=unpacking.version_scraping,
|
|
700
|
+
)
|
|
701
|
+
for i in range(len(packer.open_group(path)))
|
|
702
|
+
)
|
|
703
|
+
|
|
704
|
+
|
|
705
|
+
IndexableType = TypeVar(
|
|
706
|
+
"IndexableType", tuple[Any, ...], list[Any], set[Any], frozenset[Any]
|
|
707
|
+
)
|
|
708
|
+
|
|
709
|
+
|
|
710
|
+
class Indexable(BuiltinGroup[IndexableType], Generic[IndexableType], abc.ABC):
|
|
711
|
+
recast: type[IndexableType]
|
|
712
|
+
|
|
713
|
+
@classmethod
|
|
714
|
+
def _pack_subcontent(
|
|
715
|
+
cls,
|
|
716
|
+
obj: IndexableType,
|
|
717
|
+
packer: Packer,
|
|
718
|
+
path: str,
|
|
719
|
+
packing: PackingArguments,
|
|
720
|
+
) -> None:
|
|
721
|
+
for i, v in enumerate(obj):
|
|
722
|
+
pack(
|
|
723
|
+
v,
|
|
724
|
+
packer,
|
|
725
|
+
packer.join(path, f"i{i}"),
|
|
726
|
+
packing.memo,
|
|
727
|
+
packing.references,
|
|
728
|
+
packing.require_versions,
|
|
729
|
+
packing.forbidden_modules,
|
|
730
|
+
packing.version_scraping,
|
|
731
|
+
_pickle_protocol=packing._pickle_protocol,
|
|
732
|
+
)
|
|
733
|
+
|
|
734
|
+
@classmethod
|
|
735
|
+
def unpack(
|
|
736
|
+
cls, packer: Packer, path: str, unpacking: UnpackingArguments
|
|
737
|
+
) -> IndexableType:
|
|
738
|
+
return cls.recast(
|
|
739
|
+
unpack(
|
|
740
|
+
packer,
|
|
741
|
+
packer.join(path, f"i{i}"),
|
|
742
|
+
unpacking.memo,
|
|
743
|
+
version_validator=unpacking.version_validator,
|
|
744
|
+
version_scraping=unpacking.version_scraping,
|
|
745
|
+
)
|
|
746
|
+
for i in range(len(packer.open_group(path)))
|
|
747
|
+
)
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
class Tuple(Indexable[tuple[Any, ...]]):
|
|
751
|
+
recast = tuple
|
|
752
|
+
|
|
753
|
+
|
|
754
|
+
class List(Indexable[list[Any]]):
|
|
755
|
+
recast = list
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
class Set(Indexable[set[Any]]):
|
|
759
|
+
recast = set
|
|
760
|
+
|
|
761
|
+
|
|
762
|
+
class FrozenSet(Indexable[frozenset[Any]]):
|
|
763
|
+
recast = frozenset
|
|
764
|
+
|
|
765
|
+
|
|
766
|
+
def pack(
|
|
767
|
+
obj: object,
|
|
768
|
+
packer: Packer,
|
|
769
|
+
path: str,
|
|
770
|
+
memo: PackingMemoAlias,
|
|
771
|
+
references: ReferencesAlias,
|
|
772
|
+
require_versions: bool,
|
|
773
|
+
forbidden_modules: list[str] | tuple[str, ...],
|
|
774
|
+
version_scraping: VersionScrapingMap | None,
|
|
775
|
+
_pickle_protocol: SupportsIndex = pickle.DEFAULT_PROTOCOL,
|
|
776
|
+
) -> None:
|
|
777
|
+
if _pickle_protocol not in (4, 3, 2, 1, 0):
|
|
778
|
+
raise PickleProtocolError(
|
|
779
|
+
f"pickle protocol must be <= 4, got {_pickle_protocol}"
|
|
780
|
+
)
|
|
781
|
+
|
|
782
|
+
packing_args = PackingArguments(
|
|
783
|
+
memo=memo,
|
|
784
|
+
references=references,
|
|
785
|
+
require_versions=require_versions,
|
|
786
|
+
forbidden_modules=forbidden_modules,
|
|
787
|
+
version_scraping=version_scraping,
|
|
788
|
+
_pickle_protocol=_pickle_protocol,
|
|
789
|
+
)
|
|
790
|
+
|
|
791
|
+
t = type if isinstance(obj, type) else type(obj)
|
|
792
|
+
simple_class = KNOWN_ITEM_MAP.get(t)
|
|
793
|
+
if simple_class is not None:
|
|
794
|
+
simple_class.pack(
|
|
795
|
+
obj,
|
|
796
|
+
packer,
|
|
797
|
+
path,
|
|
798
|
+
packing_args,
|
|
799
|
+
)
|
|
800
|
+
return
|
|
801
|
+
|
|
802
|
+
obj_id = id(obj)
|
|
803
|
+
reference = memo.get(obj_id)
|
|
804
|
+
if reference is not None:
|
|
805
|
+
Reference.pack(reference, packer, path, packing_args)
|
|
806
|
+
return
|
|
807
|
+
else:
|
|
808
|
+
memo[obj_id] = path
|
|
809
|
+
references.append(obj)
|
|
810
|
+
|
|
811
|
+
complex_class = packer.get_bespoke_content_class(obj)
|
|
812
|
+
if complex_class is not None:
|
|
813
|
+
complex_class.pack(obj, packer, path, packing_args)
|
|
814
|
+
return
|
|
815
|
+
|
|
816
|
+
group_class = get_group_content_class(obj)
|
|
817
|
+
if group_class is not None:
|
|
818
|
+
group_class.pack(obj, packer, path, packing_args)
|
|
819
|
+
return
|
|
820
|
+
|
|
821
|
+
rv = obj.__reduce_ex__(_pickle_protocol)
|
|
822
|
+
if isinstance(rv, str):
|
|
823
|
+
Global.pack(
|
|
824
|
+
get_importable_string_from_string_reduction(rv, obj),
|
|
825
|
+
packer,
|
|
826
|
+
path,
|
|
827
|
+
packing_args,
|
|
828
|
+
)
|
|
829
|
+
return
|
|
830
|
+
else:
|
|
831
|
+
Reducible.pack(obj, packer, path, packing_args, rv=rv)
|
|
832
|
+
return
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
KNOWN_ITEM_MAP: dict[
|
|
836
|
+
type | types.FunctionType | types.BuiltinFunctionType, type[Item[Any, Any, Packer]]
|
|
837
|
+
] = {
|
|
838
|
+
type: Global,
|
|
839
|
+
types.FunctionType: Global,
|
|
840
|
+
type(all): Global,
|
|
841
|
+
type(None): NoneItem,
|
|
842
|
+
bool: Bool,
|
|
843
|
+
int: Long,
|
|
844
|
+
float: Float,
|
|
845
|
+
complex: Complex,
|
|
846
|
+
bytes: Bytes,
|
|
847
|
+
bytearray: Bytearray,
|
|
848
|
+
str: Str,
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
KNOWN_GROUP_MAP: dict[type, type[Group[Any, Any]]] = {
|
|
853
|
+
dict: Dict,
|
|
854
|
+
types.UnionType: Union,
|
|
855
|
+
tuple: Tuple,
|
|
856
|
+
list: List,
|
|
857
|
+
set: Set,
|
|
858
|
+
frozenset: FrozenSet,
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
def get_group_content_class(obj: object) -> type[Group[Any, Any]] | None:
|
|
863
|
+
t = type(obj)
|
|
864
|
+
if t is dict and all(isinstance(k, str) for k in cast(dict[str, Any], obj)):
|
|
865
|
+
return StrKeyDict
|
|
866
|
+
|
|
867
|
+
return KNOWN_GROUP_MAP.get(t)
|
|
868
|
+
|
|
869
|
+
|
|
870
|
+
def unpack(
|
|
871
|
+
packer: Packer,
|
|
872
|
+
path: str,
|
|
873
|
+
memo: UnpackingMemoAlias,
|
|
874
|
+
version_validator: VersionValidatorType,
|
|
875
|
+
version_scraping: VersionScrapingMap | None,
|
|
876
|
+
) -> object:
|
|
877
|
+
memo_value = memo.get(path, NotData)
|
|
878
|
+
if memo_value is NotData:
|
|
879
|
+
metadata = packer.unpack_metadata(path)
|
|
880
|
+
content_class = import_from_string(metadata.content_type)
|
|
881
|
+
if metadata is not None:
|
|
882
|
+
validate_version(
|
|
883
|
+
metadata, validator=version_validator, version_scraping=version_scraping
|
|
884
|
+
)
|
|
885
|
+
value = content_class.unpack(
|
|
886
|
+
packer,
|
|
887
|
+
path,
|
|
888
|
+
UnpackingArguments(
|
|
889
|
+
memo=memo,
|
|
890
|
+
version_validator=version_validator,
|
|
891
|
+
version_scraping=version_scraping,
|
|
892
|
+
),
|
|
893
|
+
)
|
|
894
|
+
if path not in memo:
|
|
895
|
+
memo[path] = value
|
|
896
|
+
return value
|
|
897
|
+
return memo_value
|