|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import base64 |
4 | | -import importlib |
5 | | -import pickle |
6 | | -from typing import TYPE_CHECKING, cast, final |
| 3 | +from typing import TYPE_CHECKING, final |
7 | 4 |
|
8 | 5 | from typing_extensions import Protocol, override |
9 | 6 |
|
10 | 7 | from duron.log import is_json_value |
11 | 8 |
|
12 | | -BaseModel: type[_pydantic.BaseModel] | None = None |
13 | | -try: |
14 | | - import pydantic as _pydantic |
15 | | - |
16 | | - BaseModel = _pydantic.BaseModel |
17 | | -except ImportError: |
18 | | - pass |
19 | | - |
20 | 9 | if TYPE_CHECKING: |
21 | 10 | from duron.log import JSONValue |
22 | 11 |
|
23 | | -__all__ = ["Codec", "DEFAULT_CODEC"] |
| 12 | +__all__ = ["Codec", "DefaultCodec"] |
24 | 13 |
|
25 | 14 |
|
26 | 15 | class Codec(Protocol): |
27 | 16 | def encode_json(self, result: object) -> JSONValue: ... |
28 | 17 | def decode_json(self, encoded: JSONValue) -> object: ... |
29 | 18 |
|
30 | | - def encode_state(self, obj: object) -> str: ... |
31 | | - def decode_state(self, state: str) -> object: ... |
32 | | - |
33 | 19 |
|
34 | 20 | @final |
35 | | -class _DefaultCodec(Codec): |
36 | | - def __init__(self) -> None: |
37 | | - self._type_cache: dict[str, type] = {} |
38 | | - |
39 | | - def _lookup_model(self, qual: str) -> type: |
40 | | - cls = self._type_cache.get(qual) |
41 | | - if cls is not None: |
42 | | - return cls |
43 | | - |
44 | | - mod, name = qual.split(":", 1) |
45 | | - obj: object = importlib.import_module(mod) |
46 | | - for part in name.split("."): |
47 | | - obj = getattr(obj, part) |
48 | | - if isinstance(obj, type): |
49 | | - self._type_cache[qual] = obj |
50 | | - return obj |
51 | | - else: |
52 | | - raise TypeError(f"Imported object is not a type: {obj!r}") |
53 | | - |
| 21 | +class DefaultCodec(Codec): |
54 | 22 | @override |
55 | 23 | def encode_json(self, result: object) -> JSONValue: |
56 | | - if BaseModel and isinstance(result, BaseModel): |
57 | | - cls = result.__class__ |
58 | | - model = result.model_dump(mode="json") |
59 | | - model["_duron.pydantic"] = f"{cls.__module__}:{cls.__qualname__}" |
60 | | - return model |
61 | 24 | if is_json_value(result): |
62 | 25 | return result |
63 | 26 | raise TypeError(f"Result is not JSON-serializable: {result!r}") |
64 | 27 |
|
65 | 28 | @override |
66 | 29 | def decode_json(self, encoded: JSONValue) -> object: |
67 | | - if isinstance(encoded, dict) and "_duron.pydantic" in encoded: |
68 | | - model = self._lookup_model(cast("str", encoded["_duron.pydantic"])) |
69 | | - if not BaseModel or not issubclass(model, BaseModel): |
70 | | - raise TypeError(f"Decoded class is not a BaseModel subclass: {model}") |
71 | | - return model.model_validate({ |
72 | | - k: v for k, v in encoded.items() if not k.startswith("_duron.") |
73 | | - }) |
74 | 30 | return encoded |
75 | | - |
76 | | - @override |
77 | | - def encode_state(self, obj: object) -> str: |
78 | | - return base64.b64encode(pickle.dumps(obj, protocol=5)).decode("ascii") |
79 | | - |
80 | | - @override |
81 | | - def decode_state(self, state: str) -> object: |
82 | | - return pickle.loads(base64.b64decode(state.encode())) |
83 | | - |
84 | | - |
85 | | -DEFAULT_CODEC: Codec = _DefaultCodec() |
0 commit comments