omlish 0.0.0.dev413__py3-none-any.whl → 0.0.0.dev415__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.
- omlish/__about__.py +2 -3
- omlish/codecs/registry.py +1 -1
- omlish/dataclasses/__init__.py +135 -113
- omlish/dataclasses/impl/api/classes/make.py +5 -3
- omlish/dataclasses/impl/configs.py +29 -29
- omlish/lite/maysyncs.py +1 -0
- omlish/manifests/base.py +1 -1
- omlish/marshal/__init__.py +98 -57
- omlish/marshal/base/__init__.py +0 -0
- omlish/marshal/base/contexts.py +75 -0
- omlish/marshal/{errors.py → base/errors.py} +1 -1
- omlish/marshal/base/options.py +2 -0
- omlish/marshal/base/overrides.py +25 -0
- omlish/marshal/{registries.py → base/registries.py} +4 -8
- omlish/marshal/base/types.py +70 -0
- omlish/marshal/{values.py → base/values.py} +1 -13
- omlish/marshal/composite/iterables.py +7 -7
- omlish/marshal/composite/literals.py +7 -7
- omlish/marshal/composite/mappings.py +7 -7
- omlish/marshal/composite/maybes.py +7 -7
- omlish/marshal/composite/newtypes.py +6 -6
- omlish/marshal/composite/optionals.py +7 -7
- omlish/marshal/composite/special.py +6 -6
- omlish/marshal/composite/wrapped.py +5 -5
- omlish/marshal/factories/__init__.py +0 -0
- omlish/marshal/factories/func.py +28 -0
- omlish/marshal/factories/match.py +34 -0
- omlish/marshal/factories/multi.py +55 -0
- omlish/marshal/factories/recursive.py +120 -0
- omlish/marshal/factories/simple.py +28 -0
- omlish/marshal/factories/typecache.py +91 -0
- omlish/marshal/factories/typemap.py +65 -0
- omlish/marshal/globals.py +7 -7
- omlish/marshal/naming.py +1 -1
- omlish/marshal/objects/dataclasses.py +7 -7
- omlish/marshal/objects/marshal.py +4 -4
- omlish/marshal/objects/metadata.py +4 -4
- omlish/marshal/objects/namedtuples.py +7 -7
- omlish/marshal/objects/unmarshal.py +4 -4
- omlish/marshal/polymorphism/marshal.py +4 -4
- omlish/marshal/polymorphism/metadata.py +1 -1
- omlish/marshal/polymorphism/standard.py +2 -2
- omlish/marshal/polymorphism/unions.py +7 -7
- omlish/marshal/polymorphism/unmarshal.py +4 -4
- omlish/marshal/singular/base64.py +7 -7
- omlish/marshal/singular/datetimes.py +7 -7
- omlish/marshal/singular/enums.py +7 -7
- omlish/marshal/singular/numbers.py +7 -7
- omlish/marshal/singular/primitives.py +7 -7
- omlish/marshal/singular/uuids.py +7 -7
- omlish/marshal/standard.py +8 -8
- omlish/marshal/trivial/any.py +7 -7
- omlish/marshal/trivial/forbidden.py +7 -7
- omlish/marshal/trivial/nop.py +5 -5
- omlish/subprocesses/maysyncs.py +7 -96
- {omlish-0.0.0.dev413.dist-info → omlish-0.0.0.dev415.dist-info}/METADATA +1 -1
- {omlish-0.0.0.dev413.dist-info → omlish-0.0.0.dev415.dist-info}/RECORD +61 -53
- omlish/inject/.dataclasses.json +0 -3
- omlish/marshal/.dataclasses.json +0 -3
- omlish/marshal/base.py +0 -472
- omlish/marshal/factories.py +0 -116
- omlish/marshal/proxy.py +0 -26
- {omlish-0.0.0.dev413.dist-info → omlish-0.0.0.dev415.dist-info}/WHEEL +0 -0
- {omlish-0.0.0.dev413.dist-info → omlish-0.0.0.dev415.dist-info}/entry_points.txt +0 -0
- {omlish-0.0.0.dev413.dist-info → omlish-0.0.0.dev415.dist-info}/licenses/LICENSE +0 -0
- {omlish-0.0.0.dev413.dist-info → omlish-0.0.0.dev415.dist-info}/top_level.txt +0 -0
omlish/marshal/base.py
DELETED
@@ -1,472 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
TODO:
|
3
|
-
- redacted
|
4
|
-
- strongly typed MarshalerFactory base class?
|
5
|
-
- strongly typed Composite/Cached Marshaler/Unmarshaler factories - footgun
|
6
|
-
- streaming? Start/EndObject, etc..
|
7
|
-
- lang.Marker - class name, handle type[Foo]
|
8
|
-
- can't disambiguate from str - can't coexist in bare union
|
9
|
-
- factories being free MatchFns does more harm than good - in practice these are such big guns you want to write a
|
10
|
-
class body if only ceremonially
|
11
|
-
|
12
|
-
See:
|
13
|
-
- https://github.com/python-attrs/cattrs
|
14
|
-
- https://github.com/jcrist/msgspec
|
15
|
-
- https://github.com/Fatal1ty/mashumaro
|
16
|
-
- https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/serializers.md#custom-serializers
|
17
|
-
|
18
|
-
cattrs:
|
19
|
-
*
|
20
|
-
|
21
|
-
Jackson:
|
22
|
-
- USE_ANNOTATIONS
|
23
|
-
- AUTO_DETECT_CREATORS
|
24
|
-
- AUTO_DETECT_FIELDS
|
25
|
-
- AUTO_DETECT_GETTERS
|
26
|
-
- AUTO_DETECT_IS_GETTERS
|
27
|
-
- AUTO_DETECT_SETTERS
|
28
|
-
- REQUIRE_SETTERS_FOR_GETTERS
|
29
|
-
- USE_GETTERS_AS_SETTERS
|
30
|
-
- INFER_CREATOR_FROM_CONSTRUCTOR_PROPERTIES
|
31
|
-
- INFER_PROPERTY_MUTATORS
|
32
|
-
- ALLOW_FINAL_FIELDS_AS_MUTATORS
|
33
|
-
- ALLOW_VOID_VALUED_PROPERTIES
|
34
|
-
- CAN_OVERRIDE_ACCESS_MODIFIERS
|
35
|
-
- OVERRIDE_PUBLIC_ACCESS_MODIFIERS
|
36
|
-
- SORT_PROPERTIES_ALPHABETICALLY
|
37
|
-
- USE_WRAPPER_NAME_AS_PROPERTY_NAME
|
38
|
-
- ACCEPT_CASE_INSENSITIVE_ENUMS
|
39
|
-
- ACCEPT_CASE_INSENSITIVE_PROPERTIES
|
40
|
-
- ACCEPT_CASE_INSENSITIVE_VALUES
|
41
|
-
- ALLOW_EXPLICIT_PROPERTY_RENAMING
|
42
|
-
- USE_STD_BEAN_NAMING
|
43
|
-
- ALLOW_COERCION_OF_SCALARS
|
44
|
-
- DEFAULT_VIEW_INCLUSION
|
45
|
-
- IGNORE_DUPLICATE_MODULE_REGISTRATIONS
|
46
|
-
- IGNORE_MERGE_FOR_UNMERGEABLE
|
47
|
-
- USE_BASE_TYPE_AS_DEFAULT_IMPL
|
48
|
-
- USE_STATIC_TYPING
|
49
|
-
- BLOCK_UNSAFE_POLYMORPHIC_BASE_TYPES
|
50
|
-
|
51
|
-
https://github.com/yukinarit/pyserde
|
52
|
-
- datatypes
|
53
|
-
- typing.Union
|
54
|
-
- typing.NewType for primitive types
|
55
|
-
- typing.Any
|
56
|
-
- typing.Literal
|
57
|
-
- typing.Generic
|
58
|
-
- typing.ClassVar
|
59
|
-
- dataclasses.InitVar
|
60
|
-
- Enum and IntEnum
|
61
|
-
- pathlib.Path
|
62
|
-
- decimal.Decimal
|
63
|
-
- uuid.UUID
|
64
|
-
- datetime.date, datetime.time, datetime.datetime
|
65
|
-
- ipaddress
|
66
|
-
- numpy types
|
67
|
-
- Class Attributes
|
68
|
-
- Field Attributes
|
69
|
-
- Decorators
|
70
|
-
- Type Check
|
71
|
-
- Union Representation
|
72
|
-
- Forward reference
|
73
|
-
- PEP563 Postponed Evaluation of Annotations
|
74
|
-
- PEP585 Type Hinting Generics In Standard Collections
|
75
|
-
- PEP604 Allow writing union types as X | Y
|
76
|
-
- PEP681 Data Class Transform
|
77
|
-
- Case Conversion
|
78
|
-
- Rename
|
79
|
-
- Alias
|
80
|
-
- Skip (de)serialization (skip, skip_if, skip_if_false, skip_if_default)
|
81
|
-
- Custom field (de)serializer
|
82
|
-
- Custom class (de)serializer
|
83
|
-
- Custom global (de)serializer
|
84
|
-
- Flatten
|
85
|
-
"""
|
86
|
-
import abc
|
87
|
-
import typing as ta
|
88
|
-
|
89
|
-
from .. import check
|
90
|
-
from .. import collections as col
|
91
|
-
from .. import dataclasses as dc
|
92
|
-
from .. import lang
|
93
|
-
from .. import reflect as rfl
|
94
|
-
from ..funcs import match as mfs
|
95
|
-
from .errors import UnhandledTypeError
|
96
|
-
from .factories import RecursiveTypeFactory
|
97
|
-
from .factories import TypeCacheFactory
|
98
|
-
from .factories import TypeMapFactory
|
99
|
-
from .proxy import _Proxy
|
100
|
-
from .registries import Registry
|
101
|
-
from .registries import RegistryItem
|
102
|
-
from .values import Value
|
103
|
-
|
104
|
-
|
105
|
-
T = ta.TypeVar('T')
|
106
|
-
|
107
|
-
|
108
|
-
##
|
109
|
-
|
110
|
-
|
111
|
-
class Marshaler(lang.Abstract):
|
112
|
-
@abc.abstractmethod
|
113
|
-
def marshal(self, ctx: 'MarshalContext', o: ta.Any) -> Value:
|
114
|
-
raise NotImplementedError
|
115
|
-
|
116
|
-
|
117
|
-
class Unmarshaler(lang.Abstract):
|
118
|
-
@abc.abstractmethod
|
119
|
-
def unmarshal(self, ctx: 'UnmarshalContext', v: Value) -> ta.Any:
|
120
|
-
raise NotImplementedError
|
121
|
-
|
122
|
-
|
123
|
-
##
|
124
|
-
|
125
|
-
|
126
|
-
MarshalerMaker: ta.TypeAlias = mfs.MatchFn[['MarshalContext', rfl.Type], Marshaler]
|
127
|
-
UnmarshalerMaker: ta.TypeAlias = mfs.MatchFn[['UnmarshalContext', rfl.Type], Unmarshaler]
|
128
|
-
|
129
|
-
|
130
|
-
class MarshalerFactory(lang.Abstract):
|
131
|
-
@property
|
132
|
-
@abc.abstractmethod
|
133
|
-
def make_marshaler(self) -> MarshalerMaker:
|
134
|
-
raise NotImplementedError
|
135
|
-
|
136
|
-
|
137
|
-
class UnmarshalerFactory(lang.Abstract):
|
138
|
-
@property
|
139
|
-
@abc.abstractmethod
|
140
|
-
def make_unmarshaler(self) -> UnmarshalerMaker:
|
141
|
-
raise NotImplementedError
|
142
|
-
|
143
|
-
|
144
|
-
##
|
145
|
-
|
146
|
-
|
147
|
-
@dc.dataclass(frozen=True)
|
148
|
-
class MarshalerFactory_(MarshalerFactory): # noqa
|
149
|
-
fn: MarshalerMaker
|
150
|
-
|
151
|
-
@property
|
152
|
-
def make_marshaler(self) -> MarshalerMaker:
|
153
|
-
return self.fn
|
154
|
-
|
155
|
-
|
156
|
-
@dc.dataclass(frozen=True)
|
157
|
-
class UnmarshalerFactory_(UnmarshalerFactory): # noqa
|
158
|
-
fn: UnmarshalerMaker
|
159
|
-
|
160
|
-
@property
|
161
|
-
def make_unmarshaler(self) -> UnmarshalerMaker:
|
162
|
-
return self.fn
|
163
|
-
|
164
|
-
|
165
|
-
##
|
166
|
-
|
167
|
-
|
168
|
-
class SimpleMarshalerFactory(
|
169
|
-
MarshalerFactory,
|
170
|
-
MarshalerMaker,
|
171
|
-
lang.Abstract,
|
172
|
-
):
|
173
|
-
@property
|
174
|
-
def make_marshaler(self) -> MarshalerMaker:
|
175
|
-
return self
|
176
|
-
|
177
|
-
|
178
|
-
class SimpleUnmarshalerFactory(
|
179
|
-
UnmarshalerFactory,
|
180
|
-
UnmarshalerMaker,
|
181
|
-
lang.Abstract,
|
182
|
-
):
|
183
|
-
@property
|
184
|
-
def make_unmarshaler(self) -> UnmarshalerMaker:
|
185
|
-
return self
|
186
|
-
|
187
|
-
|
188
|
-
##
|
189
|
-
|
190
|
-
|
191
|
-
class MarshalerFactoryMatchClass(
|
192
|
-
MarshalerFactory,
|
193
|
-
mfs.MatchFnClass[['MarshalContext', rfl.Type], Marshaler],
|
194
|
-
lang.Abstract,
|
195
|
-
):
|
196
|
-
@property
|
197
|
-
def make_marshaler(self) -> MarshalerMaker:
|
198
|
-
return self
|
199
|
-
|
200
|
-
|
201
|
-
class UnmarshalerFactoryMatchClass(
|
202
|
-
UnmarshalerFactory,
|
203
|
-
mfs.MatchFnClass[['UnmarshalContext', rfl.Type], Unmarshaler],
|
204
|
-
lang.Abstract,
|
205
|
-
):
|
206
|
-
@property
|
207
|
-
def make_unmarshaler(self) -> UnmarshalerMaker:
|
208
|
-
return self
|
209
|
-
|
210
|
-
|
211
|
-
##
|
212
|
-
|
213
|
-
|
214
|
-
class MultiMarshalerFactory(MarshalerFactory):
|
215
|
-
def __init__(
|
216
|
-
self,
|
217
|
-
fs: ta.Iterable[MarshalerFactory],
|
218
|
-
*,
|
219
|
-
strict: bool = False,
|
220
|
-
) -> None:
|
221
|
-
super().__init__()
|
222
|
-
|
223
|
-
self._fs = list(fs)
|
224
|
-
self._mmf: mfs.MultiMatchFn[[MarshalContext, rfl.Type], Marshaler] = mfs.MultiMatchFn(
|
225
|
-
[f.make_marshaler for f in self._fs],
|
226
|
-
strict=strict,
|
227
|
-
)
|
228
|
-
|
229
|
-
@property
|
230
|
-
def make_marshaler(self) -> MarshalerMaker:
|
231
|
-
return self._mmf
|
232
|
-
|
233
|
-
|
234
|
-
class MultiUnmarshalerFactory(UnmarshalerFactory):
|
235
|
-
def __init__(
|
236
|
-
self,
|
237
|
-
fs: ta.Iterable[UnmarshalerFactory],
|
238
|
-
*,
|
239
|
-
strict: bool = False,
|
240
|
-
) -> None:
|
241
|
-
super().__init__()
|
242
|
-
|
243
|
-
self._fs = list(fs)
|
244
|
-
self._mmf: mfs.MultiMatchFn[[UnmarshalContext, rfl.Type], Unmarshaler] = mfs.MultiMatchFn(
|
245
|
-
[f.make_unmarshaler for f in self._fs],
|
246
|
-
strict=strict,
|
247
|
-
)
|
248
|
-
|
249
|
-
@property
|
250
|
-
def make_unmarshaler(self) -> UnmarshalerMaker:
|
251
|
-
return self._mmf
|
252
|
-
|
253
|
-
|
254
|
-
##
|
255
|
-
|
256
|
-
|
257
|
-
@dc.dataclass(frozen=True)
|
258
|
-
class TypeMapMarshalerFactory(
|
259
|
-
TypeMapFactory['MarshalContext', Marshaler],
|
260
|
-
MarshalerFactory,
|
261
|
-
):
|
262
|
-
@property
|
263
|
-
def make_marshaler(self) -> MarshalerMaker:
|
264
|
-
return self
|
265
|
-
|
266
|
-
|
267
|
-
@dc.dataclass(frozen=True)
|
268
|
-
class TypeMapUnmarshalerFactory(
|
269
|
-
TypeMapFactory['UnmarshalContext', Unmarshaler],
|
270
|
-
UnmarshalerFactory,
|
271
|
-
):
|
272
|
-
@property
|
273
|
-
def make_unmarshaler(self) -> UnmarshalerMaker:
|
274
|
-
return self
|
275
|
-
|
276
|
-
|
277
|
-
# class TypeMapMarshalerFactory(MarshalerFactory):
|
278
|
-
# def __init__(self, m: ta.Mapping[rfl.Type, MarshalerFactory]) -> None:
|
279
|
-
# super().__init__()
|
280
|
-
#
|
281
|
-
# self._m = m
|
282
|
-
# self._tmf: TypeMapFactory['MarshalContext', Marshaler] = TypeMapFactory({
|
283
|
-
# t: f.make_marshaler
|
284
|
-
# for t, f in m.items()
|
285
|
-
# })
|
286
|
-
#
|
287
|
-
# @property
|
288
|
-
# def make_marshaler(self) -> MarshalerMaker:
|
289
|
-
# return self._tmf
|
290
|
-
|
291
|
-
|
292
|
-
# class TypeMapUnmarshalerFactory(UnmarshalerFactory):
|
293
|
-
# def __init__(self, m: ta.Mapping[rfl.Type, UnmarshalerFactory]) -> None:
|
294
|
-
# super().__init__()
|
295
|
-
#
|
296
|
-
# self._m = m
|
297
|
-
# self._tmf: TypeMapFactory[UnmarshalContext, Unmarshaler] = TypeMapFactory({
|
298
|
-
# t: f.make_unmarshaler
|
299
|
-
# for t, f in m.items()
|
300
|
-
# })
|
301
|
-
#
|
302
|
-
# @property
|
303
|
-
# def make_unmarshaler(self) -> UnmarshalerMaker:
|
304
|
-
# return self._tmf
|
305
|
-
|
306
|
-
|
307
|
-
##
|
308
|
-
|
309
|
-
|
310
|
-
class TypeCacheMarshalerFactory(MarshalerFactory):
|
311
|
-
def __init__(self, f: MarshalerFactory) -> None:
|
312
|
-
super().__init__()
|
313
|
-
|
314
|
-
self._f = f
|
315
|
-
self._tcf: TypeCacheFactory[MarshalContext, Marshaler] = TypeCacheFactory(f.make_marshaler)
|
316
|
-
|
317
|
-
@property
|
318
|
-
def make_marshaler(self) -> MarshalerMaker:
|
319
|
-
return self._tcf
|
320
|
-
|
321
|
-
|
322
|
-
class TypeCacheUnmarshalerFactory(UnmarshalerFactory):
|
323
|
-
def __init__(self, f: UnmarshalerFactory) -> None:
|
324
|
-
super().__init__()
|
325
|
-
|
326
|
-
self._f = f
|
327
|
-
self._tcf: TypeCacheFactory[UnmarshalContext, Unmarshaler] = TypeCacheFactory(f.make_unmarshaler)
|
328
|
-
|
329
|
-
@property
|
330
|
-
def make_unmarshaler(self) -> UnmarshalerMaker:
|
331
|
-
return self._tcf
|
332
|
-
|
333
|
-
|
334
|
-
##
|
335
|
-
|
336
|
-
|
337
|
-
@dc.dataclass(frozen=True)
|
338
|
-
class FuncMarshaler(Marshaler, lang.Final):
|
339
|
-
fn: ta.Callable[['MarshalContext', ta.Any], Value]
|
340
|
-
|
341
|
-
def marshal(self, ctx: 'MarshalContext', o: ta.Any) -> Value:
|
342
|
-
return self.fn(ctx, o)
|
343
|
-
|
344
|
-
|
345
|
-
@dc.dataclass(frozen=True)
|
346
|
-
class FuncUnmarshaler(Unmarshaler, lang.Final):
|
347
|
-
fn: ta.Callable[['UnmarshalContext', Value], ta.Any]
|
348
|
-
|
349
|
-
def unmarshal(self, ctx: 'UnmarshalContext', v: Value) -> ta.Any:
|
350
|
-
return self.fn(ctx, v)
|
351
|
-
|
352
|
-
|
353
|
-
##
|
354
|
-
|
355
|
-
|
356
|
-
class Option:
|
357
|
-
pass
|
358
|
-
|
359
|
-
|
360
|
-
##
|
361
|
-
|
362
|
-
|
363
|
-
@dc.dataclass(frozen=True)
|
364
|
-
class BaseContext(lang.Abstract):
|
365
|
-
registry: Registry
|
366
|
-
options: col.TypeMap[Option] = col.TypeMap()
|
367
|
-
|
368
|
-
def _reflect(self, o: ta.Any) -> rfl.Type:
|
369
|
-
def override(o):
|
370
|
-
if (ovr := self.registry.get_of(o, ReflectOverride)):
|
371
|
-
return ovr[-1].rty
|
372
|
-
return None
|
373
|
-
|
374
|
-
return rfl.Reflector(override=override).type(o)
|
375
|
-
|
376
|
-
|
377
|
-
@dc.dataclass(frozen=True)
|
378
|
-
class MarshalContext(BaseContext, lang.Final):
|
379
|
-
factory: MarshalerFactory | None = None
|
380
|
-
|
381
|
-
def make(self, o: ta.Any) -> Marshaler:
|
382
|
-
rty = self._reflect(o)
|
383
|
-
try:
|
384
|
-
return check.not_none(self.factory).make_marshaler(self, rty)
|
385
|
-
except mfs.MatchGuardError:
|
386
|
-
raise UnhandledTypeError(rty) # noqa
|
387
|
-
|
388
|
-
def marshal(self, obj: ta.Any, ty: ta.Any | None = None) -> Value:
|
389
|
-
return self.make(ty if ty is not None else type(obj)).marshal(self, obj)
|
390
|
-
|
391
|
-
|
392
|
-
@dc.dataclass(frozen=True)
|
393
|
-
class UnmarshalContext(BaseContext, lang.Final):
|
394
|
-
factory: UnmarshalerFactory | None = None
|
395
|
-
|
396
|
-
def make(self, o: ta.Any) -> Unmarshaler:
|
397
|
-
rty = self._reflect(o)
|
398
|
-
try:
|
399
|
-
return check.not_none(self.factory).make_unmarshaler(self, rty)
|
400
|
-
except mfs.MatchGuardError:
|
401
|
-
raise UnhandledTypeError(rty) # noqa
|
402
|
-
|
403
|
-
@ta.overload
|
404
|
-
def unmarshal(self, v: Value, ty: type[T]) -> T:
|
405
|
-
...
|
406
|
-
|
407
|
-
@ta.overload
|
408
|
-
def unmarshal(self, v: Value, ty: ta.Any) -> ta.Any:
|
409
|
-
...
|
410
|
-
|
411
|
-
def unmarshal(self, v, ty):
|
412
|
-
return self.make(ty).unmarshal(self, v)
|
413
|
-
|
414
|
-
|
415
|
-
##
|
416
|
-
|
417
|
-
|
418
|
-
class _ProxyMarshaler(_Proxy[Marshaler], Marshaler):
|
419
|
-
def marshal(self, ctx: MarshalContext, o: ta.Any) -> Value:
|
420
|
-
return self._obj.marshal(ctx, o)
|
421
|
-
|
422
|
-
|
423
|
-
class RecursiveMarshalerFactory(MarshalerFactory, lang.Final):
|
424
|
-
def __init__(self, f: MarshalerFactory) -> None:
|
425
|
-
super().__init__()
|
426
|
-
|
427
|
-
self._f = f
|
428
|
-
self._rtf: RecursiveTypeFactory[MarshalContext, Marshaler] = RecursiveTypeFactory(
|
429
|
-
self._f.make_marshaler, # noqa
|
430
|
-
_ProxyMarshaler._new, # noqa
|
431
|
-
)
|
432
|
-
|
433
|
-
@property
|
434
|
-
def make_marshaler(self) -> MarshalerMaker:
|
435
|
-
return self._rtf
|
436
|
-
|
437
|
-
|
438
|
-
class _ProxyUnmarshaler(_Proxy[Unmarshaler], Unmarshaler):
|
439
|
-
def unmarshal(self, ctx: UnmarshalContext, v: Value) -> ta.Any:
|
440
|
-
return self._obj.unmarshal(ctx, v)
|
441
|
-
|
442
|
-
|
443
|
-
class RecursiveUnmarshalerFactory(UnmarshalerFactory, lang.Final):
|
444
|
-
def __init__(self, f: UnmarshalerFactory) -> None:
|
445
|
-
super().__init__()
|
446
|
-
|
447
|
-
self._f = f
|
448
|
-
self._rtf: RecursiveTypeFactory[UnmarshalContext, Unmarshaler] = RecursiveTypeFactory(
|
449
|
-
self._f.make_unmarshaler, # noqa
|
450
|
-
_ProxyUnmarshaler._new, # noqa
|
451
|
-
)
|
452
|
-
|
453
|
-
@property
|
454
|
-
def make_unmarshaler(self) -> UnmarshalerMaker:
|
455
|
-
return self._rtf
|
456
|
-
|
457
|
-
|
458
|
-
##
|
459
|
-
|
460
|
-
|
461
|
-
@dc.dataclass(frozen=True, kw_only=True)
|
462
|
-
class Override(RegistryItem, lang.Final):
|
463
|
-
marshaler: Marshaler | None = dc.xfield(None, check_type=(Marshaler, None))
|
464
|
-
marshaler_factory: MarshalerFactory | None = None
|
465
|
-
|
466
|
-
unmarshaler: Unmarshaler | None = dc.xfield(None, check_type=(Unmarshaler, None))
|
467
|
-
unmarshaler_factory: UnmarshalerFactory | None = None
|
468
|
-
|
469
|
-
|
470
|
-
@dc.dataclass(frozen=True)
|
471
|
-
class ReflectOverride(RegistryItem, lang.Final):
|
472
|
-
rty: rfl.Type
|
omlish/marshal/factories.py
DELETED
@@ -1,116 +0,0 @@
|
|
1
|
-
import dataclasses as dc
|
2
|
-
import threading
|
3
|
-
import typing as ta
|
4
|
-
|
5
|
-
from .. import check
|
6
|
-
from .. import reflect as rfl
|
7
|
-
from ..funcs import match as mfs
|
8
|
-
|
9
|
-
|
10
|
-
R = ta.TypeVar('R')
|
11
|
-
C = ta.TypeVar('C')
|
12
|
-
|
13
|
-
|
14
|
-
##
|
15
|
-
|
16
|
-
|
17
|
-
@dc.dataclass(frozen=True)
|
18
|
-
class TypeMapFactory(mfs.MatchFn[[C, rfl.Type], R]):
|
19
|
-
m: ta.Mapping[rfl.Type, R] = dc.field(default_factory=dict)
|
20
|
-
|
21
|
-
def __post_init__(self) -> None:
|
22
|
-
for k in self.m:
|
23
|
-
if not isinstance(k, rfl.TYPES):
|
24
|
-
raise TypeError(k)
|
25
|
-
|
26
|
-
def guard(self, ctx: C, rty: rfl.Type) -> bool:
|
27
|
-
check.isinstance(rty, rfl.TYPES)
|
28
|
-
return rty in self.m
|
29
|
-
|
30
|
-
def fn(self, ctx: C, rty: rfl.Type) -> R:
|
31
|
-
check.isinstance(rty, rfl.TYPES)
|
32
|
-
try:
|
33
|
-
return self.m[rty]
|
34
|
-
except KeyError:
|
35
|
-
raise mfs.MatchGuardError(ctx, rty) # noqa
|
36
|
-
|
37
|
-
|
38
|
-
##
|
39
|
-
|
40
|
-
|
41
|
-
class TypeCacheFactory(mfs.MatchFn[[C, rfl.Type], R]):
|
42
|
-
def __init__(self, f: mfs.MatchFn[[C, rfl.Type], R]) -> None:
|
43
|
-
super().__init__()
|
44
|
-
|
45
|
-
self._f = f
|
46
|
-
self._dct: dict[rfl.Type, R | None] = {}
|
47
|
-
self._mtx = threading.RLock()
|
48
|
-
|
49
|
-
def guard(self, ctx: C, rty: rfl.Type) -> bool:
|
50
|
-
check.isinstance(rty, rfl.TYPES)
|
51
|
-
with self._mtx:
|
52
|
-
try:
|
53
|
-
e = self._dct[rty]
|
54
|
-
except KeyError:
|
55
|
-
if self._f.guard(ctx, rty):
|
56
|
-
return True
|
57
|
-
else:
|
58
|
-
self._dct[rty] = None
|
59
|
-
return False
|
60
|
-
else:
|
61
|
-
return e is not None
|
62
|
-
|
63
|
-
def fn(self, ctx: C, rty: rfl.Type) -> R:
|
64
|
-
check.isinstance(rty, rfl.TYPES)
|
65
|
-
with self._mtx:
|
66
|
-
try:
|
67
|
-
e = self._dct[rty]
|
68
|
-
except KeyError:
|
69
|
-
try:
|
70
|
-
ret = self._f(ctx, rty)
|
71
|
-
except mfs.MatchGuardError:
|
72
|
-
self._dct[rty] = None
|
73
|
-
raise
|
74
|
-
else:
|
75
|
-
self._dct[rty] = ret
|
76
|
-
return ret
|
77
|
-
else:
|
78
|
-
if e is None:
|
79
|
-
raise mfs.MatchGuardError(ctx, rty)
|
80
|
-
else:
|
81
|
-
return e
|
82
|
-
|
83
|
-
|
84
|
-
##
|
85
|
-
|
86
|
-
|
87
|
-
class RecursiveTypeFactory(mfs.MatchFn[[C, rfl.Type], R]):
|
88
|
-
def __init__(
|
89
|
-
self,
|
90
|
-
f: mfs.MatchFn[[C, rfl.Type], R],
|
91
|
-
prx: ta.Callable[[], tuple[R, ta.Callable[[R], None]]],
|
92
|
-
) -> None:
|
93
|
-
super().__init__()
|
94
|
-
|
95
|
-
self._f = f
|
96
|
-
self._prx = prx
|
97
|
-
self._dct: dict[rfl.Type, R] = {}
|
98
|
-
|
99
|
-
def guard(self, ctx: C, rty: rfl.Type) -> bool:
|
100
|
-
check.isinstance(rty, rfl.TYPES)
|
101
|
-
return self._f.guard(ctx, rty)
|
102
|
-
|
103
|
-
def fn(self, ctx: C, rty: rfl.Type) -> R:
|
104
|
-
check.isinstance(rty, rfl.TYPES)
|
105
|
-
try:
|
106
|
-
return self._dct[rty]
|
107
|
-
except KeyError:
|
108
|
-
pass
|
109
|
-
p, sp = self._prx()
|
110
|
-
self._dct[rty] = p
|
111
|
-
try:
|
112
|
-
r = self._f(ctx, rty)
|
113
|
-
sp(r)
|
114
|
-
return r
|
115
|
-
finally:
|
116
|
-
del self._dct[rty]
|
omlish/marshal/proxy.py
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
import typing as ta
|
2
|
-
|
3
|
-
|
4
|
-
T = ta.TypeVar('T')
|
5
|
-
|
6
|
-
|
7
|
-
##
|
8
|
-
|
9
|
-
|
10
|
-
class _Proxy(ta.Generic[T]):
|
11
|
-
__obj: T | None = None
|
12
|
-
|
13
|
-
@property
|
14
|
-
def _obj(self) -> T:
|
15
|
-
if self.__obj is None:
|
16
|
-
raise TypeError('recursive proxy not set')
|
17
|
-
return self.__obj
|
18
|
-
|
19
|
-
def _set_obj(self, obj: T) -> None:
|
20
|
-
if self.__obj is not None:
|
21
|
-
raise TypeError('recursive proxy already set')
|
22
|
-
self.__obj = obj
|
23
|
-
|
24
|
-
@classmethod
|
25
|
-
def _new(cls) -> tuple[ta.Any, ta.Callable[[ta.Any], None]]:
|
26
|
-
return (p := cls()), p._set_obj # noqa
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|