tigrbl-core 0.1.0.dev5__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.
- tigrbl_core/_spec/__init__.py +88 -0
- tigrbl_core/_spec/alias_spec.py +35 -0
- tigrbl_core/_spec/app_spec.py +216 -0
- tigrbl_core/_spec/binding_spec.py +78 -0
- tigrbl_core/_spec/column_spec.py +183 -0
- tigrbl_core/_spec/engine_spec.py +384 -0
- tigrbl_core/_spec/field_spec.py +49 -0
- tigrbl_core/_spec/hook_spec.py +42 -0
- tigrbl_core/_spec/hook_types.py +34 -0
- tigrbl_core/_spec/io_spec.py +137 -0
- tigrbl_core/_spec/middleware_spec.py +28 -0
- tigrbl_core/_spec/op_spec.py +452 -0
- tigrbl_core/_spec/op_utils.py +43 -0
- tigrbl_core/_spec/plugins.py +44 -0
- tigrbl_core/_spec/registry.py +38 -0
- tigrbl_core/_spec/request_spec.py +21 -0
- tigrbl_core/_spec/response_resolver.py +63 -0
- tigrbl_core/_spec/response_spec.py +36 -0
- tigrbl_core/_spec/response_types.py +12 -0
- tigrbl_core/_spec/router_spec.py +102 -0
- tigrbl_core/_spec/schema_spec.py +47 -0
- tigrbl_core/_spec/serde.py +164 -0
- tigrbl_core/_spec/session_spec.py +148 -0
- tigrbl_core/_spec/storage_spec.py +79 -0
- tigrbl_core/_spec/table_registry_spec.py +16 -0
- tigrbl_core/_spec/table_spec.py +118 -0
- tigrbl_core/config/__init__.py +6 -0
- tigrbl_core/config/constants.py +224 -0
- tigrbl_core/config/defaults.py +32 -0
- tigrbl_core/config/engine_traversal.py +102 -0
- tigrbl_core/config/resolver.py +276 -0
- tigrbl_core/core/__init__.py +7 -0
- tigrbl_core/op/__init__.py +17 -0
- tigrbl_core/op/canonical.py +39 -0
- tigrbl_core/op/collect.py +20 -0
- tigrbl_core/op/types.py +29 -0
- tigrbl_core/schema/__init__.py +28 -0
- tigrbl_core/schema/builder/__init__.py +17 -0
- tigrbl_core/schema/builder/build_schema.py +307 -0
- tigrbl_core/schema/builder/cache.py +24 -0
- tigrbl_core/schema/builder/extras.py +85 -0
- tigrbl_core/schema/builder/helpers.py +87 -0
- tigrbl_core/schema/builder/list_params.py +117 -0
- tigrbl_core/schema/builder/strip_parent_fields.py +70 -0
- tigrbl_core/schema/get_schema.py +85 -0
- tigrbl_core/schema/utils.py +142 -0
- tigrbl_core-0.1.0.dev5.dist-info/METADATA +54 -0
- tigrbl_core-0.1.0.dev5.dist-info/RECORD +49 -0
- tigrbl_core-0.1.0.dev5.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Canonical spec package.
|
|
2
|
+
|
|
3
|
+
Keep this module import-light to avoid circular imports during package startup.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from importlib import import_module
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
_EXPORTS = {
|
|
12
|
+
"AliasSpec": "alias_spec",
|
|
13
|
+
"AppSpec": "app_spec",
|
|
14
|
+
"BindingSpec": "binding_spec",
|
|
15
|
+
"BindingRegistrySpec": "binding_spec",
|
|
16
|
+
"TransportBindingSpec": "binding_spec",
|
|
17
|
+
"HttpRestBindingSpec": "binding_spec",
|
|
18
|
+
"HttpJsonRpcBindingSpec": "binding_spec",
|
|
19
|
+
"WsBindingSpec": "binding_spec",
|
|
20
|
+
"resolve_rest_nested_prefix": "binding_spec",
|
|
21
|
+
"ColumnSpec": "column_spec",
|
|
22
|
+
"EngineSpec": "engine_spec",
|
|
23
|
+
"EngineProviderSpec": "engine_spec",
|
|
24
|
+
"FieldSpec": "field_spec",
|
|
25
|
+
"F": "field_spec",
|
|
26
|
+
"HookSpec": "hook_spec",
|
|
27
|
+
"IOSpec": "io_spec",
|
|
28
|
+
"IO": "io_spec",
|
|
29
|
+
"MiddlewareSpec": "middleware_spec",
|
|
30
|
+
"OpSpec": "op_spec",
|
|
31
|
+
"Arity": "op_spec",
|
|
32
|
+
"TargetOp": "op_spec",
|
|
33
|
+
"PersistPolicy": "op_spec",
|
|
34
|
+
"PHASE": "op_spec",
|
|
35
|
+
"PHASES": "op_spec",
|
|
36
|
+
"HookPhase": "hook_spec",
|
|
37
|
+
"TemplateSpec": "response_spec",
|
|
38
|
+
"ResponseSpec": "response_spec",
|
|
39
|
+
"resolve_response_spec": "response_resolver",
|
|
40
|
+
"RequestSpec": "request_spec",
|
|
41
|
+
"RouterSpec": "router_spec",
|
|
42
|
+
"SchemaSpec": "schema_spec",
|
|
43
|
+
"SchemaRef": "schema_spec",
|
|
44
|
+
"SchemaArg": "schema_spec",
|
|
45
|
+
"SessionSpec": "session_spec",
|
|
46
|
+
"session_spec": "session_spec",
|
|
47
|
+
"tx_read_committed": "session_spec",
|
|
48
|
+
"tx_repeatable_read": "session_spec",
|
|
49
|
+
"tx_serializable": "session_spec",
|
|
50
|
+
"readonly": "session_spec",
|
|
51
|
+
"StorageSpec": "storage_spec",
|
|
52
|
+
"S": "storage_spec",
|
|
53
|
+
"StorageTransformSpec": "storage_spec",
|
|
54
|
+
"StorageTransform": "storage_spec",
|
|
55
|
+
"ForeignKeySpec": "storage_spec",
|
|
56
|
+
"TableSpec": "table_spec",
|
|
57
|
+
"TableRegistrySpec": "table_registry_spec",
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
__all__ = list(_EXPORTS)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def __getattr__(name: str) -> Any:
|
|
64
|
+
if name in {"PHASE", "PHASES"}:
|
|
65
|
+
from .hook_types import HookPhase, HookPhases
|
|
66
|
+
|
|
67
|
+
value = HookPhase if name == "PHASE" else HookPhases
|
|
68
|
+
globals()[name] = value
|
|
69
|
+
return value
|
|
70
|
+
if name in {"F", "IO", "S"}:
|
|
71
|
+
aliases = {
|
|
72
|
+
"F": ("field_spec", "FieldSpec"),
|
|
73
|
+
"IO": ("io_spec", "IOSpec"),
|
|
74
|
+
"S": ("storage_spec", "StorageSpec"),
|
|
75
|
+
}
|
|
76
|
+
module_name, attr = aliases[name]
|
|
77
|
+
module = import_module(f"{__name__}.{module_name}")
|
|
78
|
+
value = getattr(module, attr)
|
|
79
|
+
globals()[name] = value
|
|
80
|
+
return value
|
|
81
|
+
|
|
82
|
+
module_name = _EXPORTS.get(name)
|
|
83
|
+
if module_name is None:
|
|
84
|
+
raise AttributeError(name)
|
|
85
|
+
module = import_module(f"{__name__}.{module_name}")
|
|
86
|
+
value = getattr(module, name)
|
|
87
|
+
globals()[name] = value
|
|
88
|
+
return value
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from abc import ABC, abstractmethod
|
|
4
|
+
from typing import TYPE_CHECKING, Optional
|
|
5
|
+
|
|
6
|
+
from .._spec.op_spec import Arity, PersistPolicy
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING: # pragma: no cover
|
|
9
|
+
from .schema_spec import SchemaArg
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class AliasSpec(ABC):
|
|
13
|
+
@property
|
|
14
|
+
@abstractmethod
|
|
15
|
+
def alias(self) -> str: ...
|
|
16
|
+
|
|
17
|
+
@property
|
|
18
|
+
@abstractmethod
|
|
19
|
+
def request_schema(self) -> Optional[SchemaArg]: ...
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
@abstractmethod
|
|
23
|
+
def response_schema(self) -> Optional[SchemaArg]: ...
|
|
24
|
+
|
|
25
|
+
@property
|
|
26
|
+
@abstractmethod
|
|
27
|
+
def persist(self) -> Optional[PersistPolicy]: ...
|
|
28
|
+
|
|
29
|
+
@property
|
|
30
|
+
@abstractmethod
|
|
31
|
+
def arity(self) -> Optional[Arity]: ...
|
|
32
|
+
|
|
33
|
+
@property
|
|
34
|
+
@abstractmethod
|
|
35
|
+
def rest(self) -> Optional[bool]: ...
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# pkgs/standards/tigrbl_core/tigrbl/_spec/app_spec.py
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from typing import Any, Callable, Iterable, Mapping, Optional, Sequence
|
|
5
|
+
|
|
6
|
+
from .._spec.engine_spec import EngineCfg
|
|
7
|
+
from .._spec.response_spec import ResponseSpec
|
|
8
|
+
from .serde import SerdeMixin
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _seqify(value: Any) -> tuple[Any, ...]:
|
|
12
|
+
"""Normalize sequence-like inputs while treating scalars as a single item."""
|
|
13
|
+
|
|
14
|
+
if value is None:
|
|
15
|
+
return ()
|
|
16
|
+
if isinstance(value, tuple):
|
|
17
|
+
return value
|
|
18
|
+
if isinstance(value, (str, bytes, bytearray)):
|
|
19
|
+
return (value,)
|
|
20
|
+
if isinstance(value, Mapping):
|
|
21
|
+
return (value,)
|
|
22
|
+
if isinstance(value, Iterable):
|
|
23
|
+
return tuple(value)
|
|
24
|
+
return (value,)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def merge_seq_attr(
|
|
28
|
+
owner: type,
|
|
29
|
+
attr: str,
|
|
30
|
+
*,
|
|
31
|
+
include_inherited: bool = False,
|
|
32
|
+
reverse: bool = False,
|
|
33
|
+
dedupe: bool = True,
|
|
34
|
+
) -> tuple[Any, ...]:
|
|
35
|
+
"""Merge sequence-like class attributes over the MRO."""
|
|
36
|
+
|
|
37
|
+
values: list[Any] = []
|
|
38
|
+
seen_hashable: set[Any] = set()
|
|
39
|
+
mro = reversed(owner.__mro__) if reverse else owner.__mro__
|
|
40
|
+
for base in mro:
|
|
41
|
+
if include_inherited:
|
|
42
|
+
if not hasattr(base, attr):
|
|
43
|
+
continue
|
|
44
|
+
seq = getattr(base, attr) or ()
|
|
45
|
+
else:
|
|
46
|
+
seq = base.__dict__.get(attr, ()) or ()
|
|
47
|
+
for item in _seqify(seq):
|
|
48
|
+
if dedupe:
|
|
49
|
+
try:
|
|
50
|
+
if item in seen_hashable:
|
|
51
|
+
continue
|
|
52
|
+
seen_hashable.add(item)
|
|
53
|
+
except TypeError:
|
|
54
|
+
if any(item == existing for existing in values):
|
|
55
|
+
continue
|
|
56
|
+
values.append(item)
|
|
57
|
+
return tuple(values)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def normalize_app_spec(spec: "AppSpec") -> "AppSpec":
|
|
61
|
+
"""Return a normalized app spec snapshot with stable sequence fields."""
|
|
62
|
+
|
|
63
|
+
routers = _seqify(spec.routers)
|
|
64
|
+
tables = _seqify(spec.tables)
|
|
65
|
+
ops = _seqify(spec.ops)
|
|
66
|
+
|
|
67
|
+
return AppSpec(
|
|
68
|
+
title=str(spec.title or "Tigrbl"),
|
|
69
|
+
description=spec.description,
|
|
70
|
+
version=str(spec.version or "0.1.0"),
|
|
71
|
+
engine=spec.engine,
|
|
72
|
+
routers=routers,
|
|
73
|
+
ops=ops,
|
|
74
|
+
tables=tables,
|
|
75
|
+
schemas=_seqify(spec.schemas),
|
|
76
|
+
hooks=_seqify(spec.hooks),
|
|
77
|
+
security_deps=_seqify(spec.security_deps),
|
|
78
|
+
deps=_seqify(spec.deps),
|
|
79
|
+
middlewares=_seqify(spec.middlewares),
|
|
80
|
+
response=spec.response,
|
|
81
|
+
jsonrpc_prefix=str(spec.jsonrpc_prefix or "/rpc"),
|
|
82
|
+
system_prefix=str(spec.system_prefix or "/system"),
|
|
83
|
+
lifespan=spec.lifespan,
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@dataclass(eq=False)
|
|
88
|
+
class AppSpec(SerdeMixin):
|
|
89
|
+
"""
|
|
90
|
+
Used to *produce an App subclass* via App.from_spec().
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
title: str = "Tigrbl"
|
|
94
|
+
description: str | None = None
|
|
95
|
+
version: str = "0.1.0"
|
|
96
|
+
engine: Optional[EngineCfg] = None
|
|
97
|
+
|
|
98
|
+
# NEW: multi-Router composition (store Router classes or instances)
|
|
99
|
+
routers: Sequence[Any] = field(default_factory=tuple)
|
|
100
|
+
|
|
101
|
+
# NEW: orchestration/topology knobs
|
|
102
|
+
ops: Sequence[Any] = field(default_factory=tuple) # op descriptors or specs
|
|
103
|
+
tables: Sequence[Any] = field(default_factory=tuple) # table refs owned by app
|
|
104
|
+
schemas: Sequence[Any] = field(default_factory=tuple) # schema classes/defs
|
|
105
|
+
hooks: Sequence[Callable[..., Any]] = field(default_factory=tuple)
|
|
106
|
+
|
|
107
|
+
# security/dep stacks (ASGI dependencies or callables)
|
|
108
|
+
security_deps: Sequence[Callable[..., Any]] = field(default_factory=tuple)
|
|
109
|
+
deps: Sequence[Callable[..., Any]] = field(default_factory=tuple)
|
|
110
|
+
|
|
111
|
+
# response defaults
|
|
112
|
+
response: Optional[ResponseSpec] = None
|
|
113
|
+
|
|
114
|
+
# system routing prefixes (REST/JSON-RPC namespaces)
|
|
115
|
+
jsonrpc_prefix: str = "/rpc"
|
|
116
|
+
system_prefix: str = "/system"
|
|
117
|
+
|
|
118
|
+
# optional framework bits
|
|
119
|
+
middlewares: Sequence[Any] = field(default_factory=tuple)
|
|
120
|
+
lifespan: Optional[Callable[..., Any]] = None
|
|
121
|
+
|
|
122
|
+
@classmethod
|
|
123
|
+
def from_dict(cls, payload: dict[str, Any]) -> "AppSpec":
|
|
124
|
+
return super().from_dict(payload)
|
|
125
|
+
|
|
126
|
+
@classmethod
|
|
127
|
+
def collect(cls, app: type) -> "AppSpec":
|
|
128
|
+
sentinel = object()
|
|
129
|
+
title: Any = sentinel
|
|
130
|
+
version: Any = sentinel
|
|
131
|
+
engine: Any | None = sentinel # type: ignore[assignment]
|
|
132
|
+
response: Any = sentinel
|
|
133
|
+
jsonrpc_prefix: Any = sentinel
|
|
134
|
+
system_prefix: Any = sentinel
|
|
135
|
+
lifespan: Any = sentinel
|
|
136
|
+
|
|
137
|
+
for base in app.__mro__:
|
|
138
|
+
if "TITLE" in base.__dict__ and title is sentinel:
|
|
139
|
+
title = base.__dict__["TITLE"]
|
|
140
|
+
if "VERSION" in base.__dict__ and version is sentinel:
|
|
141
|
+
version = base.__dict__["VERSION"]
|
|
142
|
+
if "ENGINE" in base.__dict__ and engine is sentinel:
|
|
143
|
+
engine = base.__dict__["ENGINE"]
|
|
144
|
+
if "RESPONSE" in base.__dict__ and response is sentinel:
|
|
145
|
+
response = base.__dict__["RESPONSE"]
|
|
146
|
+
if "JSONRPC_PREFIX" in base.__dict__ and jsonrpc_prefix is sentinel:
|
|
147
|
+
jsonrpc_prefix = base.__dict__["JSONRPC_PREFIX"]
|
|
148
|
+
if "SYSTEM_PREFIX" in base.__dict__ and system_prefix is sentinel:
|
|
149
|
+
system_prefix = base.__dict__["SYSTEM_PREFIX"]
|
|
150
|
+
if "LIFESPAN" in base.__dict__ and lifespan is sentinel:
|
|
151
|
+
lifespan = base.__dict__["LIFESPAN"]
|
|
152
|
+
|
|
153
|
+
if title is sentinel:
|
|
154
|
+
title = "Tigrbl"
|
|
155
|
+
if version is sentinel:
|
|
156
|
+
version = "0.1.0"
|
|
157
|
+
if engine is sentinel:
|
|
158
|
+
engine = None
|
|
159
|
+
if response is sentinel:
|
|
160
|
+
response = None
|
|
161
|
+
if jsonrpc_prefix is sentinel:
|
|
162
|
+
jsonrpc_prefix = "/rpc"
|
|
163
|
+
if system_prefix is sentinel:
|
|
164
|
+
system_prefix = "/system"
|
|
165
|
+
if lifespan is sentinel:
|
|
166
|
+
lifespan = None
|
|
167
|
+
|
|
168
|
+
description = getattr(app, "DESCRIPTION", None)
|
|
169
|
+
include_inherited_routers = "ROUTERS" not in app.__dict__
|
|
170
|
+
spec = cls(
|
|
171
|
+
title=title,
|
|
172
|
+
description=description,
|
|
173
|
+
version=version,
|
|
174
|
+
engine=engine,
|
|
175
|
+
routers=tuple(
|
|
176
|
+
merge_seq_attr(
|
|
177
|
+
app,
|
|
178
|
+
"ROUTERS",
|
|
179
|
+
include_inherited=include_inherited_routers,
|
|
180
|
+
reverse=include_inherited_routers,
|
|
181
|
+
dedupe=False,
|
|
182
|
+
)
|
|
183
|
+
or ()
|
|
184
|
+
),
|
|
185
|
+
ops=tuple(merge_seq_attr(app, "OPS") or ()),
|
|
186
|
+
tables=tuple(merge_seq_attr(app, "TABLES") or ()),
|
|
187
|
+
schemas=tuple(merge_seq_attr(app, "SCHEMAS") or ()),
|
|
188
|
+
hooks=tuple(merge_seq_attr(app, "HOOKS") or ()),
|
|
189
|
+
security_deps=tuple(merge_seq_attr(app, "SECURITY_DEPS") or ()),
|
|
190
|
+
deps=tuple(merge_seq_attr(app, "DEPS") or ()),
|
|
191
|
+
response=response,
|
|
192
|
+
jsonrpc_prefix=(jsonrpc_prefix or "/rpc"),
|
|
193
|
+
system_prefix=(system_prefix or "/system"),
|
|
194
|
+
middlewares=tuple(merge_seq_attr(app, "MIDDLEWARES") or ()),
|
|
195
|
+
lifespan=lifespan,
|
|
196
|
+
)
|
|
197
|
+
return normalize_app_spec(
|
|
198
|
+
cls(
|
|
199
|
+
title=spec.title,
|
|
200
|
+
description=spec.description,
|
|
201
|
+
version=spec.version,
|
|
202
|
+
engine=spec.engine,
|
|
203
|
+
routers=tuple(spec.routers or ()),
|
|
204
|
+
ops=tuple(spec.ops or ()),
|
|
205
|
+
tables=tuple(spec.tables or ()),
|
|
206
|
+
schemas=tuple(spec.schemas or ()),
|
|
207
|
+
hooks=tuple(spec.hooks or ()),
|
|
208
|
+
security_deps=tuple(spec.security_deps or ()),
|
|
209
|
+
deps=tuple(spec.deps or ()),
|
|
210
|
+
response=spec.response,
|
|
211
|
+
jsonrpc_prefix=(spec.jsonrpc_prefix or "/rpc"),
|
|
212
|
+
system_prefix=(spec.system_prefix or "/system"),
|
|
213
|
+
middlewares=tuple(spec.middlewares or ()),
|
|
214
|
+
lifespan=spec.lifespan,
|
|
215
|
+
)
|
|
216
|
+
)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass, field
|
|
4
|
+
from typing import Literal, Optional, Type, Union
|
|
5
|
+
|
|
6
|
+
from ..config.constants import TIGRBL_NESTED_PATHS_ATTR
|
|
7
|
+
from .serde import SerdeMixin
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(frozen=True, slots=True)
|
|
11
|
+
class HttpRestBindingSpec(SerdeMixin):
|
|
12
|
+
proto: Literal["http.rest", "https.rest"]
|
|
13
|
+
methods: tuple[str, ...]
|
|
14
|
+
path: str
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass(frozen=True, slots=True)
|
|
18
|
+
class HttpJsonRpcBindingSpec(SerdeMixin):
|
|
19
|
+
proto: Literal["http.jsonrpc", "https.jsonrpc"]
|
|
20
|
+
rpc_method: str
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True, slots=True)
|
|
24
|
+
class WsBindingSpec(SerdeMixin):
|
|
25
|
+
proto: Literal["ws", "wss"]
|
|
26
|
+
path: str
|
|
27
|
+
subprotocols: tuple[str, ...] = ()
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
TransportBindingSpec = Union[
|
|
31
|
+
HttpRestBindingSpec,
|
|
32
|
+
HttpJsonRpcBindingSpec,
|
|
33
|
+
WsBindingSpec,
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(frozen=True, slots=True)
|
|
38
|
+
class BindingSpec(SerdeMixin):
|
|
39
|
+
"""Named binding declaration used for registry composition."""
|
|
40
|
+
|
|
41
|
+
name: str
|
|
42
|
+
spec: TransportBindingSpec
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
@dataclass(slots=True)
|
|
46
|
+
class BindingRegistrySpec(SerdeMixin):
|
|
47
|
+
"""Simple in-memory registry for named transport bindings."""
|
|
48
|
+
|
|
49
|
+
_bindings: dict[str, BindingSpec] = field(default_factory=dict)
|
|
50
|
+
|
|
51
|
+
def register(self, binding: BindingSpec) -> None:
|
|
52
|
+
self._bindings[binding.name] = binding
|
|
53
|
+
|
|
54
|
+
def get(self, name: str) -> Optional[BindingSpec]:
|
|
55
|
+
return self._bindings.get(name)
|
|
56
|
+
|
|
57
|
+
def values(self) -> tuple[BindingSpec, ...]:
|
|
58
|
+
return tuple(self._bindings.values())
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def resolve_rest_nested_prefix(model: Type) -> Optional[str]:
|
|
62
|
+
"""Return the configured nested REST prefix for ``model`` if present."""
|
|
63
|
+
|
|
64
|
+
cb = getattr(model, TIGRBL_NESTED_PATHS_ATTR, None)
|
|
65
|
+
if callable(cb):
|
|
66
|
+
return cb()
|
|
67
|
+
return getattr(model, "_nested_path", None)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
__all__ = [
|
|
71
|
+
"BindingSpec",
|
|
72
|
+
"BindingRegistrySpec",
|
|
73
|
+
"HttpJsonRpcBindingSpec",
|
|
74
|
+
"HttpRestBindingSpec",
|
|
75
|
+
"TransportBindingSpec",
|
|
76
|
+
"WsBindingSpec",
|
|
77
|
+
"resolve_rest_nested_prefix",
|
|
78
|
+
]
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
from functools import lru_cache
|
|
5
|
+
from types import SimpleNamespace
|
|
6
|
+
from typing import Any, Callable, Dict, Optional
|
|
7
|
+
|
|
8
|
+
from .._spec.field_spec import FieldSpec as F
|
|
9
|
+
from .._spec.io_spec import IOSpec as IO
|
|
10
|
+
from .._spec.storage_spec import StorageSpec as S
|
|
11
|
+
from .serde import SerdeMixin
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger("uvicorn")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class ColumnSpec(SerdeMixin):
|
|
17
|
+
"""Aggregate configuration for a model attribute.
|
|
18
|
+
|
|
19
|
+
A :class:`ColumnSpec` brings together the three lower-level specs used by
|
|
20
|
+
Tigrbl's declarative column system:
|
|
21
|
+
|
|
22
|
+
- ``storage`` (:class:`~tigrbl._spec.storage_spec.StorageSpec`) controls
|
|
23
|
+
how the value is persisted in the database.
|
|
24
|
+
- ``field`` (:class:`~tigrbl._spec.field_spec.FieldSpec`) describes the
|
|
25
|
+
Python type and any schema metadata.
|
|
26
|
+
- ``io`` (:class:`~tigrbl._spec.io_spec.IOSpec`) governs inbound and
|
|
27
|
+
outbound API exposure.
|
|
28
|
+
|
|
29
|
+
Optional ``default_factory`` and ``read_producer`` callables allow for
|
|
30
|
+
programmatic defaults and virtual read-time values respectively.
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
def __init__(
|
|
34
|
+
self,
|
|
35
|
+
*,
|
|
36
|
+
storage: S | None,
|
|
37
|
+
field: F | None = None,
|
|
38
|
+
io: IO | None = None,
|
|
39
|
+
default_factory: Optional[Callable[[dict], Any]] = None,
|
|
40
|
+
read_producer: Optional[Callable[[object, dict], Any]] = None,
|
|
41
|
+
) -> None:
|
|
42
|
+
self.storage = storage
|
|
43
|
+
self.field = field if field is not None else F()
|
|
44
|
+
self.io = io if io is not None else IO()
|
|
45
|
+
self.default_factory = default_factory
|
|
46
|
+
self.read_producer = read_producer
|
|
47
|
+
|
|
48
|
+
# Default inbound/outbound verbs for columns lacking an explicit ColumnSpec.
|
|
49
|
+
#
|
|
50
|
+
# Without this, plain SQLAlchemy ``Column`` definitions are omitted from the
|
|
51
|
+
# collected spec map, causing downstream components to treat their values as
|
|
52
|
+
# unknown. By seeding such columns with a permissive IO spec we ensure they
|
|
53
|
+
# participate in canonical CRUD operations just like columns defined via
|
|
54
|
+
# ``acol``.
|
|
55
|
+
_DEFAULT_IO = IO(
|
|
56
|
+
in_verbs=("create", "update", "replace"),
|
|
57
|
+
out_verbs=("read", "list"),
|
|
58
|
+
mutable_verbs=("create", "update", "replace"),
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
@staticmethod
|
|
62
|
+
def _model_label(model: object) -> str:
|
|
63
|
+
return str(
|
|
64
|
+
getattr(model, "__name__", None)
|
|
65
|
+
or getattr(model, "name", None)
|
|
66
|
+
or type(model).__name__
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def _coerce_columns_iterable(columns: object) -> tuple[object, ...]:
|
|
71
|
+
"""Normalize model/table column containers to an iterable tuple.
|
|
72
|
+
|
|
73
|
+
Some table classes expose ``columns`` as a ``SimpleNamespace`` of
|
|
74
|
+
``ColumnSpec`` objects for convenience. Runtime collectors should treat
|
|
75
|
+
that namespace as a mapping and iterate over its values rather than
|
|
76
|
+
trying to iterate the namespace object directly.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
if isinstance(columns, SimpleNamespace):
|
|
80
|
+
return tuple(columns.__dict__.values())
|
|
81
|
+
if isinstance(columns, dict):
|
|
82
|
+
return tuple(columns.values())
|
|
83
|
+
try:
|
|
84
|
+
return tuple(columns) # type: ignore[arg-type]
|
|
85
|
+
except TypeError:
|
|
86
|
+
return ()
|
|
87
|
+
|
|
88
|
+
@staticmethod
|
|
89
|
+
@lru_cache(maxsize=None)
|
|
90
|
+
def _mro_collect_columns_cached(
|
|
91
|
+
model: object, _cache_bust: int
|
|
92
|
+
) -> Dict[str, "ColumnSpec"]:
|
|
93
|
+
"""Collect ColumnSpecs declared on *model* and all mixins.
|
|
94
|
+
|
|
95
|
+
Iterates across the model's MRO so that mixin-defined columns are
|
|
96
|
+
included in the resulting mapping. Later definitions take precedence
|
|
97
|
+
over earlier ones in the MRO. Any table-backed columns lacking a spec
|
|
98
|
+
are populated with a default ColumnSpec so they participate in opviews
|
|
99
|
+
and schema generation.
|
|
100
|
+
"""
|
|
101
|
+
logger.debug("Collecting columns for %s", ColumnSpec._model_label(model))
|
|
102
|
+
out: Dict[str, ColumnSpec] = {}
|
|
103
|
+
mro = getattr(model, "__mro__", ()) or ()
|
|
104
|
+
for base in reversed(mro):
|
|
105
|
+
mapping = getattr(base, "__tigrbl_colspecs__", None)
|
|
106
|
+
if isinstance(mapping, dict):
|
|
107
|
+
out.update(mapping)
|
|
108
|
+
mapping = getattr(base, "__tigrbl_cols__", None)
|
|
109
|
+
if isinstance(mapping, dict):
|
|
110
|
+
out.update(mapping)
|
|
111
|
+
|
|
112
|
+
cols = None
|
|
113
|
+
table = getattr(model, "__table__", None)
|
|
114
|
+
if table is not None:
|
|
115
|
+
cols = getattr(table, "columns", None)
|
|
116
|
+
elif hasattr(model, "columns"):
|
|
117
|
+
cols = getattr(model, "columns", None)
|
|
118
|
+
|
|
119
|
+
if cols is not None:
|
|
120
|
+
for col in ColumnSpec._coerce_columns_iterable(cols):
|
|
121
|
+
name = getattr(col, "key", None) or getattr(col, "name", None)
|
|
122
|
+
if not isinstance(name, str):
|
|
123
|
+
continue
|
|
124
|
+
out.setdefault(name, ColumnSpec(storage=S(), io=ColumnSpec._DEFAULT_IO))
|
|
125
|
+
else:
|
|
126
|
+
# Declarative models can be inspected before SQLAlchemy finishes
|
|
127
|
+
# materializing ``__table__``. In that transient state plain
|
|
128
|
+
# ``Column(...)`` declarations still exist on the class body and
|
|
129
|
+
# should participate in schema inference.
|
|
130
|
+
for base in reversed(mro):
|
|
131
|
+
for attr_name, value in vars(base).items():
|
|
132
|
+
if attr_name.startswith("_") or attr_name in out:
|
|
133
|
+
continue
|
|
134
|
+
if hasattr(value, "type") and hasattr(value, "nullable"):
|
|
135
|
+
out[attr_name] = ColumnSpec(
|
|
136
|
+
storage=S(), io=ColumnSpec._DEFAULT_IO
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
logger.debug(
|
|
140
|
+
"Collected %d columns for %s", len(out), ColumnSpec._model_label(model)
|
|
141
|
+
)
|
|
142
|
+
return out
|
|
143
|
+
|
|
144
|
+
@classmethod
|
|
145
|
+
def collect(
|
|
146
|
+
cls, model: object, *, _cache_bust: int | None = None
|
|
147
|
+
) -> Dict[str, "ColumnSpec"]:
|
|
148
|
+
"""Collect ColumnSpecs for *model* with topology-aware cache busting."""
|
|
149
|
+
|
|
150
|
+
if _cache_bust is None:
|
|
151
|
+
table = getattr(model, "__table__", None)
|
|
152
|
+
cols = getattr(table, "columns", None) if table is not None else None
|
|
153
|
+
col_count = (
|
|
154
|
+
len(cls._coerce_columns_iterable(cols)) if cols is not None else 0
|
|
155
|
+
)
|
|
156
|
+
_cache_bust = hash(
|
|
157
|
+
(
|
|
158
|
+
id(getattr(model, "__tigrbl_colspecs__", None)),
|
|
159
|
+
id(getattr(model, "__tigrbl_cols__", None)),
|
|
160
|
+
id(table),
|
|
161
|
+
col_count,
|
|
162
|
+
)
|
|
163
|
+
)
|
|
164
|
+
return cls._mro_collect_columns_cached(model, _cache_bust)
|
|
165
|
+
|
|
166
|
+
@classmethod
|
|
167
|
+
def mro_collect_columns(
|
|
168
|
+
cls, model: object, *, _cache_bust: int | None = None
|
|
169
|
+
) -> Dict[str, "ColumnSpec"]:
|
|
170
|
+
"""Backward-compatible alias for :meth:`collect`."""
|
|
171
|
+
|
|
172
|
+
return cls.collect(model, _cache_bust=_cache_bust)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def mro_collect_columns(
|
|
176
|
+
model: object, *, _cache_bust: int | None = None
|
|
177
|
+
) -> Dict[str, ColumnSpec]:
|
|
178
|
+
return ColumnSpec.collect(model, _cache_bust=_cache_bust)
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
mro_collect_columns.cache_clear = ColumnSpec._mro_collect_columns_cached.cache_clear
|
|
182
|
+
|
|
183
|
+
__all__ = ["ColumnSpec", "mro_collect_columns"]
|