omlish 0.0.0.dev453__py3-none-any.whl → 0.0.0.dev454__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 -2
- omlish/collections/identity.py +1 -0
- omlish/dataclasses/__init__.py +2 -0
- omlish/funcs/guard.py +27 -16
- omlish/lang/__init__.py +1 -0
- omlish/lang/iterables.py +8 -0
- omlish/lite/attrops.py +2 -0
- omlish/lite/dataclasses.py +30 -0
- omlish/marshal/__init__.py +12 -11
- omlish/marshal/base/contexts.py +4 -7
- omlish/marshal/base/funcs.py +16 -11
- omlish/marshal/base/types.py +17 -7
- omlish/marshal/composite/iterables.py +31 -20
- omlish/marshal/composite/literals.py +14 -18
- omlish/marshal/composite/mappings.py +34 -23
- omlish/marshal/composite/maybes.py +27 -19
- omlish/marshal/composite/newtypes.py +14 -14
- omlish/marshal/composite/optionals.py +12 -14
- omlish/marshal/composite/special.py +13 -13
- omlish/marshal/composite/unions/__init__.py +0 -0
- omlish/marshal/composite/unions/literals.py +91 -0
- omlish/marshal/composite/unions/primitives.py +101 -0
- omlish/marshal/factories/invalidate.py +16 -66
- omlish/marshal/factories/method.py +28 -0
- omlish/marshal/factories/moduleimport/factories.py +13 -54
- omlish/marshal/factories/multi.py +11 -23
- omlish/marshal/factories/recursive.py +40 -56
- omlish/marshal/factories/typecache.py +23 -75
- omlish/marshal/factories/typemap.py +40 -41
- omlish/marshal/objects/dataclasses.py +106 -97
- omlish/marshal/objects/marshal.py +15 -12
- omlish/marshal/objects/namedtuples.py +46 -40
- omlish/marshal/objects/unmarshal.py +16 -13
- omlish/marshal/polymorphism/marshal.py +6 -9
- omlish/marshal/polymorphism/unions.py +15 -9
- omlish/marshal/polymorphism/unmarshal.py +6 -8
- omlish/marshal/singular/enums.py +12 -18
- omlish/marshal/standard.py +8 -8
- omlish/marshal/trivial/forbidden.py +19 -24
- omlish/os/forkhooks.py +4 -0
- omlish/specs/jsonrpc/_marshal.py +33 -24
- omlish/specs/openapi/_marshal.py +20 -17
- omlish/typedvalues/marshal.py +81 -55
- {omlish-0.0.0.dev453.dist-info → omlish-0.0.0.dev454.dist-info}/METADATA +1 -1
- {omlish-0.0.0.dev453.dist-info → omlish-0.0.0.dev454.dist-info}/RECORD +49 -48
- omlish/marshal/composite/unions.py +0 -213
- omlish/marshal/factories/match.py +0 -34
- omlish/marshal/factories/simple.py +0 -28
- {omlish-0.0.0.dev453.dist-info → omlish-0.0.0.dev454.dist-info}/WHEEL +0 -0
- {omlish-0.0.0.dev453.dist-info → omlish-0.0.0.dev454.dist-info}/entry_points.txt +0 -0
- {omlish-0.0.0.dev453.dist-info → omlish-0.0.0.dev454.dist-info}/licenses/LICENSE +0 -0
- {omlish-0.0.0.dev453.dist-info → omlish-0.0.0.dev454.dist-info}/top_level.txt +0 -0
@@ -1,15 +1,13 @@
|
|
1
1
|
import typing as ta
|
2
2
|
|
3
3
|
from ... import reflect as rfl
|
4
|
-
from ...funcs import
|
4
|
+
from ...funcs import guard as gfs
|
5
5
|
from ..base.contexts import MarshalContext
|
6
6
|
from ..base.contexts import UnmarshalContext
|
7
7
|
from ..base.types import Marshaler
|
8
8
|
from ..base.types import MarshalerFactory
|
9
|
-
from ..base.types import MarshalerMaker
|
10
9
|
from ..base.types import Unmarshaler
|
11
10
|
from ..base.types import UnmarshalerFactory
|
12
|
-
from ..base.types import UnmarshalerMaker
|
13
11
|
|
14
12
|
|
15
13
|
##
|
@@ -18,38 +16,28 @@ from ..base.types import UnmarshalerMaker
|
|
18
16
|
class MultiMarshalerFactory(MarshalerFactory):
|
19
17
|
def __init__(
|
20
18
|
self,
|
21
|
-
|
22
|
-
*,
|
19
|
+
*facs: MarshalerFactory,
|
23
20
|
strict: bool = False,
|
24
21
|
) -> None:
|
25
22
|
super().__init__()
|
26
23
|
|
27
|
-
self.
|
28
|
-
self.
|
29
|
-
[f.make_marshaler for f in self._fs],
|
30
|
-
strict=strict,
|
31
|
-
)
|
24
|
+
self._facs = facs
|
25
|
+
self._mgf = gfs.multi(*[f.make_marshaler for f in self._facs], strict=strict)
|
32
26
|
|
33
|
-
|
34
|
-
|
35
|
-
return self._mmf
|
27
|
+
def make_marshaler(self, ctx: MarshalContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
|
28
|
+
return self._mgf(ctx, rty)
|
36
29
|
|
37
30
|
|
38
31
|
class MultiUnmarshalerFactory(UnmarshalerFactory):
|
39
32
|
def __init__(
|
40
33
|
self,
|
41
|
-
|
42
|
-
*,
|
34
|
+
*facs: UnmarshalerFactory,
|
43
35
|
strict: bool = False,
|
44
36
|
) -> None:
|
45
37
|
super().__init__()
|
46
38
|
|
47
|
-
self.
|
48
|
-
self.
|
49
|
-
[f.make_unmarshaler for f in self._fs],
|
50
|
-
strict=strict,
|
51
|
-
)
|
39
|
+
self._facs = facs
|
40
|
+
self._mgf = gfs.multi(*[f.make_unmarshaler for f in self._facs], strict=strict)
|
52
41
|
|
53
|
-
|
54
|
-
|
55
|
-
return self._mmf
|
42
|
+
def make_unmarshaler(self, ctx: UnmarshalContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
|
43
|
+
return self._mgf(ctx, rty)
|
@@ -1,61 +1,52 @@
|
|
1
1
|
import typing as ta
|
2
2
|
|
3
|
-
from ... import check
|
4
|
-
from ... import lang
|
5
3
|
from ... import reflect as rfl
|
6
|
-
from ...funcs import match as mfs
|
7
4
|
from ..base.contexts import MarshalContext
|
8
5
|
from ..base.contexts import UnmarshalContext
|
9
6
|
from ..base.types import Marshaler
|
10
7
|
from ..base.types import MarshalerFactory
|
11
|
-
from ..base.types import MarshalerMaker
|
12
8
|
from ..base.types import Unmarshaler
|
13
9
|
from ..base.types import UnmarshalerFactory
|
14
|
-
from ..base.types import UnmarshalerMaker
|
15
10
|
from ..base.values import Value
|
16
11
|
|
17
12
|
|
13
|
+
FactoryT = ta.TypeVar('FactoryT', bound=MarshalerFactory | UnmarshalerFactory)
|
18
14
|
T = ta.TypeVar('T')
|
19
|
-
R = ta.TypeVar('R')
|
20
|
-
C = ta.TypeVar('C')
|
21
15
|
|
22
16
|
|
23
17
|
##
|
24
18
|
|
25
19
|
|
26
|
-
class
|
20
|
+
class _RecursiveFactory(ta.Generic[FactoryT]):
|
27
21
|
def __init__(
|
28
22
|
self,
|
29
|
-
|
30
|
-
prx: ta.Callable[[], tuple[
|
23
|
+
fac: FactoryT,
|
24
|
+
prx: ta.Callable[[], tuple[ta.Any, ta.Callable[[ta.Any], None]]],
|
31
25
|
) -> None:
|
32
26
|
super().__init__()
|
33
27
|
|
34
|
-
self.
|
28
|
+
self._fac = fac
|
35
29
|
self._prx = prx
|
36
|
-
self._dct: dict[rfl.Type, R] = {}
|
37
|
-
|
38
|
-
def guard(self, ctx: C, rty: rfl.Type) -> bool:
|
39
|
-
check.isinstance(rty, rfl.TYPES)
|
40
|
-
return self._f.guard(ctx, rty)
|
41
|
-
|
42
|
-
def fn(self, ctx: C, rty: rfl.Type) -> R:
|
43
|
-
check.isinstance(rty, rfl.TYPES)
|
44
|
-
try:
|
45
|
-
return self._dct[rty]
|
46
|
-
except KeyError:
|
47
|
-
pass
|
48
|
-
p, sp = self._prx()
|
49
|
-
self._dct[rty] = p
|
50
|
-
try:
|
51
|
-
r = self._f(ctx, rty)
|
52
|
-
sp(r)
|
53
|
-
return r
|
54
|
-
finally:
|
55
|
-
del self._dct[rty]
|
56
30
|
|
31
|
+
self._dct: dict[rfl.Type, ta.Any] = {}
|
57
32
|
|
58
|
-
|
33
|
+
def _wrap(self, m, rty):
|
34
|
+
def inner():
|
35
|
+
try:
|
36
|
+
return self._dct[rty]
|
37
|
+
except KeyError:
|
38
|
+
pass
|
39
|
+
|
40
|
+
p, sp = self._prx()
|
41
|
+
self._dct[rty] = p
|
42
|
+
try:
|
43
|
+
r = m()
|
44
|
+
sp(r)
|
45
|
+
return r
|
46
|
+
finally:
|
47
|
+
del self._dct[rty]
|
48
|
+
|
49
|
+
return inner
|
59
50
|
|
60
51
|
|
61
52
|
class _Proxy(ta.Generic[T]):
|
@@ -77,7 +68,7 @@ class _Proxy(ta.Generic[T]):
|
|
77
68
|
return (p := cls()), p._set_obj # noqa
|
78
69
|
|
79
70
|
|
80
|
-
|
71
|
+
#
|
81
72
|
|
82
73
|
|
83
74
|
class _ProxyMarshaler(_Proxy[Marshaler], Marshaler):
|
@@ -85,19 +76,17 @@ class _ProxyMarshaler(_Proxy[Marshaler], Marshaler):
|
|
85
76
|
return self._obj.marshal(ctx, o)
|
86
77
|
|
87
78
|
|
88
|
-
class RecursiveMarshalerFactory(MarshalerFactory,
|
89
|
-
def __init__(self,
|
90
|
-
super().__init__()
|
79
|
+
class RecursiveMarshalerFactory(_RecursiveFactory[MarshalerFactory], MarshalerFactory):
|
80
|
+
def __init__(self, fac: MarshalerFactory) -> None:
|
81
|
+
super().__init__(fac, _ProxyMarshaler._new) # noqa
|
91
82
|
|
92
|
-
|
93
|
-
self.
|
94
|
-
|
95
|
-
|
96
|
-
)
|
83
|
+
def make_marshaler(self, ctx: MarshalContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
|
84
|
+
if (m := self._fac.make_marshaler(ctx, rty)) is None:
|
85
|
+
return None
|
86
|
+
return self._wrap(m, rty)
|
97
87
|
|
98
|
-
|
99
|
-
|
100
|
-
return self._rtf
|
88
|
+
|
89
|
+
#
|
101
90
|
|
102
91
|
|
103
92
|
class _ProxyUnmarshaler(_Proxy[Unmarshaler], Unmarshaler):
|
@@ -105,16 +94,11 @@ class _ProxyUnmarshaler(_Proxy[Unmarshaler], Unmarshaler):
|
|
105
94
|
return self._obj.unmarshal(ctx, v)
|
106
95
|
|
107
96
|
|
108
|
-
class RecursiveUnmarshalerFactory(UnmarshalerFactory,
|
109
|
-
def __init__(self,
|
110
|
-
super().__init__()
|
97
|
+
class RecursiveUnmarshalerFactory(_RecursiveFactory[UnmarshalerFactory], UnmarshalerFactory):
|
98
|
+
def __init__(self, fac: UnmarshalerFactory) -> None:
|
99
|
+
super().__init__(fac, _ProxyUnmarshaler._new) # noqa
|
111
100
|
|
112
|
-
|
113
|
-
self.
|
114
|
-
|
115
|
-
|
116
|
-
)
|
117
|
-
|
118
|
-
@property
|
119
|
-
def make_unmarshaler(self) -> UnmarshalerMaker:
|
120
|
-
return self._rtf
|
101
|
+
def make_unmarshaler(self, ctx: UnmarshalContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
|
102
|
+
if (m := self._fac.make_unmarshaler(ctx, rty)) is None:
|
103
|
+
return None
|
104
|
+
return self._wrap(m, rty)
|
@@ -1,111 +1,59 @@
|
|
1
|
+
"""
|
2
|
+
FIXME:
|
3
|
+
- fix Context lifetime broken design already
|
4
|
+
- obvious: FactoryContext
|
5
|
+
"""
|
1
6
|
import threading
|
2
7
|
import typing as ta
|
3
8
|
|
4
9
|
from ... import check
|
5
10
|
from ... import reflect as rfl
|
6
|
-
from ...funcs import match as mfs
|
7
11
|
from ..base.contexts import MarshalContext
|
8
12
|
from ..base.contexts import UnmarshalContext
|
9
13
|
from ..base.types import Marshaler
|
10
14
|
from ..base.types import MarshalerFactory
|
11
|
-
from ..base.types import MarshalerMaker
|
12
15
|
from ..base.types import Unmarshaler
|
13
16
|
from ..base.types import UnmarshalerFactory
|
14
|
-
from ..base.types import UnmarshalerMaker
|
15
17
|
|
16
18
|
|
17
|
-
|
18
|
-
C = ta.TypeVar('C')
|
19
|
+
FactoryT = ta.TypeVar('FactoryT', bound=MarshalerFactory | UnmarshalerFactory)
|
19
20
|
|
20
21
|
|
21
22
|
##
|
22
23
|
|
23
24
|
|
24
|
-
class _TypeCacheFactory(
|
25
|
-
def __init__(self,
|
25
|
+
class _TypeCacheFactory(ta.Generic[FactoryT]):
|
26
|
+
def __init__(self, fac: FactoryT) -> None:
|
26
27
|
super().__init__()
|
27
28
|
|
28
|
-
self.
|
29
|
-
self._dct: dict[rfl.Type, R | None] = {}
|
30
|
-
self._lock = threading.RLock()
|
31
|
-
|
32
|
-
def guard(self, ctx: C, rty: rfl.Type) -> bool:
|
33
|
-
check.isinstance(rty, rfl.TYPES)
|
34
|
-
|
35
|
-
try:
|
36
|
-
return self._dct[rty] is not None
|
37
|
-
except KeyError:
|
38
|
-
pass
|
39
|
-
|
40
|
-
with self._lock:
|
41
|
-
try:
|
42
|
-
e = self._dct[rty]
|
43
|
-
|
44
|
-
except KeyError:
|
45
|
-
if self._f.guard(ctx, rty):
|
46
|
-
return True
|
47
|
-
else:
|
48
|
-
self._dct[rty] = None
|
49
|
-
return False
|
29
|
+
self._fac = fac
|
50
30
|
|
51
|
-
|
52
|
-
|
31
|
+
self._dct: dict[rfl.Type, ta.Any | None] = {}
|
32
|
+
self._lock = threading.RLock()
|
53
33
|
|
54
|
-
def
|
34
|
+
def _make(self, rty, dfl):
|
55
35
|
check.isinstance(rty, rfl.TYPES)
|
56
36
|
|
57
37
|
try:
|
58
|
-
|
38
|
+
return self._dct[rty]
|
59
39
|
except KeyError:
|
60
40
|
pass
|
61
|
-
else:
|
62
|
-
if e is None:
|
63
|
-
raise mfs.MatchGuardError(ctx, rty)
|
64
|
-
else:
|
65
|
-
return e
|
66
41
|
|
67
42
|
with self._lock:
|
68
43
|
try:
|
69
|
-
|
70
|
-
|
44
|
+
return self._dct[rty]
|
71
45
|
except KeyError:
|
72
|
-
|
73
|
-
ret = self._f(ctx, rty)
|
74
|
-
except mfs.MatchGuardError:
|
75
|
-
self._dct[rty] = None
|
76
|
-
raise
|
77
|
-
else:
|
78
|
-
self._dct[rty] = ret
|
79
|
-
return ret
|
80
|
-
|
81
|
-
if e is None:
|
82
|
-
raise mfs.MatchGuardError(ctx, rty)
|
83
|
-
else:
|
84
|
-
return e
|
46
|
+
pass
|
85
47
|
|
86
|
-
|
87
|
-
|
48
|
+
m = self._dct[rty] = dfl()
|
49
|
+
return m
|
88
50
|
|
89
51
|
|
90
|
-
class TypeCacheMarshalerFactory(MarshalerFactory):
|
91
|
-
def
|
92
|
-
|
93
|
-
|
94
|
-
self._f = f
|
95
|
-
self._tcf: _TypeCacheFactory[MarshalContext, Marshaler] = _TypeCacheFactory(f.make_marshaler)
|
96
|
-
|
97
|
-
@property
|
98
|
-
def make_marshaler(self) -> MarshalerMaker:
|
99
|
-
return self._tcf
|
100
|
-
|
101
|
-
|
102
|
-
class TypeCacheUnmarshalerFactory(UnmarshalerFactory):
|
103
|
-
def __init__(self, f: UnmarshalerFactory) -> None:
|
104
|
-
super().__init__()
|
52
|
+
class TypeCacheMarshalerFactory(_TypeCacheFactory[MarshalerFactory], MarshalerFactory):
|
53
|
+
def make_marshaler(self, ctx: MarshalContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
|
54
|
+
return self._make(rty, lambda: self._fac.make_marshaler(ctx, rty))
|
105
55
|
|
106
|
-
self._f = f
|
107
|
-
self._tcf: _TypeCacheFactory[UnmarshalContext, Unmarshaler] = _TypeCacheFactory(f.make_unmarshaler)
|
108
56
|
|
109
|
-
|
110
|
-
def make_unmarshaler(self) ->
|
111
|
-
return self.
|
57
|
+
class TypeCacheUnmarshalerFactory(_TypeCacheFactory[UnmarshalerFactory], UnmarshalerFactory):
|
58
|
+
def make_unmarshaler(self, ctx: UnmarshalContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
|
59
|
+
return self._make(rty, lambda: self._fac.make_unmarshaler(ctx, rty))
|
@@ -3,63 +3,62 @@ import typing as ta
|
|
3
3
|
from ... import check
|
4
4
|
from ... import dataclasses as dc
|
5
5
|
from ... import reflect as rfl
|
6
|
-
from ...funcs import match as mfs
|
7
6
|
from ..base.contexts import MarshalContext
|
8
7
|
from ..base.contexts import UnmarshalContext
|
9
8
|
from ..base.types import Marshaler
|
10
9
|
from ..base.types import MarshalerFactory
|
11
|
-
from ..base.types import MarshalerMaker
|
12
10
|
from ..base.types import Unmarshaler
|
13
11
|
from ..base.types import UnmarshalerFactory
|
14
|
-
from ..base.types import UnmarshalerMaker
|
15
|
-
|
16
|
-
|
17
|
-
R = ta.TypeVar('R')
|
18
|
-
C = ta.TypeVar('C')
|
19
12
|
|
20
13
|
|
21
14
|
##
|
22
15
|
|
23
16
|
|
24
17
|
@dc.dataclass(frozen=True)
|
25
|
-
class
|
26
|
-
m: ta.Mapping[rfl.Type,
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
return rty in self.m
|
36
|
-
|
37
|
-
def fn(self, ctx: C, rty: rfl.Type) -> R:
|
18
|
+
class TypeMapMarshalerFactory(MarshalerFactory):
|
19
|
+
m: ta.Mapping[rfl.Type, Marshaler | MarshalerFactory] = dc.xfield(
|
20
|
+
default_factory=dict,
|
21
|
+
coerce=lambda d: {
|
22
|
+
check.isinstance(k, rfl.TYPES): check.isinstance(v, (Marshaler, UnmarshalerFactory))
|
23
|
+
for k, v in d.items()
|
24
|
+
},
|
25
|
+
)
|
26
|
+
|
27
|
+
def make_marshaler(self, ctx: MarshalContext, rty: rfl.Type) -> ta.Callable[[], Marshaler] | None:
|
38
28
|
check.isinstance(rty, rfl.TYPES)
|
39
29
|
try:
|
40
|
-
|
30
|
+
m = self.m[rty]
|
41
31
|
except KeyError:
|
42
|
-
|
43
|
-
|
32
|
+
return None
|
44
33
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
MarshalerFactory,
|
52
|
-
):
|
53
|
-
@property
|
54
|
-
def make_marshaler(self) -> MarshalerMaker:
|
55
|
-
return self # noqa
|
34
|
+
if isinstance(m, Marshaler):
|
35
|
+
return lambda: m
|
36
|
+
elif isinstance(m, MarshalerFactory):
|
37
|
+
return m.make_marshaler(ctx, rty)
|
38
|
+
else:
|
39
|
+
raise TypeError(m)
|
56
40
|
|
57
41
|
|
58
42
|
@dc.dataclass(frozen=True)
|
59
|
-
class TypeMapUnmarshalerFactory(
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
43
|
+
class TypeMapUnmarshalerFactory(UnmarshalerFactory):
|
44
|
+
u: ta.Mapping[rfl.Type, Unmarshaler | UnmarshalerFactory] = dc.xfield(
|
45
|
+
default_factory=dict,
|
46
|
+
coerce=lambda d: {
|
47
|
+
check.isinstance(k, rfl.TYPES): check.isinstance(v, (Unmarshaler, UnmarshalerFactory))
|
48
|
+
for k, v in d.items()
|
49
|
+
},
|
50
|
+
)
|
51
|
+
|
52
|
+
def make_unmarshaler(self, ctx: UnmarshalContext, rty: rfl.Type) -> ta.Callable[[], Unmarshaler] | None:
|
53
|
+
check.isinstance(rty, rfl.TYPES)
|
54
|
+
try:
|
55
|
+
u = self.u[rty]
|
56
|
+
except KeyError:
|
57
|
+
return None
|
58
|
+
|
59
|
+
if isinstance(u, Unmarshaler):
|
60
|
+
return lambda: u
|
61
|
+
elif isinstance(u, UnmarshalerFactory):
|
62
|
+
return u.make_unmarshaler(ctx, rty)
|
63
|
+
else:
|
64
|
+
raise TypeError(u)
|