omlish 0.0.0.dev6__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.

Files changed (106) hide show
  1. omlish/__about__.py +109 -5
  2. omlish/__init__.py +0 -8
  3. omlish/asyncs/__init__.py +0 -9
  4. omlish/asyncs/anyio.py +40 -0
  5. omlish/bootstrap.py +737 -0
  6. omlish/check.py +1 -1
  7. omlish/collections/__init__.py +4 -0
  8. omlish/collections/exceptions.py +2 -0
  9. omlish/collections/utils.py +38 -9
  10. omlish/configs/strings.py +2 -0
  11. omlish/dataclasses/__init__.py +7 -0
  12. omlish/dataclasses/impl/descriptors.py +95 -0
  13. omlish/dataclasses/impl/reflect.py +1 -1
  14. omlish/dataclasses/utils.py +23 -0
  15. omlish/{lang/datetimes.py → datetimes.py} +8 -4
  16. omlish/diag/procfs.py +1 -1
  17. omlish/diag/threads.py +131 -48
  18. omlish/docker.py +16 -1
  19. omlish/fnpairs.py +0 -4
  20. omlish/{serde → formats}/dotenv.py +3 -0
  21. omlish/{serde → formats}/yaml.py +2 -2
  22. omlish/graphs/trees.py +1 -1
  23. omlish/http/consts.py +6 -0
  24. omlish/http/sessions.py +2 -2
  25. omlish/inject/__init__.py +4 -0
  26. omlish/inject/binder.py +3 -3
  27. omlish/inject/elements.py +1 -1
  28. omlish/inject/impl/injector.py +57 -27
  29. omlish/inject/impl/origins.py +2 -0
  30. omlish/inject/origins.py +3 -0
  31. omlish/inject/utils.py +18 -0
  32. omlish/iterators.py +69 -2
  33. omlish/lang/__init__.py +16 -7
  34. omlish/lang/classes/restrict.py +10 -0
  35. omlish/lang/contextmanagers.py +1 -1
  36. omlish/lang/descriptors.py +3 -3
  37. omlish/lang/imports.py +67 -0
  38. omlish/lang/iterables.py +40 -0
  39. omlish/lang/maybes.py +3 -0
  40. omlish/lang/objects.py +38 -0
  41. omlish/lang/strings.py +25 -0
  42. omlish/lang/sys.py +9 -0
  43. omlish/lang/typing.py +37 -0
  44. omlish/lite/__init__.py +1 -0
  45. omlish/lite/cached.py +18 -0
  46. omlish/lite/check.py +29 -0
  47. omlish/lite/contextmanagers.py +18 -0
  48. omlish/lite/json.py +30 -0
  49. omlish/lite/logs.py +52 -0
  50. omlish/lite/marshal.py +316 -0
  51. omlish/lite/reflect.py +49 -0
  52. omlish/lite/runtime.py +18 -0
  53. omlish/lite/secrets.py +19 -0
  54. omlish/lite/strings.py +25 -0
  55. omlish/lite/subprocesses.py +112 -0
  56. omlish/logs/configs.py +15 -2
  57. omlish/logs/formatters.py +7 -2
  58. omlish/marshal/__init__.py +28 -0
  59. omlish/marshal/any.py +5 -5
  60. omlish/marshal/base.py +27 -11
  61. omlish/marshal/base64.py +24 -9
  62. omlish/marshal/dataclasses.py +34 -28
  63. omlish/marshal/datetimes.py +74 -18
  64. omlish/marshal/enums.py +14 -8
  65. omlish/marshal/exceptions.py +11 -1
  66. omlish/marshal/factories.py +59 -74
  67. omlish/marshal/forbidden.py +35 -0
  68. omlish/marshal/global_.py +11 -4
  69. omlish/marshal/iterables.py +21 -24
  70. omlish/marshal/mappings.py +23 -26
  71. omlish/marshal/numbers.py +51 -0
  72. omlish/marshal/optionals.py +11 -12
  73. omlish/marshal/polymorphism.py +86 -21
  74. omlish/marshal/primitives.py +4 -5
  75. omlish/marshal/standard.py +13 -8
  76. omlish/marshal/uuids.py +4 -5
  77. omlish/matchfns.py +218 -0
  78. omlish/os.py +64 -0
  79. omlish/reflect/__init__.py +39 -0
  80. omlish/reflect/isinstance.py +38 -0
  81. omlish/reflect/ops.py +84 -0
  82. omlish/reflect/subst.py +110 -0
  83. omlish/reflect/types.py +275 -0
  84. omlish/secrets/__init__.py +18 -2
  85. omlish/secrets/crypto.py +132 -0
  86. omlish/secrets/marshal.py +36 -7
  87. omlish/secrets/openssl.py +207 -0
  88. omlish/secrets/secrets.py +260 -8
  89. omlish/secrets/subprocesses.py +42 -0
  90. omlish/sql/dbs.py +6 -5
  91. omlish/sql/exprs.py +12 -0
  92. omlish/sql/secrets.py +10 -0
  93. omlish/term.py +1 -1
  94. omlish/testing/pytest/plugins/switches.py +54 -19
  95. omlish/text/glyphsplit.py +5 -0
  96. omlish-0.0.0.dev7.dist-info/METADATA +50 -0
  97. {omlish-0.0.0.dev6.dist-info → omlish-0.0.0.dev7.dist-info}/RECORD +104 -76
  98. {omlish-0.0.0.dev6.dist-info → omlish-0.0.0.dev7.dist-info}/WHEEL +1 -1
  99. omlish/reflect.py +0 -470
  100. omlish-0.0.0.dev6.dist-info/METADATA +0 -34
  101. /omlish/{asyncs/futures.py → concurrent.py} +0 -0
  102. /omlish/{serde → formats}/__init__.py +0 -0
  103. /omlish/{serde → formats}/json.py +0 -0
  104. /omlish/{serde → formats}/props.py +0 -0
  105. {omlish-0.0.0.dev6.dist-info → omlish-0.0.0.dev7.dist-info}/LICENSE +0 -0
  106. {omlish-0.0.0.dev6.dist-info → omlish-0.0.0.dev7.dist-info}/top_level.txt +0 -0
@@ -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(level: ta.Any = None) -> logging.Handler:
51
+ def configure_standard_logging(
52
+ level: ta.Any = None,
53
+ *,
54
+ json: bool = False,
55
+ ) -> logging.Handler:
51
56
  handler = logging.StreamHandler()
52
- handler.setFormatter(StandardLogFormatter(build_log_format(STANDARD_LOG_FORMAT_PARTS)))
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
@@ -6,7 +6,7 @@ import logging
6
6
  import typing as ta
7
7
 
8
8
  from .. import term
9
- from ..serde import json
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 = {k: v for k, o in self.KEYS.items() for v in [getattr(record, k)] if not (o and v is None)}
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)
@@ -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,
@@ -54,3 +74,11 @@ from .standard import ( # noqa
54
74
  from .values import ( # noqa
55
75
  Value,
56
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 MarshalerFactory
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: MarshalerFactory = TypeMapFactory({ta.Any: ANY_MARSHALER_UNMARSHALER}) # type: ignore
25
- ANY_UNMARSHALER_FACTORY: UnmarshalerFactory = TypeMapFactory({ta.Any: ANY_MARSHALER_UNMARSHALER}) # type: ignore
24
+ ANY_MARSHALER_FACTORY = TypeMapMarshalerFactory({rfl.ANY: ANY_MARSHALER_UNMARSHALER})
25
+ ANY_UNMARSHALER_FACTORY = TypeMapUnmarshalerFactory({rfl.ANY: ANY_MARSHALER_UNMARSHALER})
omlish/marshal/base.py CHANGED
@@ -2,6 +2,7 @@
2
2
  TODO:
3
3
  - mappings
4
4
  - redacted
5
+ - strongly typed MarshalerFactory base class?
5
6
  - strongly typed Composite/Cached Marshaler/Unmarshaler factories - footgun
6
7
  - streaming? Start/EndObject, etc..
7
8
 
@@ -80,10 +81,12 @@ import typing as ta
80
81
  from .. import check
81
82
  from .. import collections as col
82
83
  from .. import lang
84
+ from .. import matchfns as mfs
83
85
  from .. import reflect as rfl
84
86
  from .exceptions import UnhandledTypeError
85
- from .factories import Factory
86
87
  from .factories import RecursiveTypeFactory
88
+ from .factories import TypeCacheFactory
89
+ from .factories import TypeMapFactory
87
90
  from .registries import Registry
88
91
  from .registries import RegistryItem
89
92
  from .utils import _Proxy
@@ -105,8 +108,19 @@ class Unmarshaler(lang.Abstract):
105
108
  raise NotImplementedError
106
109
 
107
110
 
108
- MarshalerFactory = Factory[Marshaler, 'MarshalContext', rfl.Type]
109
- UnmarshalerFactory = Factory[Unmarshaler, 'UnmarshalContext', rfl.Type]
111
+ MarshalerFactory: ta.TypeAlias = mfs.MatchFn[['MarshalContext', rfl.Type], Marshaler]
112
+ UnmarshalerFactory: ta.TypeAlias = mfs.MatchFn[['UnmarshalContext', rfl.Type], Unmarshaler]
113
+
114
+ MarshalerFactoryMatchClass: ta.TypeAlias = mfs.MatchFnClass[['MarshalContext', rfl.Type], Marshaler]
115
+ UnmarshalerFactoryMatchClass: ta.TypeAlias = mfs.MatchFnClass[['UnmarshalContext', rfl.Type], Unmarshaler]
116
+
117
+ #
118
+
119
+ TypeMapMarshalerFactory: ta.TypeAlias = TypeMapFactory['MarshalContext', Marshaler]
120
+ TypeMapUnmarshalerFactory: ta.TypeAlias = TypeMapFactory['UnmarshalContext', Unmarshaler]
121
+
122
+ TypeCacheMarshalerFactory: ta.TypeAlias = TypeCacheFactory['MarshalContext', Marshaler]
123
+ TypeCacheUnmarshalerFactory: ta.TypeAlias = TypeCacheFactory['UnmarshalContext', Unmarshaler]
110
124
 
111
125
 
112
126
  ##
@@ -150,9 +164,10 @@ class MarshalContext(BaseContext, lang.Final):
150
164
 
151
165
  def make(self, o: ta.Any) -> Marshaler:
152
166
  rty = rfl.type_(o)
153
- if (m := check.not_none(self.factory)(self, rty)) is not None: # noqa
154
- return m
155
- raise UnhandledTypeError(rty)
167
+ try:
168
+ return check.not_none(self.factory)(self, rty) # type: ignore
169
+ except mfs.MatchGuardError:
170
+ raise UnhandledTypeError(rty) # noqa
156
171
 
157
172
 
158
173
  @dc.dataclass(frozen=True)
@@ -161,9 +176,10 @@ class UnmarshalContext(BaseContext, lang.Final):
161
176
 
162
177
  def make(self, o: ta.Any) -> Unmarshaler:
163
178
  rty = rfl.type_(o)
164
- if (m := check.not_none(self.factory)(self, rty)) is not None: # noqa
165
- return m
166
- raise UnhandledTypeError(rty)
179
+ try:
180
+ return check.not_none(self.factory)(self, rty) # type: ignore
181
+ except mfs.MatchGuardError:
182
+ raise UnhandledTypeError(rty) # noqa
167
183
 
168
184
 
169
185
  ##
@@ -174,7 +190,7 @@ class _ProxyMarshaler(_Proxy[Marshaler], Marshaler):
174
190
  return self._obj.marshal(ctx, o)
175
191
 
176
192
 
177
- class RecursiveMarshalerFactory(RecursiveTypeFactory[Marshaler, MarshalContext], lang.Final):
193
+ class RecursiveMarshalerFactory(RecursiveTypeFactory[MarshalContext, Marshaler], lang.Final):
178
194
  def __init__(self, f: MarshalerFactory) -> None:
179
195
  super().__init__(f, _ProxyMarshaler._new) # noqa
180
196
 
@@ -184,7 +200,7 @@ class _ProxyUnmarshaler(_Proxy[Unmarshaler], Unmarshaler):
184
200
  return self._obj.unmarshal(ctx, v)
185
201
 
186
202
 
187
- class RecursiveUnmarshalerFactory(RecursiveTypeFactory[Unmarshaler, UnmarshalContext], lang.Final):
203
+ class RecursiveUnmarshalerFactory(RecursiveTypeFactory[UnmarshalContext, Unmarshaler], lang.Final):
188
204
  def __init__(self, f: UnmarshalerFactory) -> None:
189
205
  super().__init__(f, _ProxyUnmarshaler._new) # noqa
190
206
 
omlish/marshal/base64.py CHANGED
@@ -1,25 +1,40 @@
1
1
  import base64
2
+ import typing as ta
2
3
 
3
4
  from .. import check
5
+ from .. import dataclasses as dc
4
6
  from .base import MarshalContext
5
7
  from .base import Marshaler
6
- from .base import MarshalerFactory
8
+ from .base import TypeMapMarshalerFactory
9
+ from .base import TypeMapUnmarshalerFactory
7
10
  from .base import UnmarshalContext
8
11
  from .base import Unmarshaler
9
- from .base import UnmarshalerFactory
10
- from .factories import TypeMapFactory
11
12
  from .values import Value
12
13
 
13
14
 
14
- class Base64MarshalerUnmarshaler(Marshaler, Unmarshaler):
15
+ T = ta.TypeVar('T')
16
+
17
+
18
+ @dc.dataclass(frozen=True)
19
+ class Base64MarshalerUnmarshaler(Marshaler, Unmarshaler, ta.Generic[T]):
20
+ ty: type[T]
21
+
15
22
  def marshal(self, ctx: MarshalContext, o: bytes) -> str:
16
23
  return base64.b64encode(o).decode()
17
24
 
18
- def unmarshal(self, ctx: UnmarshalContext, v: Value) -> bytes:
19
- return base64.b64decode(check.isinstance(v, str).encode())
25
+ def unmarshal(self, ctx: UnmarshalContext, v: Value) -> T:
26
+ return self.ty(base64.b64decode(check.isinstance(v, str).encode())) # type: ignore
27
+
20
28
 
29
+ BASE64_TYPES = (
30
+ bytes,
31
+ bytearray,
32
+ )
21
33
 
22
- BASE64_MARSHALER_UNMARSHALER = Base64MarshalerUnmarshaler()
34
+ BASE64_MARSHALER_FACTORY = TypeMapMarshalerFactory({
35
+ ty: Base64MarshalerUnmarshaler(ty) for ty in BASE64_TYPES
36
+ })
23
37
 
24
- BASE64_MARSHALER_FACTORY: MarshalerFactory = TypeMapFactory({bytes: BASE64_MARSHALER_UNMARSHALER})
25
- BASE64_UNMARSHALER_FACTORY: UnmarshalerFactory = TypeMapFactory({bytes: BASE64_MARSHALER_UNMARSHALER})
38
+ BASE64_UNMARSHALER_FACTORY = TypeMapUnmarshalerFactory({
39
+ ty: Base64MarshalerUnmarshaler(ty) for ty in BASE64_TYPES
40
+ })
@@ -79,37 +79,43 @@ def _make_field_obj(ctx, ty, obj, fac):
79
79
 
80
80
 
81
81
  class DataclassMarshalerFactory(MarshalerFactory):
82
- def __call__(self, ctx: MarshalContext, rty: rfl.Type) -> Marshaler | None:
83
- if isinstance(rty, type) and dc.is_dataclass(rty):
84
- dc_md = get_dataclass_metadata(rty)
85
- fields = [
86
- (fi, _make_field_obj(ctx, fi.type, fi.metadata.marshaler, fi.metadata.marshaler_factory))
87
- for fi in get_field_infos(rty, ctx.options)
88
- ]
89
- return ObjectMarshaler(
90
- fields,
91
- unknown_field=dc_md.unknown_field,
92
- )
93
- return None
82
+ def guard(self, ctx: MarshalContext, rty: rfl.Type) -> bool:
83
+ return isinstance(rty, type) and dc.is_dataclass(rty)
84
+
85
+ def fn(self, ctx: MarshalContext, rty: rfl.Type) -> Marshaler:
86
+ ty = check.isinstance(rty, type)
87
+ check.state(dc.is_dataclass(ty))
88
+ dc_md = get_dataclass_metadata(ty)
89
+ fields = [
90
+ (fi, _make_field_obj(ctx, fi.type, fi.metadata.marshaler, fi.metadata.marshaler_factory))
91
+ for fi in get_field_infos(ty, ctx.options)
92
+ ]
93
+ return ObjectMarshaler(
94
+ fields,
95
+ unknown_field=dc_md.unknown_field,
96
+ )
94
97
 
95
98
 
96
99
  ##
97
100
 
98
101
 
99
102
  class DataclassUnmarshalerFactory(UnmarshalerFactory):
100
- def __call__(self, ctx: UnmarshalContext, rty: rfl.Type) -> Unmarshaler | None:
101
- if isinstance(rty, type) and dc.is_dataclass(rty):
102
- dc_md = get_dataclass_metadata(rty)
103
- d: dict[str, tuple[FieldInfo, Unmarshaler]] = {}
104
- for fi in get_field_infos(rty, ctx.options):
105
- tup = (fi, _make_field_obj(ctx, fi.type, fi.metadata.unmarshaler, fi.metadata.unmarshaler_factory))
106
- for un in fi.unmarshal_names:
107
- if un in d:
108
- raise KeyError(f'Duplicate fields for name {un!r}: {fi.name!r}, {d[un][0].name!r}')
109
- d[un] = tup
110
- return ObjectUnmarshaler(
111
- rty,
112
- d,
113
- unknown_field=dc_md.unknown_field,
114
- )
115
- return None
103
+ def guard(self, ctx: UnmarshalContext, rty: rfl.Type) -> bool:
104
+ return isinstance(rty, type) and dc.is_dataclass(rty)
105
+
106
+ def fn(self, ctx: UnmarshalContext, rty: rfl.Type) -> Unmarshaler:
107
+ ty = check.isinstance(rty, type)
108
+ check.state(dc.is_dataclass(ty))
109
+ dc_md = get_dataclass_metadata(ty)
110
+ d: dict[str, tuple[FieldInfo, Unmarshaler]] = {}
111
+ for fi in get_field_infos(ty, ctx.options):
112
+ tup = (fi, _make_field_obj(ctx, fi.type, fi.metadata.unmarshaler, fi.metadata.unmarshaler_factory))
113
+ for un in fi.unmarshal_names:
114
+ if un in d:
115
+ raise KeyError(f'Duplicate fields for name {un!r}: {fi.name!r}, {d[un][0].name!r}')
116
+ d[un] = tup
117
+ return ObjectUnmarshaler(
118
+ ty,
119
+ d,
120
+ unknown_field=dc_md.unknown_field,
121
+ )
@@ -3,16 +3,28 @@ import datetime
3
3
  import typing as ta
4
4
 
5
5
  from .. import check
6
+ from .. import datetimes as dts
6
7
  from .base import MarshalContext
7
8
  from .base import Marshaler
8
- from .base import MarshalerFactory
9
+ from .base import TypeMapMarshalerFactory
10
+ from .base import TypeMapUnmarshalerFactory
9
11
  from .base import UnmarshalContext
10
12
  from .base import Unmarshaler
11
- from .base import UnmarshalerFactory
12
- from .factories import TypeMapFactory
13
13
  from .values import Value
14
14
 
15
15
 
16
+ DatetimeLikeT = ta.TypeVar('DatetimeLikeT', bound=datetime.datetime | datetime.date | datetime.time)
17
+
18
+ _DATETIME_LIKES = (
19
+ datetime.datetime,
20
+ datetime.date,
21
+ datetime.time,
22
+ )
23
+
24
+
25
+ ##
26
+
27
+
16
28
  DATE_FORMATS: ta.Sequence[str] = [
17
29
  '%Y-%m-%d',
18
30
  ]
@@ -43,48 +55,92 @@ DATETIME_FORMATS: ta.Sequence[str] = [
43
55
  ]
44
56
 
45
57
 
58
+ ##
59
+
60
+
46
61
  @dc.dataclass(frozen=True)
47
- class DatetimeMarshaler(Marshaler):
62
+ class DatetimeMarshaler(Marshaler, ta.Generic[DatetimeLikeT]):
63
+ cls: type[DatetimeLikeT]
48
64
  fmt: str
49
65
 
50
- def marshal(self, ctx: MarshalContext, o: datetime.datetime) -> Value:
66
+ def marshal(self, ctx: MarshalContext, o: DatetimeLikeT) -> Value:
51
67
  return o.strftime(self.fmt)
52
68
 
53
69
 
70
+ _ZERO_DATE = datetime.datetime.now().strptime('', '').date() # noqa
71
+ _ZERO_TIME = datetime.time(0)
72
+
73
+
54
74
  @dc.dataclass(frozen=True)
55
- class DatetimeUnmarshaler(Unmarshaler):
75
+ class DatetimeUnmarshaler(Unmarshaler, ta.Generic[DatetimeLikeT]):
76
+ cls: type[DatetimeLikeT]
56
77
  fmts: ta.Sequence[str]
57
78
  try_iso: bool = False
58
79
 
59
- def unmarshal(self, ctx: UnmarshalContext, v: Value) -> datetime.datetime:
80
+ def unmarshal(self, ctx: UnmarshalContext, v: Value) -> DatetimeLikeT:
60
81
  v = check.isinstance(v, str)
61
82
 
62
83
  if self.try_iso:
63
84
  try:
64
- return datetime.datetime.fromisoformat(v)
85
+ return self.cls.fromisoformat(v) # type: ignore
65
86
  except ValueError:
66
87
  pass
67
88
 
68
89
  for fmt in self.fmts:
69
90
  try:
70
- return datetime.datetime.strptime(v, fmt) # FIXME: timezone # noqa
91
+ dt = datetime.datetime.strptime(v, fmt) # FIXME: timezone # noqa
71
92
  except ValueError:
72
93
  pass
94
+ else:
95
+ if self.cls is datetime.datetime:
96
+ return dt # type: ignore
97
+ elif self.cls is datetime.date:
98
+ if dt.time() != _ZERO_TIME:
99
+ raise ValueError(dt)
100
+ return dt.date() # type: ignore
101
+ elif self.cls is datetime.time:
102
+ if dt.date() != _ZERO_DATE:
103
+ raise ValueError(dt)
104
+ return dt.time() # type: ignore
105
+ else:
106
+ raise TypeError(self.cls)
73
107
 
74
108
  raise ValueError(v)
75
109
 
76
110
 
77
- DATETIME_MARSHALER = DatetimeMarshaler(DATETIME_FORMATS[0])
78
- DATETIME_UNMARSHALER = DatetimeUnmarshaler(DATETIME_FORMATS, try_iso=True)
79
-
80
- DATETIME_MARSHALER_FACTORY: MarshalerFactory = TypeMapFactory({datetime.datetime: DATETIME_MARSHALER})
81
- DATETIME_UNMARSHALER_FACTORY: UnmarshalerFactory = TypeMapFactory({datetime.datetime: DATETIME_UNMARSHALER})
111
+ ##
82
112
 
83
113
 
84
- class IsoDatetimeMarshalerUnmarshaler(Marshaler, Unmarshaler):
114
+ class IsoDatetimeMarshalerUnmarshaler(Marshaler, Unmarshaler, ta.Generic[DatetimeLikeT]):
115
+ cls: type[DatetimeLikeT]
85
116
 
86
- def marshal(self, ctx: MarshalContext, o: datetime.datetime) -> Value:
117
+ def marshal(self, ctx: MarshalContext, o: DatetimeLikeT) -> Value:
87
118
  return o.isoformat()
88
119
 
89
- def unmarshal(self, ctx: UnmarshalContext, v: Value) -> datetime.datetime:
90
- return datetime.datetime.fromisoformat(v) # type: ignore
120
+ def unmarshal(self, ctx: UnmarshalContext, v: Value) -> DatetimeLikeT:
121
+ return self.cls.fromisoformat(v) # type: ignore
122
+
123
+
124
+ ##
125
+
126
+
127
+ class TimedeltaMarshalerUnmarshaler(Marshaler, Unmarshaler):
128
+ def marshal(self, ctx: MarshalContext, o: datetime.timedelta) -> Value:
129
+ return str(o)
130
+
131
+ def unmarshal(self, ctx: UnmarshalContext, v: Value) -> ta.Any:
132
+ return dts.parse_timedelta(check.isinstance(v, str))
133
+
134
+
135
+ ##
136
+
137
+
138
+ DATETIME_MARSHALER_FACTORY = TypeMapMarshalerFactory({
139
+ **{cls: DatetimeMarshaler(cls, DATETIME_FORMATS[0]) for cls in _DATETIME_LIKES},
140
+ datetime.timedelta: TimedeltaMarshalerUnmarshaler(),
141
+ })
142
+
143
+ DATETIME_UNMARSHALER_FACTORY = TypeMapUnmarshalerFactory({
144
+ **{cls: DatetimeUnmarshaler(cls, DATETIME_FORMATS, try_iso=True) for cls in _DATETIME_LIKES},
145
+ datetime.timedelta: TimedeltaMarshalerUnmarshaler(),
146
+ })
omlish/marshal/enums.py CHANGED
@@ -22,10 +22,13 @@ class EnumMarshaler(Marshaler):
22
22
 
23
23
 
24
24
  class EnumMarshalerFactory(MarshalerFactory):
25
- def __call__(self, ctx: MarshalContext, rty: rfl.Type) -> Marshaler | None:
26
- if isinstance(rty, type) and issubclass(rty, enum.Enum):
27
- return EnumMarshaler(rty)
28
- return None
25
+ def guard(self, ctx: MarshalContext, rty: rfl.Type) -> bool:
26
+ return isinstance(rty, type) and issubclass(rty, enum.Enum)
27
+
28
+ def fn(self, ctx: MarshalContext, rty: rfl.Type) -> Marshaler:
29
+ ty = check.isinstance(rty, type)
30
+ check.state(issubclass(ty, enum.Enum))
31
+ return EnumMarshaler(ty)
29
32
 
30
33
 
31
34
  @dc.dataclass(frozen=True)
@@ -37,7 +40,10 @@ class EnumUnmarshaler(Unmarshaler):
37
40
 
38
41
 
39
42
  class EnumUnmarshalerFactory(UnmarshalerFactory):
40
- def __call__(self, ctx: UnmarshalContext, rty: rfl.Type) -> Unmarshaler | None:
41
- if isinstance(rty, type) and issubclass(rty, enum.Enum):
42
- return EnumUnmarshaler(rty)
43
- return None
43
+ def guard(self, ctx: UnmarshalContext, rty: rfl.Type) -> bool:
44
+ return isinstance(rty, type) and issubclass(rty, enum.Enum)
45
+
46
+ def fn(self, ctx: UnmarshalContext, rty: rfl.Type) -> Unmarshaler:
47
+ ty = check.isinstance(rty, type)
48
+ check.state(issubclass(ty, enum.Enum))
49
+ return EnumUnmarshaler(ty)
@@ -1,7 +1,17 @@
1
1
  from .. import reflect as rfl
2
2
 
3
3
 
4
- class UnhandledTypeError(Exception):
4
+ class MarshalError(Exception):
5
+ pass
6
+
7
+
8
+ class UnhandledTypeError(MarshalError):
9
+ @property
10
+ def rty(self) -> rfl.Type:
11
+ return self.args[0]
12
+
13
+
14
+ class ForbiddenTypeError(MarshalError):
5
15
  @property
6
16
  def rty(self) -> rfl.Type:
7
17
  return self.args[0]