omlish 0.0.0.dev5__py3-none-any.whl → 0.0.0.dev7__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.
Potentially problematic release.
This version of omlish might be problematic. Click here for more details.
- omlish/__about__.py +109 -5
- omlish/__init__.py +0 -8
- omlish/asyncs/__init__.py +9 -9
- omlish/asyncs/anyio.py +123 -19
- omlish/asyncs/asyncio.py +23 -0
- omlish/asyncs/asyncs.py +9 -6
- omlish/asyncs/bridge.py +316 -0
- omlish/asyncs/trio_asyncio.py +7 -3
- omlish/bootstrap.py +737 -0
- omlish/check.py +1 -1
- omlish/collections/__init__.py +5 -0
- omlish/collections/exceptions.py +2 -0
- omlish/collections/identity.py +7 -0
- omlish/collections/utils.py +38 -9
- omlish/configs/strings.py +96 -0
- omlish/dataclasses/__init__.py +16 -0
- omlish/dataclasses/impl/copy.py +30 -0
- omlish/dataclasses/impl/descriptors.py +95 -0
- omlish/dataclasses/impl/exceptions.py +6 -0
- omlish/dataclasses/impl/fields.py +24 -25
- omlish/dataclasses/impl/init.py +4 -2
- omlish/dataclasses/impl/main.py +2 -0
- omlish/dataclasses/impl/reflect.py +1 -1
- omlish/dataclasses/utils.py +67 -0
- omlish/{lang/datetimes.py → datetimes.py} +8 -4
- omlish/diag/__init__.py +4 -0
- omlish/diag/procfs.py +2 -2
- omlish/{testing → diag}/pydevd.py +35 -0
- omlish/diag/threads.py +131 -48
- omlish/dispatch/_dispatch2.py +65 -0
- omlish/dispatch/_dispatch3.py +104 -0
- omlish/docker.py +16 -1
- omlish/fnpairs.py +11 -4
- omlish/formats/__init__.py +0 -0
- omlish/{configs → formats}/dotenv.py +15 -24
- omlish/{json.py → formats/json.py} +2 -1
- omlish/formats/yaml.py +223 -0
- omlish/graphs/trees.py +1 -1
- omlish/http/asgi.py +2 -1
- omlish/http/collections.py +15 -0
- omlish/http/consts.py +22 -1
- omlish/http/sessions.py +10 -3
- omlish/inject/__init__.py +49 -17
- omlish/inject/binder.py +185 -5
- omlish/inject/bindings.py +3 -36
- omlish/inject/eagers.py +2 -8
- omlish/inject/elements.py +31 -10
- omlish/inject/exceptions.py +1 -1
- omlish/inject/impl/elements.py +37 -12
- omlish/inject/impl/injector.py +72 -25
- omlish/inject/impl/inspect.py +33 -5
- omlish/inject/impl/origins.py +77 -0
- omlish/inject/impl/{private.py → privates.py} +2 -2
- omlish/inject/impl/scopes.py +6 -2
- omlish/inject/injector.py +8 -4
- omlish/inject/inspect.py +18 -0
- omlish/inject/keys.py +8 -14
- omlish/inject/listeners.py +26 -0
- omlish/inject/managed.py +76 -10
- omlish/inject/multis.py +68 -18
- omlish/inject/origins.py +30 -0
- omlish/inject/overrides.py +5 -4
- omlish/inject/{private.py → privates.py} +6 -10
- omlish/inject/providers.py +12 -85
- omlish/inject/scopes.py +13 -6
- omlish/inject/types.py +3 -1
- omlish/inject/utils.py +18 -0
- omlish/iterators.py +69 -2
- omlish/lang/__init__.py +24 -9
- omlish/lang/cached.py +2 -2
- omlish/lang/classes/restrict.py +12 -1
- omlish/lang/classes/simple.py +18 -8
- omlish/lang/contextmanagers.py +13 -4
- omlish/lang/descriptors.py +132 -1
- omlish/lang/functions.py +8 -28
- omlish/lang/imports.py +67 -0
- omlish/lang/iterables.py +60 -1
- omlish/lang/maybes.py +3 -0
- omlish/lang/objects.py +38 -0
- omlish/lang/strings.py +25 -0
- omlish/lang/sys.py +9 -0
- omlish/lang/typing.py +42 -0
- omlish/lifecycles/__init__.py +34 -0
- omlish/lifecycles/abstract.py +43 -0
- omlish/lifecycles/base.py +51 -0
- omlish/lifecycles/contextmanagers.py +74 -0
- omlish/lifecycles/controller.py +116 -0
- omlish/lifecycles/manager.py +161 -0
- omlish/lifecycles/states.py +43 -0
- omlish/lifecycles/transitions.py +64 -0
- omlish/lite/__init__.py +1 -0
- omlish/lite/cached.py +18 -0
- omlish/lite/check.py +29 -0
- omlish/lite/contextmanagers.py +18 -0
- omlish/lite/json.py +30 -0
- omlish/lite/logs.py +52 -0
- omlish/lite/marshal.py +316 -0
- omlish/lite/reflect.py +49 -0
- omlish/lite/runtime.py +18 -0
- omlish/lite/secrets.py +19 -0
- omlish/lite/strings.py +25 -0
- omlish/lite/subprocesses.py +112 -0
- omlish/logs/configs.py +15 -2
- omlish/logs/formatters.py +7 -2
- omlish/marshal/__init__.py +32 -0
- omlish/marshal/any.py +5 -5
- omlish/marshal/base.py +27 -11
- omlish/marshal/base64.py +24 -9
- omlish/marshal/dataclasses.py +34 -28
- omlish/marshal/datetimes.py +74 -18
- omlish/marshal/enums.py +14 -8
- omlish/marshal/exceptions.py +11 -1
- omlish/marshal/factories.py +59 -74
- omlish/marshal/forbidden.py +35 -0
- omlish/marshal/global_.py +11 -4
- omlish/marshal/iterables.py +21 -24
- omlish/marshal/mappings.py +23 -26
- omlish/marshal/naming.py +4 -0
- omlish/marshal/numbers.py +51 -0
- omlish/marshal/objects.py +1 -0
- omlish/marshal/optionals.py +11 -12
- omlish/marshal/polymorphism.py +86 -21
- omlish/marshal/primitives.py +4 -5
- omlish/marshal/standard.py +13 -8
- omlish/marshal/uuids.py +4 -5
- omlish/matchfns.py +218 -0
- omlish/os.py +64 -0
- omlish/reflect/__init__.py +39 -0
- omlish/reflect/isinstance.py +38 -0
- omlish/reflect/ops.py +84 -0
- omlish/reflect/subst.py +110 -0
- omlish/reflect/types.py +275 -0
- omlish/secrets/__init__.py +23 -0
- omlish/secrets/crypto.py +132 -0
- omlish/secrets/marshal.py +70 -0
- omlish/secrets/openssl.py +207 -0
- omlish/secrets/passwords.py +120 -0
- omlish/secrets/secrets.py +299 -0
- omlish/secrets/subprocesses.py +42 -0
- omlish/sql/dbs.py +7 -6
- omlish/sql/duckdb.py +136 -0
- omlish/sql/exprs.py +12 -0
- omlish/sql/secrets.py +10 -0
- omlish/sql/sqlean.py +17 -0
- omlish/term.py +2 -2
- omlish/testing/pytest/__init__.py +3 -2
- omlish/testing/pytest/inject/harness.py +3 -3
- omlish/testing/pytest/marks.py +4 -7
- omlish/testing/pytest/plugins/__init__.py +1 -0
- omlish/testing/pytest/plugins/asyncs.py +136 -0
- omlish/testing/pytest/plugins/pydevd.py +1 -1
- omlish/testing/pytest/plugins/switches.py +54 -19
- omlish/text/glyphsplit.py +97 -0
- omlish-0.0.0.dev7.dist-info/METADATA +50 -0
- omlish-0.0.0.dev7.dist-info/RECORD +268 -0
- {omlish-0.0.0.dev5.dist-info → omlish-0.0.0.dev7.dist-info}/WHEEL +1 -1
- omlish/reflect.py +0 -355
- omlish-0.0.0.dev5.dist-info/METADATA +0 -34
- omlish-0.0.0.dev5.dist-info/RECORD +0 -212
- /omlish/{asyncs/futures.py → concurrent.py} +0 -0
- /omlish/{configs → formats}/props.py +0 -0
- {omlish-0.0.0.dev5.dist-info → omlish-0.0.0.dev7.dist-info}/LICENSE +0 -0
- {omlish-0.0.0.dev5.dist-info → omlish-0.0.0.dev7.dist-info}/top_level.txt +0 -0
omlish/lite/marshal.py
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
"""
|
|
2
|
+
TODO:
|
|
3
|
+
- pickle stdlib objs? have to pin to 3.8 pickle protocol, will be cross-version
|
|
4
|
+
"""
|
|
5
|
+
# ruff: noqa: UP006 UP007
|
|
6
|
+
import abc
|
|
7
|
+
import base64
|
|
8
|
+
import collections.abc
|
|
9
|
+
import dataclasses as dc # noqa
|
|
10
|
+
import datetime
|
|
11
|
+
import decimal
|
|
12
|
+
import enum
|
|
13
|
+
import fractions
|
|
14
|
+
import typing as ta
|
|
15
|
+
import uuid
|
|
16
|
+
import weakref # noqa
|
|
17
|
+
|
|
18
|
+
from .check import check_isinstance
|
|
19
|
+
from .check import check_not_none
|
|
20
|
+
from .reflect import deep_subclasses
|
|
21
|
+
from .reflect import get_optional_alias_arg
|
|
22
|
+
from .reflect import is_generic_alias
|
|
23
|
+
from .reflect import is_union_alias
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
T = ta.TypeVar('T')
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
##
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class ObjMarshaler(abc.ABC):
|
|
33
|
+
@abc.abstractmethod
|
|
34
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
35
|
+
raise NotImplementedError
|
|
36
|
+
|
|
37
|
+
@abc.abstractmethod
|
|
38
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
39
|
+
raise NotImplementedError
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class NopObjMarshaler(ObjMarshaler):
|
|
43
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
44
|
+
return o
|
|
45
|
+
|
|
46
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
47
|
+
return o
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dc.dataclass()
|
|
51
|
+
class ProxyObjMarshaler(ObjMarshaler):
|
|
52
|
+
m: ta.Optional[ObjMarshaler] = None
|
|
53
|
+
|
|
54
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
55
|
+
return check_not_none(self.m).marshal(o)
|
|
56
|
+
|
|
57
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
58
|
+
return check_not_none(self.m).unmarshal(o)
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
@dc.dataclass(frozen=True)
|
|
62
|
+
class CastObjMarshaler(ObjMarshaler):
|
|
63
|
+
ty: type
|
|
64
|
+
|
|
65
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
66
|
+
return o
|
|
67
|
+
|
|
68
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
69
|
+
return self.ty(o)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class DynamicObjMarshaler(ObjMarshaler):
|
|
73
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
74
|
+
return marshal_obj(o)
|
|
75
|
+
|
|
76
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
77
|
+
return o
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@dc.dataclass(frozen=True)
|
|
81
|
+
class Base64ObjMarshaler(ObjMarshaler):
|
|
82
|
+
ty: type
|
|
83
|
+
|
|
84
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
85
|
+
return base64.b64encode(o).decode('ascii')
|
|
86
|
+
|
|
87
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
88
|
+
return self.ty(base64.b64decode(o))
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
@dc.dataclass(frozen=True)
|
|
92
|
+
class EnumObjMarshaler(ObjMarshaler):
|
|
93
|
+
ty: type
|
|
94
|
+
|
|
95
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
96
|
+
return o.name
|
|
97
|
+
|
|
98
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
99
|
+
return self.ty.__members__[o] # type: ignore
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
@dc.dataclass(frozen=True)
|
|
103
|
+
class OptionalObjMarshaler(ObjMarshaler):
|
|
104
|
+
item: ObjMarshaler
|
|
105
|
+
|
|
106
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
107
|
+
if o is None:
|
|
108
|
+
return None
|
|
109
|
+
return self.item.marshal(o)
|
|
110
|
+
|
|
111
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
112
|
+
if o is None:
|
|
113
|
+
return None
|
|
114
|
+
return self.item.unmarshal(o)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@dc.dataclass(frozen=True)
|
|
118
|
+
class MappingObjMarshaler(ObjMarshaler):
|
|
119
|
+
ty: type
|
|
120
|
+
km: ObjMarshaler
|
|
121
|
+
vm: ObjMarshaler
|
|
122
|
+
|
|
123
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
124
|
+
return {self.km.marshal(k): self.vm.marshal(v) for k, v in o.items()}
|
|
125
|
+
|
|
126
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
127
|
+
return self.ty((self.km.unmarshal(k), self.vm.unmarshal(v)) for k, v in o.items())
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
@dc.dataclass(frozen=True)
|
|
131
|
+
class IterableObjMarshaler(ObjMarshaler):
|
|
132
|
+
ty: type
|
|
133
|
+
item: ObjMarshaler
|
|
134
|
+
|
|
135
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
136
|
+
return [self.item.marshal(e) for e in o]
|
|
137
|
+
|
|
138
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
139
|
+
return self.ty(self.item.unmarshal(e) for e in o)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@dc.dataclass(frozen=True)
|
|
143
|
+
class DataclassObjMarshaler(ObjMarshaler):
|
|
144
|
+
ty: type
|
|
145
|
+
fs: ta.Mapping[str, ObjMarshaler]
|
|
146
|
+
|
|
147
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
148
|
+
return {k: m.marshal(getattr(o, k)) for k, m in self.fs.items()}
|
|
149
|
+
|
|
150
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
151
|
+
return self.ty(**{k: self.fs[k].unmarshal(v) for k, v in o.items()})
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
@dc.dataclass(frozen=True)
|
|
155
|
+
class PolymorphicObjMarshaler(ObjMarshaler):
|
|
156
|
+
class Impl(ta.NamedTuple):
|
|
157
|
+
ty: type
|
|
158
|
+
tag: str
|
|
159
|
+
m: ObjMarshaler
|
|
160
|
+
|
|
161
|
+
impls_by_ty: ta.Mapping[type, Impl]
|
|
162
|
+
impls_by_tag: ta.Mapping[str, Impl]
|
|
163
|
+
|
|
164
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
165
|
+
impl = self.impls_by_ty[type(o)]
|
|
166
|
+
return {impl.tag: impl.m.marshal(o)}
|
|
167
|
+
|
|
168
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
169
|
+
[(t, v)] = o.items()
|
|
170
|
+
impl = self.impls_by_tag[t]
|
|
171
|
+
return impl.m.unmarshal(v)
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
@dc.dataclass(frozen=True)
|
|
175
|
+
class DatetimeObjMarshaler(ObjMarshaler):
|
|
176
|
+
ty: type
|
|
177
|
+
|
|
178
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
179
|
+
return o.isoformat()
|
|
180
|
+
|
|
181
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
182
|
+
return self.ty.fromisoformat(o) # type: ignore
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
class DecimalObjMarshaler(ObjMarshaler):
|
|
186
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
187
|
+
return str(check_isinstance(o, decimal.Decimal))
|
|
188
|
+
|
|
189
|
+
def unmarshal(self, v: ta.Any) -> ta.Any:
|
|
190
|
+
return decimal.Decimal(check_isinstance(v, str))
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
class FractionObjMarshaler(ObjMarshaler):
|
|
194
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
195
|
+
fr = check_isinstance(o, fractions.Fraction)
|
|
196
|
+
return [fr.numerator, fr.denominator]
|
|
197
|
+
|
|
198
|
+
def unmarshal(self, v: ta.Any) -> ta.Any:
|
|
199
|
+
num, denom = check_isinstance(v, list)
|
|
200
|
+
return fractions.Fraction(num, denom)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class UuidObjMarshaler(ObjMarshaler):
|
|
204
|
+
def marshal(self, o: ta.Any) -> ta.Any:
|
|
205
|
+
return str(o)
|
|
206
|
+
|
|
207
|
+
def unmarshal(self, o: ta.Any) -> ta.Any:
|
|
208
|
+
return uuid.UUID(o)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
_OBJ_MARSHALERS: ta.Dict[ta.Any, ObjMarshaler] = {
|
|
212
|
+
**{t: NopObjMarshaler() for t in (type(None),)},
|
|
213
|
+
**{t: CastObjMarshaler(t) for t in (int, float, str, bool)},
|
|
214
|
+
**{t: Base64ObjMarshaler(t) for t in (bytes, bytearray)},
|
|
215
|
+
**{t: IterableObjMarshaler(t, DynamicObjMarshaler()) for t in (list, tuple, set, frozenset)},
|
|
216
|
+
**{t: MappingObjMarshaler(t, DynamicObjMarshaler(), DynamicObjMarshaler()) for t in (dict,)},
|
|
217
|
+
|
|
218
|
+
ta.Any: DynamicObjMarshaler(),
|
|
219
|
+
|
|
220
|
+
**{t: DatetimeObjMarshaler(t) for t in (datetime.date, datetime.time, datetime.datetime)},
|
|
221
|
+
decimal.Decimal: DecimalObjMarshaler(),
|
|
222
|
+
fractions.Fraction: FractionObjMarshaler(),
|
|
223
|
+
uuid.UUID: UuidObjMarshaler(),
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
_OBJ_MARSHALER_GENERIC_MAPPING_TYPES: ta.Dict[ta.Any, type] = {
|
|
227
|
+
**{t: t for t in (dict,)},
|
|
228
|
+
**{t: dict for t in (collections.abc.Mapping, collections.abc.MutableMapping)},
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
_OBJ_MARSHALER_GENERIC_ITERABLE_TYPES: ta.Dict[ta.Any, type] = {
|
|
232
|
+
**{t: t for t in (list, tuple, set, frozenset)},
|
|
233
|
+
**{t: frozenset for t in (collections.abc.Set, collections.abc.MutableSet)},
|
|
234
|
+
**{t: tuple for t in (collections.abc.Sequence, collections.abc.MutableSequence)},
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def register_opj_marshaler(ty: ta.Any, m: ObjMarshaler) -> None:
|
|
239
|
+
if ty in _OBJ_MARSHALERS:
|
|
240
|
+
raise KeyError(ty)
|
|
241
|
+
_OBJ_MARSHALERS[ty] = m
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _make_obj_marshaler(ty: ta.Any) -> ObjMarshaler:
|
|
245
|
+
if isinstance(ty, type) and abc.ABC in ty.__bases__:
|
|
246
|
+
impls = [ # type: ignore
|
|
247
|
+
PolymorphicObjMarshaler.Impl(
|
|
248
|
+
ity,
|
|
249
|
+
ity.__qualname__,
|
|
250
|
+
get_obj_marshaler(ity),
|
|
251
|
+
)
|
|
252
|
+
for ity in deep_subclasses(ty)
|
|
253
|
+
if abc.ABC not in ity.__bases__
|
|
254
|
+
]
|
|
255
|
+
return PolymorphicObjMarshaler(
|
|
256
|
+
{i.ty: i for i in impls},
|
|
257
|
+
{i.tag: i for i in impls},
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
if isinstance(ty, type) and issubclass(ty, enum.Enum):
|
|
261
|
+
return EnumObjMarshaler(ty)
|
|
262
|
+
|
|
263
|
+
if dc.is_dataclass(ty):
|
|
264
|
+
return DataclassObjMarshaler(
|
|
265
|
+
ty,
|
|
266
|
+
{f.name: get_obj_marshaler(f.type) for f in dc.fields(ty)},
|
|
267
|
+
)
|
|
268
|
+
|
|
269
|
+
if is_generic_alias(ty):
|
|
270
|
+
try:
|
|
271
|
+
mt = _OBJ_MARSHALER_GENERIC_MAPPING_TYPES[ta.get_origin(ty)]
|
|
272
|
+
except KeyError:
|
|
273
|
+
pass
|
|
274
|
+
else:
|
|
275
|
+
k, v = ta.get_args(ty)
|
|
276
|
+
return MappingObjMarshaler(mt, get_obj_marshaler(k), get_obj_marshaler(v))
|
|
277
|
+
|
|
278
|
+
try:
|
|
279
|
+
st = _OBJ_MARSHALER_GENERIC_ITERABLE_TYPES[ta.get_origin(ty)]
|
|
280
|
+
except KeyError:
|
|
281
|
+
pass
|
|
282
|
+
else:
|
|
283
|
+
[e] = ta.get_args(ty)
|
|
284
|
+
return IterableObjMarshaler(st, get_obj_marshaler(e))
|
|
285
|
+
|
|
286
|
+
if is_union_alias(ty):
|
|
287
|
+
return OptionalObjMarshaler(get_obj_marshaler(get_optional_alias_arg(ty)))
|
|
288
|
+
|
|
289
|
+
raise TypeError(ty)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def get_obj_marshaler(ty: ta.Any) -> ObjMarshaler:
|
|
293
|
+
try:
|
|
294
|
+
return _OBJ_MARSHALERS[ty]
|
|
295
|
+
except KeyError:
|
|
296
|
+
pass
|
|
297
|
+
|
|
298
|
+
p = ProxyObjMarshaler()
|
|
299
|
+
_OBJ_MARSHALERS[ty] = p
|
|
300
|
+
try:
|
|
301
|
+
m = _make_obj_marshaler(ty)
|
|
302
|
+
except Exception:
|
|
303
|
+
del _OBJ_MARSHALERS[ty]
|
|
304
|
+
raise
|
|
305
|
+
else:
|
|
306
|
+
p.m = m
|
|
307
|
+
_OBJ_MARSHALERS[ty] = m
|
|
308
|
+
return m
|
|
309
|
+
|
|
310
|
+
|
|
311
|
+
def marshal_obj(o: ta.Any, ty: ta.Any = None) -> ta.Any:
|
|
312
|
+
return get_obj_marshaler(ty if ty is not None else type(o)).marshal(o)
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def unmarshal_obj(o: ta.Any, ty: ta.Union[ta.Type[T], ta.Any]) -> T:
|
|
316
|
+
return get_obj_marshaler(ty).unmarshal(o)
|
omlish/lite/reflect.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# ruff: noqa: UP006
|
|
2
|
+
import functools
|
|
3
|
+
import typing as ta
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
T = ta.TypeVar('T')
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
_GENERIC_ALIAS_TYPES = (
|
|
10
|
+
ta._GenericAlias, # type: ignore # noqa
|
|
11
|
+
*([ta._SpecialGenericAlias] if hasattr(ta, '_SpecialGenericAlias') else []), # noqa
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def is_generic_alias(obj, *, origin: ta.Any = None) -> bool:
|
|
16
|
+
return (
|
|
17
|
+
isinstance(obj, _GENERIC_ALIAS_TYPES) and
|
|
18
|
+
(origin is None or ta.get_origin(obj) is origin)
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
is_union_alias = functools.partial(is_generic_alias, origin=ta.Union)
|
|
23
|
+
is_callable_alias = functools.partial(is_generic_alias, origin=ta.Callable)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def is_optional_alias(spec: ta.Any) -> bool:
|
|
27
|
+
return (
|
|
28
|
+
isinstance(spec, _GENERIC_ALIAS_TYPES) and # noqa
|
|
29
|
+
ta.get_origin(spec) is ta.Union and
|
|
30
|
+
len(ta.get_args(spec)) == 2 and
|
|
31
|
+
any(a in (None, type(None)) for a in ta.get_args(spec))
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def get_optional_alias_arg(spec: ta.Any) -> ta.Any:
|
|
36
|
+
[it] = [it for it in ta.get_args(spec) if it not in (None, type(None))]
|
|
37
|
+
return it
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def deep_subclasses(cls: ta.Type[T]) -> ta.Iterator[ta.Type[T]]:
|
|
41
|
+
seen = set()
|
|
42
|
+
todo = list(reversed(cls.__subclasses__()))
|
|
43
|
+
while todo:
|
|
44
|
+
cur = todo.pop()
|
|
45
|
+
if cur in seen:
|
|
46
|
+
continue
|
|
47
|
+
seen.add(cur)
|
|
48
|
+
yield cur
|
|
49
|
+
todo.extend(reversed(cur.__subclasses__()))
|
omlish/lite/runtime.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
import sys
|
|
3
|
+
|
|
4
|
+
from .cached import cached_nullary
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@cached_nullary
|
|
8
|
+
def is_debugger_attached() -> bool:
|
|
9
|
+
return any(frame[1].endswith('pydevd.py') for frame in inspect.stack())
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
REQUIRED_PYTHON_VERSION = (3, 8)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def check_runtime_version() -> None:
|
|
16
|
+
if sys.version_info < REQUIRED_PYTHON_VERSION:
|
|
17
|
+
raise OSError(
|
|
18
|
+
f'Requires python {REQUIRED_PYTHON_VERSION}, got {sys.version_info} from {sys.executable}') # noqa
|
omlish/lite/secrets.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import typing as ta
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Secret:
|
|
5
|
+
_VALUE_ATTR = '__secret_value__'
|
|
6
|
+
|
|
7
|
+
def __init__(self, *, key: str | None, value: str) -> None:
|
|
8
|
+
super().__init__()
|
|
9
|
+
self._key = key
|
|
10
|
+
setattr(self, self._VALUE_ATTR, lambda: value)
|
|
11
|
+
|
|
12
|
+
def __repr__(self) -> str:
|
|
13
|
+
return f'Secret<{self._key or ""}>'
|
|
14
|
+
|
|
15
|
+
def __str__(self) -> ta.NoReturn:
|
|
16
|
+
raise TypeError
|
|
17
|
+
|
|
18
|
+
def reveal(self) -> str:
|
|
19
|
+
return getattr(self, self._VALUE_ATTR)()
|
omlish/lite/strings.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
def camel_case(name: str) -> str:
|
|
2
|
+
return ''.join(map(str.capitalize, name.split('_'))) # noqa
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def snake_case(name: str) -> str:
|
|
6
|
+
uppers: list[int | None] = [i for i, c in enumerate(name) if c.isupper()]
|
|
7
|
+
return '_'.join([name[l:r].lower() for l, r in zip([None, *uppers], [*uppers, None])]).strip('_')
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def is_dunder(name: str) -> bool:
|
|
11
|
+
return (
|
|
12
|
+
name[:2] == name[-2:] == '__' and
|
|
13
|
+
name[2:3] != '_' and
|
|
14
|
+
name[-3:-2] != '_' and
|
|
15
|
+
len(name) > 4
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def is_sunder(name: str) -> bool:
|
|
20
|
+
return (
|
|
21
|
+
name[0] == name[-1] == '_' and
|
|
22
|
+
name[1:2] != '_' and
|
|
23
|
+
name[-2:-1] != '_' and
|
|
24
|
+
len(name) > 2
|
|
25
|
+
)
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# ruff: noqa: UP006 UP007
|
|
2
|
+
import logging
|
|
3
|
+
import os
|
|
4
|
+
import shlex
|
|
5
|
+
import subprocess
|
|
6
|
+
import sys
|
|
7
|
+
import typing as ta
|
|
8
|
+
|
|
9
|
+
from .logs import log
|
|
10
|
+
from .runtime import is_debugger_attached
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
##
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
_SUBPROCESS_SHELL_WRAP_EXECS = False
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def subprocess_shell_wrap_exec(*args: str) -> ta.Tuple[str, ...]:
|
|
20
|
+
return ('sh', '-c', ' '.join(map(shlex.quote, args)))
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def subprocess_maybe_shell_wrap_exec(*args: str) -> ta.Tuple[str, ...]:
|
|
24
|
+
if _SUBPROCESS_SHELL_WRAP_EXECS or is_debugger_attached():
|
|
25
|
+
return subprocess_shell_wrap_exec(*args)
|
|
26
|
+
else:
|
|
27
|
+
return args
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _prepare_subprocess_invocation(
|
|
31
|
+
*args: str,
|
|
32
|
+
env: ta.Optional[ta.Mapping[str, ta.Any]] = None,
|
|
33
|
+
extra_env: ta.Optional[ta.Mapping[str, ta.Any]] = None,
|
|
34
|
+
quiet: bool = False,
|
|
35
|
+
shell: bool = False,
|
|
36
|
+
**kwargs: ta.Any,
|
|
37
|
+
) -> ta.Tuple[ta.Tuple[ta.Any, ...], ta.Dict[str, ta.Any]]:
|
|
38
|
+
log.debug(args)
|
|
39
|
+
if extra_env:
|
|
40
|
+
log.debug(extra_env)
|
|
41
|
+
|
|
42
|
+
if extra_env:
|
|
43
|
+
env = {**(env if env is not None else os.environ), **extra_env}
|
|
44
|
+
|
|
45
|
+
if quiet and 'stderr' not in kwargs:
|
|
46
|
+
if not log.isEnabledFor(logging.DEBUG):
|
|
47
|
+
kwargs['stderr'] = subprocess.DEVNULL
|
|
48
|
+
|
|
49
|
+
if not shell:
|
|
50
|
+
args = subprocess_maybe_shell_wrap_exec(*args)
|
|
51
|
+
|
|
52
|
+
return args, dict(
|
|
53
|
+
env=env,
|
|
54
|
+
shell=shell,
|
|
55
|
+
**kwargs,
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def subprocess_check_call(*args: str, stdout=sys.stderr, **kwargs: ta.Any) -> None:
|
|
60
|
+
args, kwargs = _prepare_subprocess_invocation(*args, stdout=stdout, **kwargs)
|
|
61
|
+
return subprocess.check_call(args, **kwargs) # type: ignore
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def subprocess_check_output(*args: str, **kwargs: ta.Any) -> bytes:
|
|
65
|
+
args, kwargs = _prepare_subprocess_invocation(*args, **kwargs)
|
|
66
|
+
return subprocess.check_output(args, **kwargs)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def subprocess_check_output_str(*args: str, **kwargs: ta.Any) -> str:
|
|
70
|
+
return subprocess_check_output(*args, **kwargs).decode().strip()
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
##
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
DEFAULT_SUBPROCESS_TRY_EXCEPTIONS: ta.Tuple[ta.Type[Exception], ...] = (
|
|
77
|
+
FileNotFoundError,
|
|
78
|
+
subprocess.CalledProcessError,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def subprocess_try_call(
|
|
83
|
+
*args: str,
|
|
84
|
+
try_exceptions: ta.Tuple[ta.Type[Exception], ...] = DEFAULT_SUBPROCESS_TRY_EXCEPTIONS,
|
|
85
|
+
**kwargs: ta.Any,
|
|
86
|
+
) -> bool:
|
|
87
|
+
try:
|
|
88
|
+
subprocess_check_call(*args, **kwargs)
|
|
89
|
+
except try_exceptions as e: # noqa
|
|
90
|
+
if log.isEnabledFor(logging.DEBUG):
|
|
91
|
+
log.exception('command failed')
|
|
92
|
+
return False
|
|
93
|
+
else:
|
|
94
|
+
return True
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def subprocess_try_output(
|
|
98
|
+
*args: str,
|
|
99
|
+
try_exceptions: ta.Tuple[ta.Type[Exception], ...] = DEFAULT_SUBPROCESS_TRY_EXCEPTIONS,
|
|
100
|
+
**kwargs: ta.Any,
|
|
101
|
+
) -> ta.Optional[bytes]:
|
|
102
|
+
try:
|
|
103
|
+
return subprocess_check_output(*args, **kwargs)
|
|
104
|
+
except try_exceptions as e: # noqa
|
|
105
|
+
if log.isEnabledFor(logging.DEBUG):
|
|
106
|
+
log.exception('command failed')
|
|
107
|
+
return None
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def subprocess_try_output_str(*args: str, **kwargs: ta.Any) -> ta.Optional[str]:
|
|
111
|
+
out = subprocess_try_output(*args, **kwargs)
|
|
112
|
+
return out.decode().strip() if out is not None else None
|
omlish/logs/configs.py
CHANGED
|
@@ -3,6 +3,7 @@ import logging
|
|
|
3
3
|
import typing as ta
|
|
4
4
|
|
|
5
5
|
from .filters import TidFilter
|
|
6
|
+
from .formatters import JsonLogFormatter
|
|
6
7
|
from .formatters import StandardLogFormatter
|
|
7
8
|
|
|
8
9
|
|
|
@@ -47,10 +48,22 @@ def build_log_format(parts: ta.Iterable[tuple[str, str]]) -> str:
|
|
|
47
48
|
return ' '.join(v for k, v in parts)
|
|
48
49
|
|
|
49
50
|
|
|
50
|
-
def configure_standard_logging(
|
|
51
|
+
def configure_standard_logging(
|
|
52
|
+
level: ta.Any = None,
|
|
53
|
+
*,
|
|
54
|
+
json: bool = False,
|
|
55
|
+
) -> logging.Handler:
|
|
51
56
|
handler = logging.StreamHandler()
|
|
52
|
-
|
|
57
|
+
|
|
58
|
+
formatter: logging.Formatter
|
|
59
|
+
if json:
|
|
60
|
+
formatter = JsonLogFormatter()
|
|
61
|
+
else:
|
|
62
|
+
formatter = StandardLogFormatter(build_log_format(STANDARD_LOG_FORMAT_PARTS))
|
|
63
|
+
handler.setFormatter(formatter)
|
|
64
|
+
|
|
53
65
|
handler.addFilter(TidFilter())
|
|
66
|
+
|
|
54
67
|
logging.root.addHandler(handler)
|
|
55
68
|
|
|
56
69
|
if level is not None:
|
omlish/logs/formatters.py
CHANGED
|
@@ -5,8 +5,8 @@ import datetime
|
|
|
5
5
|
import logging
|
|
6
6
|
import typing as ta
|
|
7
7
|
|
|
8
|
-
from .. import json
|
|
9
8
|
from .. import term
|
|
9
|
+
from ..formats import json
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
class StandardLogFormatter(logging.Formatter):
|
|
@@ -67,5 +67,10 @@ class JsonLogFormatter(logging.Formatter):
|
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
def format(self, record: logging.LogRecord) -> str:
|
|
70
|
-
dct = {
|
|
70
|
+
dct = {
|
|
71
|
+
k: v
|
|
72
|
+
for k, o in self.KEYS.items()
|
|
73
|
+
for v in [getattr(record, k)]
|
|
74
|
+
if not (o and v is None)
|
|
75
|
+
}
|
|
71
76
|
return json.dumps_compact(dct)
|
omlish/marshal/__init__.py
CHANGED
|
@@ -5,6 +5,15 @@ from .base import ( # noqa
|
|
|
5
5
|
MarshalerFactory,
|
|
6
6
|
UnmarshalerFactory,
|
|
7
7
|
|
|
8
|
+
MarshalerFactoryMatchClass,
|
|
9
|
+
UnmarshalerFactoryMatchClass,
|
|
10
|
+
|
|
11
|
+
TypeMapMarshalerFactory,
|
|
12
|
+
TypeMapUnmarshalerFactory,
|
|
13
|
+
|
|
14
|
+
TypeCacheMarshalerFactory,
|
|
15
|
+
TypeCacheUnmarshalerFactory,
|
|
16
|
+
|
|
8
17
|
FuncMarshaler,
|
|
9
18
|
FuncUnmarshaler,
|
|
10
19
|
|
|
@@ -18,6 +27,17 @@ from .base import ( # noqa
|
|
|
18
27
|
SetType,
|
|
19
28
|
)
|
|
20
29
|
|
|
30
|
+
from .exceptions import ( # noqa
|
|
31
|
+
ForbiddenTypeError,
|
|
32
|
+
MarshalError,
|
|
33
|
+
UnhandledTypeError,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
from .forbidden import ( # noqa
|
|
37
|
+
ForbiddenTypeMarshalerFactory,
|
|
38
|
+
ForbiddenTypeUnmarshalerFactory,
|
|
39
|
+
)
|
|
40
|
+
|
|
21
41
|
from .naming import ( # noqa
|
|
22
42
|
Naming,
|
|
23
43
|
translate_name,
|
|
@@ -50,3 +70,15 @@ from .standard import ( # noqa
|
|
|
50
70
|
STANDARD_UNMARSHALER_FACTORIES,
|
|
51
71
|
new_standard_unmarshaler_factory,
|
|
52
72
|
)
|
|
73
|
+
|
|
74
|
+
from .values import ( # noqa
|
|
75
|
+
Value,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
##
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
from ..lang.imports import _trigger_conditional_imports # noqa
|
|
83
|
+
|
|
84
|
+
_trigger_conditional_imports(__package__)
|
omlish/marshal/any.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import typing as ta
|
|
2
2
|
|
|
3
|
+
from .. import reflect as rfl
|
|
3
4
|
from .base import MarshalContext
|
|
4
5
|
from .base import Marshaler
|
|
5
|
-
from .base import
|
|
6
|
+
from .base import TypeMapMarshalerFactory
|
|
7
|
+
from .base import TypeMapUnmarshalerFactory
|
|
6
8
|
from .base import UnmarshalContext
|
|
7
9
|
from .base import Unmarshaler
|
|
8
|
-
from .base import UnmarshalerFactory
|
|
9
|
-
from .factories import TypeMapFactory
|
|
10
10
|
from .values import Value
|
|
11
11
|
|
|
12
12
|
|
|
@@ -21,5 +21,5 @@ class AnyMarshalerUnmarshaler(Marshaler, Unmarshaler):
|
|
|
21
21
|
|
|
22
22
|
ANY_MARSHALER_UNMARSHALER = AnyMarshalerUnmarshaler()
|
|
23
23
|
|
|
24
|
-
ANY_MARSHALER_FACTORY
|
|
25
|
-
ANY_UNMARSHALER_FACTORY
|
|
24
|
+
ANY_MARSHALER_FACTORY = TypeMapMarshalerFactory({rfl.ANY: ANY_MARSHALER_UNMARSHALER})
|
|
25
|
+
ANY_UNMARSHALER_FACTORY = TypeMapUnmarshalerFactory({rfl.ANY: ANY_MARSHALER_UNMARSHALER})
|