omlish 0.0.0.dev227__py3-none-any.whl → 0.0.0.dev228__py3-none-any.whl
Sign up to get free protection for your applications and to get access to all the features.
- omlish/__about__.py +3 -3
- omlish/diag/lsof.py +4 -5
- omlish/diag/ps.py +9 -0
- omlish/lite/timeouts.py +1 -1
- omlish/marshal/__init__.py +39 -24
- omlish/marshal/composite/__init__.py +0 -0
- omlish/marshal/{iterables.py → composite/iterables.py} +10 -10
- omlish/marshal/{literals.py → composite/literals.py} +9 -9
- omlish/marshal/{mappings.py → composite/mappings.py} +10 -10
- omlish/marshal/{maybes.py → composite/maybes.py} +11 -11
- omlish/marshal/{newtypes.py → composite/newtypes.py} +8 -8
- omlish/marshal/{optionals.py → composite/optionals.py} +9 -9
- omlish/marshal/objects/__init__.py +7 -0
- omlish/marshal/{dataclasses.py → objects/dataclasses.py} +24 -24
- omlish/marshal/{helpers.py → objects/helpers.py} +6 -3
- omlish/marshal/objects/marshal.py +108 -0
- omlish/marshal/objects/metadata.py +124 -0
- omlish/marshal/{namedtuples.py → objects/namedtuples.py} +16 -16
- omlish/marshal/objects/unmarshal.py +141 -0
- omlish/marshal/polymorphism/__init__.py +7 -0
- omlish/marshal/polymorphism/marshal.py +66 -0
- omlish/marshal/polymorphism/metadata.py +140 -0
- omlish/marshal/{unions.py → polymorphism/unions.py} +18 -18
- omlish/marshal/polymorphism/unmarshal.py +73 -0
- omlish/marshal/singular/__init__.py +0 -0
- omlish/marshal/{any.py → singular/any.py} +8 -8
- omlish/marshal/{base64.py → singular/base64.py} +9 -9
- omlish/marshal/{datetimes.py → singular/datetimes.py} +9 -9
- omlish/marshal/{enums.py → singular/enums.py} +9 -9
- omlish/marshal/{numbers.py → singular/numbers.py} +8 -8
- omlish/marshal/{primitives.py → singular/primitives.py} +8 -8
- omlish/marshal/{uuids.py → singular/uuids.py} +8 -8
- omlish/marshal/standard.py +32 -32
- omlish/os/death.py +70 -4
- omlish/os/fcntl.py +11 -12
- omlish/os/files.py +18 -3
- omlish/os/forkhooks.py +215 -0
- omlish/os/pidfiles/pinning.py +4 -0
- omlish/sockets/bind.py +4 -4
- {omlish-0.0.0.dev227.dist-info → omlish-0.0.0.dev228.dist-info}/METADATA +3 -3
- {omlish-0.0.0.dev227.dist-info → omlish-0.0.0.dev228.dist-info}/RECORD +45 -36
- omlish/marshal/objects.py +0 -317
- omlish/marshal/polymorphism.py +0 -267
- {omlish-0.0.0.dev227.dist-info → omlish-0.0.0.dev228.dist-info}/LICENSE +0 -0
- {omlish-0.0.0.dev227.dist-info → omlish-0.0.0.dev228.dist-info}/WHEEL +0 -0
- {omlish-0.0.0.dev227.dist-info → omlish-0.0.0.dev228.dist-info}/entry_points.txt +0 -0
- {omlish-0.0.0.dev227.dist-info → omlish-0.0.0.dev228.dist-info}/top_level.txt +0 -0
omlish/marshal/polymorphism.py
DELETED
@@ -1,267 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
TODO:
|
3
|
-
- auto-gen from __subclasses__ if abstract
|
4
|
-
- cfg: unless prefixed with _ or abstract
|
5
|
-
- iff Sealed
|
6
|
-
- auto-name
|
7
|
-
"""
|
8
|
-
import collections.abc
|
9
|
-
import dataclasses as dc
|
10
|
-
import typing as ta
|
11
|
-
|
12
|
-
from .. import check
|
13
|
-
from .. import lang
|
14
|
-
from .. import reflect as rfl
|
15
|
-
from .base import MarshalContext
|
16
|
-
from .base import Marshaler
|
17
|
-
from .base import MarshalerFactory
|
18
|
-
from .base import UnmarshalContext
|
19
|
-
from .base import Unmarshaler
|
20
|
-
from .base import UnmarshalerFactory
|
21
|
-
from .naming import Naming
|
22
|
-
from .naming import translate_name
|
23
|
-
from .registries import RegistryItem
|
24
|
-
from .values import Value
|
25
|
-
|
26
|
-
|
27
|
-
##
|
28
|
-
|
29
|
-
|
30
|
-
class TypeTagging(RegistryItem, lang.Abstract, lang.Sealed):
|
31
|
-
pass
|
32
|
-
|
33
|
-
|
34
|
-
class WrapperTypeTagging(TypeTagging, lang.Final):
|
35
|
-
pass
|
36
|
-
|
37
|
-
|
38
|
-
@dc.dataclass(frozen=True)
|
39
|
-
class FieldTypeTagging(TypeTagging, lang.Final):
|
40
|
-
field: str
|
41
|
-
|
42
|
-
|
43
|
-
##
|
44
|
-
|
45
|
-
|
46
|
-
@dc.dataclass(frozen=True)
|
47
|
-
class Impl:
|
48
|
-
ty: type
|
49
|
-
tag: str
|
50
|
-
alts: ta.AbstractSet[str] = frozenset()
|
51
|
-
|
52
|
-
|
53
|
-
class Impls(ta.Sequence[Impl]):
|
54
|
-
def __init__(
|
55
|
-
self,
|
56
|
-
lst: ta.Iterable[Impl],
|
57
|
-
) -> None:
|
58
|
-
super().__init__()
|
59
|
-
self._lst = list(lst)
|
60
|
-
|
61
|
-
by_ty: dict[type, Impl] = {}
|
62
|
-
by_tag: dict[str, Impl] = {}
|
63
|
-
for i in self._lst:
|
64
|
-
if i.ty in by_ty:
|
65
|
-
raise TypeError(i.ty)
|
66
|
-
if i.tag in by_tag:
|
67
|
-
raise NameError(i.tag)
|
68
|
-
for a in i.alts:
|
69
|
-
if a in by_tag:
|
70
|
-
raise NameError(a)
|
71
|
-
by_ty[i.ty] = i
|
72
|
-
by_tag[i.tag] = i
|
73
|
-
for a in i.alts:
|
74
|
-
by_tag[a] = i
|
75
|
-
self._by_ty = by_ty
|
76
|
-
self._by_tag = by_tag
|
77
|
-
|
78
|
-
def __iter__(self) -> ta.Iterator[Impl]:
|
79
|
-
return iter(self._lst)
|
80
|
-
|
81
|
-
def __len__(self) -> int:
|
82
|
-
return len(self._lst)
|
83
|
-
|
84
|
-
@ta.overload
|
85
|
-
def __getitem__(self, index: int) -> Impl: ...
|
86
|
-
|
87
|
-
@ta.overload
|
88
|
-
def __getitem__(self, index: slice) -> ta.Sequence[Impl]: ...
|
89
|
-
|
90
|
-
def __getitem__(self, index):
|
91
|
-
return self._lst[index]
|
92
|
-
|
93
|
-
@property
|
94
|
-
def by_ty(self) -> ta.Mapping[type, Impl]:
|
95
|
-
return self._by_ty
|
96
|
-
|
97
|
-
@property
|
98
|
-
def by_tag(self) -> ta.Mapping[str, Impl]:
|
99
|
-
return self._by_tag
|
100
|
-
|
101
|
-
|
102
|
-
class Polymorphism:
|
103
|
-
def __init__(
|
104
|
-
self,
|
105
|
-
ty: type,
|
106
|
-
impls: ta.Iterable[Impl],
|
107
|
-
) -> None:
|
108
|
-
super().__init__()
|
109
|
-
self._ty = ty
|
110
|
-
self._impls = Impls(impls)
|
111
|
-
|
112
|
-
for i in self._impls:
|
113
|
-
if not issubclass(i.ty, ty):
|
114
|
-
raise TypeError(i.ty, ty)
|
115
|
-
|
116
|
-
@property
|
117
|
-
def ty(self) -> type:
|
118
|
-
return self._ty
|
119
|
-
|
120
|
-
@property
|
121
|
-
def impls(self) -> Impls:
|
122
|
-
return self._impls
|
123
|
-
|
124
|
-
|
125
|
-
def polymorphism_from_subclasses(
|
126
|
-
ty: type,
|
127
|
-
*,
|
128
|
-
naming: Naming | None = None,
|
129
|
-
strip_suffix: bool = False,
|
130
|
-
) -> Polymorphism:
|
131
|
-
dct: dict[str, Impl] = {}
|
132
|
-
|
133
|
-
seen: set[type] = set()
|
134
|
-
todo: list[type] = [ty]
|
135
|
-
while todo:
|
136
|
-
cur = todo.pop()
|
137
|
-
seen.add(cur)
|
138
|
-
|
139
|
-
todo.extend(nxt for nxt in cur.__subclasses__() if nxt not in seen)
|
140
|
-
|
141
|
-
if lang.is_abstract_class(cur):
|
142
|
-
continue
|
143
|
-
|
144
|
-
name = cur.__name__
|
145
|
-
if strip_suffix:
|
146
|
-
name = lang.strip_suffix(name, ty.__name__)
|
147
|
-
if naming is not None:
|
148
|
-
name = translate_name(name, naming)
|
149
|
-
if name in dct:
|
150
|
-
raise KeyError(f'Duplicate name: {name}')
|
151
|
-
|
152
|
-
dct[name] = Impl(
|
153
|
-
cur,
|
154
|
-
name,
|
155
|
-
)
|
156
|
-
|
157
|
-
return Polymorphism(ty, dct.values())
|
158
|
-
|
159
|
-
|
160
|
-
##
|
161
|
-
|
162
|
-
|
163
|
-
@dc.dataclass(frozen=True)
|
164
|
-
class WrapperPolymorphismMarshaler(Marshaler):
|
165
|
-
m: ta.Mapping[type, tuple[str, Marshaler]]
|
166
|
-
|
167
|
-
def marshal(self, ctx: MarshalContext, o: ta.Any | None) -> Value:
|
168
|
-
tag, m = self.m[type(o)]
|
169
|
-
return {tag: m.marshal(ctx, o)}
|
170
|
-
|
171
|
-
|
172
|
-
@dc.dataclass(frozen=True)
|
173
|
-
class FieldPolymorphismMarshaler(Marshaler):
|
174
|
-
m: ta.Mapping[type, tuple[str, Marshaler]]
|
175
|
-
tf: str
|
176
|
-
|
177
|
-
def marshal(self, ctx: MarshalContext, o: ta.Any | None) -> Value:
|
178
|
-
tag, m = self.m[type(o)]
|
179
|
-
return {self.tf: tag, **m.marshal(ctx, o)} # type: ignore
|
180
|
-
|
181
|
-
|
182
|
-
def make_polymorphism_marshaler(
|
183
|
-
impls: Impls,
|
184
|
-
tt: TypeTagging,
|
185
|
-
ctx: MarshalContext,
|
186
|
-
) -> Marshaler:
|
187
|
-
m = {
|
188
|
-
i.ty: (i.tag, ctx.make(i.ty))
|
189
|
-
for i in impls
|
190
|
-
}
|
191
|
-
if isinstance(tt, WrapperTypeTagging):
|
192
|
-
return WrapperPolymorphismMarshaler(m)
|
193
|
-
elif isinstance(tt, FieldTypeTagging):
|
194
|
-
return FieldPolymorphismMarshaler(m, tt.field)
|
195
|
-
else:
|
196
|
-
raise TypeError(tt)
|
197
|
-
|
198
|
-
|
199
|
-
@dc.dataclass(frozen=True)
|
200
|
-
class PolymorphismMarshalerFactory(MarshalerFactory):
|
201
|
-
p: Polymorphism
|
202
|
-
tt: TypeTagging = WrapperTypeTagging()
|
203
|
-
|
204
|
-
def guard(self, ctx: MarshalContext, rty: rfl.Type) -> bool:
|
205
|
-
return rty is self.p.ty
|
206
|
-
|
207
|
-
def fn(self, ctx: MarshalContext, rty: rfl.Type) -> Marshaler:
|
208
|
-
check.is_(rty, self.p.ty)
|
209
|
-
return make_polymorphism_marshaler(self.p.impls, self.tt, ctx)
|
210
|
-
|
211
|
-
|
212
|
-
##
|
213
|
-
|
214
|
-
|
215
|
-
@dc.dataclass(frozen=True)
|
216
|
-
class WrapperPolymorphismUnmarshaler(Unmarshaler):
|
217
|
-
m: ta.Mapping[str, Unmarshaler]
|
218
|
-
|
219
|
-
def unmarshal(self, ctx: UnmarshalContext, v: Value) -> ta.Any | None:
|
220
|
-
ma = check.isinstance(v, collections.abc.Mapping)
|
221
|
-
[(tag, iv)] = ma.items()
|
222
|
-
u = self.m[tag]
|
223
|
-
return u.unmarshal(ctx, iv)
|
224
|
-
|
225
|
-
|
226
|
-
@dc.dataclass(frozen=True)
|
227
|
-
class FieldPolymorphismUnmarshaler(Unmarshaler):
|
228
|
-
m: ta.Mapping[str, Unmarshaler]
|
229
|
-
tf: str
|
230
|
-
|
231
|
-
def unmarshal(self, ctx: UnmarshalContext, v: Value) -> ta.Any | None:
|
232
|
-
ma = dict(check.isinstance(v, collections.abc.Mapping))
|
233
|
-
tag = ma.pop(self.tf)
|
234
|
-
u = self.m[tag]
|
235
|
-
return u.unmarshal(ctx, ma)
|
236
|
-
|
237
|
-
|
238
|
-
def make_polymorphism_unmarshaler(
|
239
|
-
impls: Impls,
|
240
|
-
tt: TypeTagging,
|
241
|
-
ctx: UnmarshalContext,
|
242
|
-
) -> Unmarshaler:
|
243
|
-
m = {
|
244
|
-
t: u
|
245
|
-
for i in impls
|
246
|
-
for u in [ctx.make(i.ty)]
|
247
|
-
for t in [i.tag, *i.alts]
|
248
|
-
}
|
249
|
-
if isinstance(tt, WrapperTypeTagging):
|
250
|
-
return WrapperPolymorphismUnmarshaler(m)
|
251
|
-
elif isinstance(tt, FieldTypeTagging):
|
252
|
-
return FieldPolymorphismUnmarshaler(m, tt.field)
|
253
|
-
else:
|
254
|
-
raise TypeError(tt)
|
255
|
-
|
256
|
-
|
257
|
-
@dc.dataclass(frozen=True)
|
258
|
-
class PolymorphismUnmarshalerFactory(UnmarshalerFactory):
|
259
|
-
p: Polymorphism
|
260
|
-
tt: TypeTagging = WrapperTypeTagging()
|
261
|
-
|
262
|
-
def guard(self, ctx: UnmarshalContext, rty: rfl.Type) -> bool:
|
263
|
-
return rty is self.p.ty
|
264
|
-
|
265
|
-
def fn(self, ctx: UnmarshalContext, rty: rfl.Type) -> Unmarshaler:
|
266
|
-
check.is_(rty, self.p.ty)
|
267
|
-
return make_polymorphism_unmarshaler(self.p.impls, self.tt, ctx)
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|