taut-shape 0.9.1__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.
- taut_shape/__init__.py +26 -0
- taut_shape/_schema.py +21 -0
- taut_shape/atom/__init__.py +5 -0
- taut_shape/atom/_generated.py +115 -0
- taut_shape/atom/engine.py +229 -0
- taut_shape/atom/wire.py +202 -0
- taut_shape/crdt/__init__.py +7 -0
- taut_shape/crdt/_generated.py +106 -0
- taut_shape/crdt/engine.py +209 -0
- taut_shape/crdt/text.py +103 -0
- taut_shape/crdt/wire.py +106 -0
- taut_shape/log/__init__.py +61 -0
- taut_shape/log/_generated.py +134 -0
- taut_shape/log/_sessions.py +160 -0
- taut_shape/log/_window.py +165 -0
- taut_shape/log/engine.py +322 -0
- taut_shape/log/messages.py +81 -0
- taut_shape/schemas/shape_atom.taut.py +202 -0
- taut_shape/schemas/shape_crdt.taut.py +89 -0
- taut_shape/schemas/shape_log.taut.py +177 -0
- taut_shape/schemas/shape_stream.taut.py +100 -0
- taut_shape/schemas/shape_swmr.taut.py +315 -0
- taut_shape/schemas/shape_value.taut.py +134 -0
- taut_shape/snapshot_delta.py +72 -0
- taut_shape/stream/__init__.py +5 -0
- taut_shape/stream/_generated.py +113 -0
- taut_shape/stream/engine.py +297 -0
- taut_shape/stream/wire.py +194 -0
- taut_shape/swmr/__init__.py +5 -0
- taut_shape/swmr/_generated.py +146 -0
- taut_shape/swmr/engine.py +419 -0
- taut_shape/swmr/wire.py +241 -0
- taut_shape/tool/__init__.py +11 -0
- taut_shape/tool/__main__.py +8 -0
- taut_shape/tool/atom_adapter.py +40 -0
- taut_shape/tool/crdt_adapter.py +29 -0
- taut_shape/tool/framing.py +134 -0
- taut_shape/tool/log_adapter.py +74 -0
- taut_shape/tool/main.py +988 -0
- taut_shape/tool/oraclefmt.py +136 -0
- taut_shape/tool/runtime.py +112 -0
- taut_shape/tool/stream_adapter.py +47 -0
- taut_shape/tool/swmr_adapter.py +52 -0
- taut_shape/tool/value_adapter.py +29 -0
- taut_shape/value/__init__.py +6 -0
- taut_shape/value/_generated.py +58 -0
- taut_shape/value/engine.py +67 -0
- taut_shape/value/wire.py +146 -0
- taut_shape-0.9.1.dist-info/METADATA +101 -0
- taut_shape-0.9.1.dist-info/RECORD +54 -0
- taut_shape-0.9.1.dist-info/WHEEL +5 -0
- taut_shape-0.9.1.dist-info/entry_points.txt +2 -0
- taut_shape-0.9.1.dist-info/licenses/LICENSE +21 -0
- taut_shape-0.9.1.dist-info/top_level.txt +1 -0
taut_shape/__init__.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""taut-shape (Python) — pure-Python delivery-shape engine (`log` first).
|
|
2
|
+
|
|
3
|
+
The public entry point is :class:`taut_shape.log.LogNode`. See ``README.md`` and
|
|
4
|
+
``dev-docs/InitialPlan.md``. The CLI tool (:mod:`taut_shape.tool`, Phase 4) and
|
|
5
|
+
its cross-language interop matrix participation (Phase 5) are built; Phase 3
|
|
6
|
+
(an ``asyncio`` shell) is not.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from .log import LogNode, StopWhen
|
|
12
|
+
from .value import ValueNode
|
|
13
|
+
from .atom import AtomNode
|
|
14
|
+
from .stream import StreamNode
|
|
15
|
+
from .swmr import SwmrNode
|
|
16
|
+
from .snapshot_delta import SnapshotDeltaNode
|
|
17
|
+
|
|
18
|
+
__all__ = [
|
|
19
|
+
"AtomNode",
|
|
20
|
+
"LogNode",
|
|
21
|
+
"SnapshotDeltaNode",
|
|
22
|
+
"StopWhen",
|
|
23
|
+
"StreamNode",
|
|
24
|
+
"SwmrNode",
|
|
25
|
+
"ValueNode",
|
|
26
|
+
]
|
taut_shape/_schema.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Load canonical shape schemas bundled in the installed distribution."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from importlib.resources import as_file, files
|
|
6
|
+
|
|
7
|
+
from taut.ir.load import load_schema
|
|
8
|
+
from taut.ir.model import Schema
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def load_packaged_schema(shape: str) -> Schema:
|
|
12
|
+
"""Load one bundled ``shape_<name>.taut.py`` schema.
|
|
13
|
+
|
|
14
|
+
``as_file`` also supports importers that do not expose package resources as
|
|
15
|
+
ordinary filesystem paths. The temporary path only needs to live for the
|
|
16
|
+
duration of ``load_schema`` because the returned model is self-contained.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
resource = files("taut_shape").joinpath("schemas", f"shape_{shape}.taut.py")
|
|
20
|
+
with as_file(resource) as path:
|
|
21
|
+
return load_schema(path)
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"""GENERATED native Python types — do not edit.
|
|
2
|
+
|
|
3
|
+
Source: taut-shape/ir/shape_atom.taut.py
|
|
4
|
+
Schema sha256: see taut-shape/ir/shape_atom.ir.json
|
|
5
|
+
Regenerate with ``tautc gen ... --api-only`` and replace this file.
|
|
6
|
+
"""
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from enum import Enum
|
|
10
|
+
|
|
11
|
+
class AtomMsgType(Enum):
|
|
12
|
+
replace = 0
|
|
13
|
+
seal = 1
|
|
14
|
+
close = 2
|
|
15
|
+
read = 3
|
|
16
|
+
end_stream = 4
|
|
17
|
+
timer_expired = 5
|
|
18
|
+
read_response = 6
|
|
19
|
+
set_timer = 7
|
|
20
|
+
cancel_timer = 8
|
|
21
|
+
producer_stop = 9
|
|
22
|
+
diagnostic = 10
|
|
23
|
+
|
|
24
|
+
class AtomState(Enum):
|
|
25
|
+
data = 0
|
|
26
|
+
would_block = 1
|
|
27
|
+
eof = 2
|
|
28
|
+
closed = 3
|
|
29
|
+
failed = 4
|
|
30
|
+
|
|
31
|
+
class AtomErrorCode(Enum):
|
|
32
|
+
unknown_atom = 0
|
|
33
|
+
producer_error = 1
|
|
34
|
+
internal = 2
|
|
35
|
+
|
|
36
|
+
class AtomStopReason(Enum):
|
|
37
|
+
last_reader_gone = 0
|
|
38
|
+
closed = 1
|
|
39
|
+
failed = 2
|
|
40
|
+
|
|
41
|
+
class AtomSeverity(Enum):
|
|
42
|
+
warn = 0
|
|
43
|
+
error = 1
|
|
44
|
+
|
|
45
|
+
class AtomDiagCode(Enum):
|
|
46
|
+
replace_after_terminal = 0
|
|
47
|
+
|
|
48
|
+
@dataclass(slots=True)
|
|
49
|
+
class AtomVersion:
|
|
50
|
+
version: int
|
|
51
|
+
|
|
52
|
+
@dataclass(slots=True)
|
|
53
|
+
class AtomValue:
|
|
54
|
+
version: int
|
|
55
|
+
payload: bytes
|
|
56
|
+
|
|
57
|
+
@dataclass(slots=True)
|
|
58
|
+
class AtomError:
|
|
59
|
+
code: AtomErrorCode
|
|
60
|
+
message: str | None
|
|
61
|
+
|
|
62
|
+
@dataclass(slots=True)
|
|
63
|
+
class AtomReplace:
|
|
64
|
+
payload: bytes
|
|
65
|
+
|
|
66
|
+
@dataclass(slots=True)
|
|
67
|
+
class AtomSeal:
|
|
68
|
+
pass
|
|
69
|
+
|
|
70
|
+
@dataclass(slots=True)
|
|
71
|
+
class AtomClose:
|
|
72
|
+
error: AtomError | None
|
|
73
|
+
|
|
74
|
+
@dataclass(slots=True)
|
|
75
|
+
class AtomReadRequest:
|
|
76
|
+
atom_id: str
|
|
77
|
+
stream_id: str
|
|
78
|
+
version: AtomVersion | None
|
|
79
|
+
timeout_ms: int | None
|
|
80
|
+
|
|
81
|
+
@dataclass(slots=True)
|
|
82
|
+
class AtomEndStream:
|
|
83
|
+
atom_id: str
|
|
84
|
+
stream_id: str
|
|
85
|
+
|
|
86
|
+
@dataclass(slots=True)
|
|
87
|
+
class AtomTimerExpired:
|
|
88
|
+
token: int
|
|
89
|
+
|
|
90
|
+
@dataclass(slots=True)
|
|
91
|
+
class AtomReadResponse:
|
|
92
|
+
atom_id: str
|
|
93
|
+
stream_id: str
|
|
94
|
+
value: AtomValue | None
|
|
95
|
+
next_version: AtomVersion
|
|
96
|
+
state: AtomState
|
|
97
|
+
error: AtomError | None
|
|
98
|
+
|
|
99
|
+
@dataclass(slots=True)
|
|
100
|
+
class AtomSetTimer:
|
|
101
|
+
token: int
|
|
102
|
+
ms: int
|
|
103
|
+
|
|
104
|
+
@dataclass(slots=True)
|
|
105
|
+
class AtomCancelTimer:
|
|
106
|
+
token: int
|
|
107
|
+
|
|
108
|
+
@dataclass(slots=True)
|
|
109
|
+
class AtomProducerStop:
|
|
110
|
+
reason: AtomStopReason
|
|
111
|
+
|
|
112
|
+
@dataclass(slots=True)
|
|
113
|
+
class AtomDiagnostic:
|
|
114
|
+
severity: AtomSeverity
|
|
115
|
+
code: AtomDiagCode
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"""Pure latest-state mailbox implementing ``atom.oracle/v1``."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
from ..log.engine import StopWhen
|
|
8
|
+
from . import _generated as g
|
|
9
|
+
|
|
10
|
+
AtomInput = (
|
|
11
|
+
g.AtomReplace
|
|
12
|
+
| g.AtomSeal
|
|
13
|
+
| g.AtomClose
|
|
14
|
+
| g.AtomReadRequest
|
|
15
|
+
| g.AtomEndStream
|
|
16
|
+
| g.AtomTimerExpired
|
|
17
|
+
)
|
|
18
|
+
AtomOutput = (
|
|
19
|
+
g.AtomReadResponse
|
|
20
|
+
| g.AtomSetTimer
|
|
21
|
+
| g.AtomCancelTimer
|
|
22
|
+
| g.AtomProducerStop
|
|
23
|
+
| g.AtomDiagnostic
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(slots=True)
|
|
28
|
+
class _Held:
|
|
29
|
+
atom_id: str
|
|
30
|
+
version: int
|
|
31
|
+
timeout_ms: int | None
|
|
32
|
+
timer_token: int | None
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class AtomNode:
|
|
36
|
+
"""One retained value with versioned reads, holds, timers, and lifecycle."""
|
|
37
|
+
|
|
38
|
+
def __init__(self, *, stop_when: StopWhen = StopWhen.LAST_READER) -> None:
|
|
39
|
+
self.stop_when = stop_when
|
|
40
|
+
self.version = 0
|
|
41
|
+
self.payload: bytes | None = None
|
|
42
|
+
self.sealed = False
|
|
43
|
+
self.closed = False
|
|
44
|
+
self.close_error: g.AtomError | None = None
|
|
45
|
+
self._streams: dict[str, _Held | None] = {}
|
|
46
|
+
self._order: list[str] = []
|
|
47
|
+
self._next_token = 1
|
|
48
|
+
|
|
49
|
+
def handle(self, message: AtomInput) -> list[AtomOutput]:
|
|
50
|
+
if isinstance(message, g.AtomReplace):
|
|
51
|
+
return self._replace(message)
|
|
52
|
+
if isinstance(message, g.AtomSeal):
|
|
53
|
+
return self._seal()
|
|
54
|
+
if isinstance(message, g.AtomClose):
|
|
55
|
+
return self._close(message)
|
|
56
|
+
if isinstance(message, g.AtomReadRequest):
|
|
57
|
+
return self._read(message)
|
|
58
|
+
if isinstance(message, g.AtomEndStream):
|
|
59
|
+
return self._end_stream(message)
|
|
60
|
+
if isinstance(message, g.AtomTimerExpired):
|
|
61
|
+
return self._timer_expired(message)
|
|
62
|
+
raise TypeError(f"unsupported atom input {type(message).__name__}")
|
|
63
|
+
|
|
64
|
+
def _replace(self, message: g.AtomReplace) -> list[AtomOutput]:
|
|
65
|
+
if self.sealed or self.closed:
|
|
66
|
+
return [
|
|
67
|
+
g.AtomDiagnostic(
|
|
68
|
+
severity=g.AtomSeverity.warn,
|
|
69
|
+
code=g.AtomDiagCode.replace_after_terminal,
|
|
70
|
+
)
|
|
71
|
+
]
|
|
72
|
+
self.version += 1
|
|
73
|
+
self.payload = bytes(message.payload)
|
|
74
|
+
return self._answer_all_held()
|
|
75
|
+
|
|
76
|
+
def _seal(self) -> list[AtomOutput]:
|
|
77
|
+
if self.sealed:
|
|
78
|
+
return []
|
|
79
|
+
self.sealed = True
|
|
80
|
+
return self._answer_all_held()
|
|
81
|
+
|
|
82
|
+
def _close(self, message: g.AtomClose) -> list[AtomOutput]:
|
|
83
|
+
if self.closed:
|
|
84
|
+
return []
|
|
85
|
+
self.closed = True
|
|
86
|
+
self.close_error = None if message.error is None else g.AtomError(
|
|
87
|
+
code=message.error.code,
|
|
88
|
+
message=message.error.message,
|
|
89
|
+
)
|
|
90
|
+
outputs = self._answer_all_held()
|
|
91
|
+
reason = (
|
|
92
|
+
g.AtomStopReason.failed
|
|
93
|
+
if self.close_error is not None
|
|
94
|
+
else g.AtomStopReason.closed
|
|
95
|
+
)
|
|
96
|
+
outputs.append(g.AtomProducerStop(reason=reason))
|
|
97
|
+
return outputs
|
|
98
|
+
|
|
99
|
+
def _ensure_stream(self, stream_id: str) -> None:
|
|
100
|
+
if stream_id not in self._streams:
|
|
101
|
+
self._streams[stream_id] = None
|
|
102
|
+
self._order.append(stream_id)
|
|
103
|
+
|
|
104
|
+
def _read(self, message: g.AtomReadRequest) -> list[AtomOutput]:
|
|
105
|
+
self._ensure_stream(message.stream_id)
|
|
106
|
+
outputs: list[AtomOutput] = []
|
|
107
|
+
prior = self._streams[message.stream_id]
|
|
108
|
+
if prior is not None and prior.timer_token is not None:
|
|
109
|
+
outputs.append(g.AtomCancelTimer(token=prior.timer_token))
|
|
110
|
+
self._streams[message.stream_id] = None
|
|
111
|
+
|
|
112
|
+
version = 0 if message.version is None else message.version.version
|
|
113
|
+
response = self._resolve(
|
|
114
|
+
message.atom_id,
|
|
115
|
+
message.stream_id,
|
|
116
|
+
version,
|
|
117
|
+
message.timeout_ms,
|
|
118
|
+
)
|
|
119
|
+
if response is not None:
|
|
120
|
+
outputs.append(response)
|
|
121
|
+
return outputs
|
|
122
|
+
|
|
123
|
+
token: int | None = None
|
|
124
|
+
if message.timeout_ms is not None and message.timeout_ms > 0:
|
|
125
|
+
token = self._next_token
|
|
126
|
+
self._next_token += 1
|
|
127
|
+
outputs.append(g.AtomSetTimer(token=token, ms=message.timeout_ms))
|
|
128
|
+
self._streams[message.stream_id] = _Held(
|
|
129
|
+
atom_id=message.atom_id,
|
|
130
|
+
version=version,
|
|
131
|
+
timeout_ms=message.timeout_ms,
|
|
132
|
+
timer_token=token,
|
|
133
|
+
)
|
|
134
|
+
return outputs
|
|
135
|
+
|
|
136
|
+
def _resolve(
|
|
137
|
+
self,
|
|
138
|
+
atom_id: str,
|
|
139
|
+
stream_id: str,
|
|
140
|
+
version: int,
|
|
141
|
+
timeout_ms: int | None,
|
|
142
|
+
) -> g.AtomReadResponse | None:
|
|
143
|
+
if version < self.version:
|
|
144
|
+
assert self.payload is not None
|
|
145
|
+
return g.AtomReadResponse(
|
|
146
|
+
atom_id=atom_id,
|
|
147
|
+
stream_id=stream_id,
|
|
148
|
+
value=g.AtomValue(version=self.version, payload=self.payload),
|
|
149
|
+
next_version=g.AtomVersion(version=self.version),
|
|
150
|
+
state=g.AtomState.data,
|
|
151
|
+
error=None,
|
|
152
|
+
)
|
|
153
|
+
next_version = g.AtomVersion(version=self.version)
|
|
154
|
+
if self.closed:
|
|
155
|
+
return g.AtomReadResponse(
|
|
156
|
+
atom_id=atom_id,
|
|
157
|
+
stream_id=stream_id,
|
|
158
|
+
value=None,
|
|
159
|
+
next_version=next_version,
|
|
160
|
+
state=(
|
|
161
|
+
g.AtomState.failed
|
|
162
|
+
if self.close_error is not None
|
|
163
|
+
else g.AtomState.closed
|
|
164
|
+
),
|
|
165
|
+
error=self.close_error,
|
|
166
|
+
)
|
|
167
|
+
if self.sealed:
|
|
168
|
+
return g.AtomReadResponse(
|
|
169
|
+
atom_id=atom_id,
|
|
170
|
+
stream_id=stream_id,
|
|
171
|
+
value=None,
|
|
172
|
+
next_version=next_version,
|
|
173
|
+
state=g.AtomState.eof,
|
|
174
|
+
error=None,
|
|
175
|
+
)
|
|
176
|
+
if timeout_ms == 0:
|
|
177
|
+
return g.AtomReadResponse(
|
|
178
|
+
atom_id=atom_id,
|
|
179
|
+
stream_id=stream_id,
|
|
180
|
+
value=None,
|
|
181
|
+
next_version=next_version,
|
|
182
|
+
state=g.AtomState.would_block,
|
|
183
|
+
error=None,
|
|
184
|
+
)
|
|
185
|
+
return None
|
|
186
|
+
|
|
187
|
+
def _answer_all_held(self) -> list[AtomOutput]:
|
|
188
|
+
outputs: list[AtomOutput] = []
|
|
189
|
+
for stream_id in self._order:
|
|
190
|
+
held = self._streams.get(stream_id)
|
|
191
|
+
if held is None:
|
|
192
|
+
continue
|
|
193
|
+
response = self._resolve(
|
|
194
|
+
held.atom_id,
|
|
195
|
+
stream_id,
|
|
196
|
+
held.version,
|
|
197
|
+
held.timeout_ms,
|
|
198
|
+
)
|
|
199
|
+
if response is None:
|
|
200
|
+
continue
|
|
201
|
+
if held.timer_token is not None:
|
|
202
|
+
outputs.append(g.AtomCancelTimer(token=held.timer_token))
|
|
203
|
+
outputs.append(response)
|
|
204
|
+
self._streams[stream_id] = None
|
|
205
|
+
return outputs
|
|
206
|
+
|
|
207
|
+
def _end_stream(self, message: g.AtomEndStream) -> list[AtomOutput]:
|
|
208
|
+
if message.stream_id not in self._streams:
|
|
209
|
+
return []
|
|
210
|
+
held = self._streams.pop(message.stream_id)
|
|
211
|
+
self._order.remove(message.stream_id)
|
|
212
|
+
outputs: list[AtomOutput] = []
|
|
213
|
+
if held is not None and held.timer_token is not None:
|
|
214
|
+
outputs.append(g.AtomCancelTimer(token=held.timer_token))
|
|
215
|
+
if not self._streams and self.stop_when is StopWhen.LAST_READER:
|
|
216
|
+
outputs.append(
|
|
217
|
+
g.AtomProducerStop(reason=g.AtomStopReason.last_reader_gone)
|
|
218
|
+
)
|
|
219
|
+
return outputs
|
|
220
|
+
|
|
221
|
+
def _timer_expired(self, message: g.AtomTimerExpired) -> list[AtomOutput]:
|
|
222
|
+
for stream_id, held in self._streams.items():
|
|
223
|
+
if held is None or held.timer_token != message.token:
|
|
224
|
+
continue
|
|
225
|
+
self._streams[stream_id] = None
|
|
226
|
+
response = self._resolve(held.atom_id, stream_id, held.version, 0)
|
|
227
|
+
assert response is not None
|
|
228
|
+
return [response]
|
|
229
|
+
return []
|
taut_shape/atom/wire.py
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"""Atom message codec and jsoncodec bridge over the canonical Taut schema."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any, BinaryIO, cast
|
|
6
|
+
|
|
7
|
+
from taut.wire import codec, jsoncodec
|
|
8
|
+
|
|
9
|
+
from .._schema import load_packaged_schema
|
|
10
|
+
from ..tool import framing
|
|
11
|
+
from . import _generated as g
|
|
12
|
+
from .engine import AtomInput, AtomOutput
|
|
13
|
+
|
|
14
|
+
_SCHEMA = load_packaged_schema("atom")
|
|
15
|
+
_TYPE_TO_MESSAGE = {
|
|
16
|
+
"replace": "AtomReplace",
|
|
17
|
+
"seal": "AtomSeal",
|
|
18
|
+
"close": "AtomClose",
|
|
19
|
+
"read": "AtomReadRequest",
|
|
20
|
+
"end_stream": "AtomEndStream",
|
|
21
|
+
"timer_expired": "AtomTimerExpired",
|
|
22
|
+
"read_response": "AtomReadResponse",
|
|
23
|
+
"set_timer": "AtomSetTimer",
|
|
24
|
+
"cancel_timer": "AtomCancelTimer",
|
|
25
|
+
"producer_stop": "AtomProducerStop",
|
|
26
|
+
"diagnostic": "AtomDiagnostic",
|
|
27
|
+
}
|
|
28
|
+
_INPUT_NAMES = frozenset(
|
|
29
|
+
{"replace", "seal", "close", "read", "end_stream", "timer_expired"}
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _error(native: dict[str, Any] | None) -> g.AtomError | None:
|
|
34
|
+
if native is None:
|
|
35
|
+
return None
|
|
36
|
+
return g.AtomError(
|
|
37
|
+
code=g.AtomErrorCode[native["code"]],
|
|
38
|
+
message=native["message"],
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _input(message_type: str, native: dict[str, Any]) -> AtomInput:
|
|
43
|
+
if message_type == "replace":
|
|
44
|
+
return g.AtomReplace(payload=native["payload"])
|
|
45
|
+
if message_type == "seal":
|
|
46
|
+
return g.AtomSeal()
|
|
47
|
+
if message_type == "close":
|
|
48
|
+
return g.AtomClose(error=_error(native["error"]))
|
|
49
|
+
if message_type == "read":
|
|
50
|
+
version = native["version"]
|
|
51
|
+
return g.AtomReadRequest(
|
|
52
|
+
atom_id=native["atom_id"],
|
|
53
|
+
stream_id=native["stream_id"],
|
|
54
|
+
version=None if version is None else g.AtomVersion(**version),
|
|
55
|
+
timeout_ms=native["timeout_ms"],
|
|
56
|
+
)
|
|
57
|
+
if message_type == "end_stream":
|
|
58
|
+
return g.AtomEndStream(
|
|
59
|
+
atom_id=native["atom_id"],
|
|
60
|
+
stream_id=native["stream_id"],
|
|
61
|
+
)
|
|
62
|
+
if message_type == "timer_expired":
|
|
63
|
+
return g.AtomTimerExpired(token=native["token"])
|
|
64
|
+
raise ValueError(f"output-only atom message {message_type!r}")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _output(message_type: str, native: dict[str, Any]) -> AtomOutput:
|
|
68
|
+
if message_type == "read_response":
|
|
69
|
+
value = native["value"]
|
|
70
|
+
return g.AtomReadResponse(
|
|
71
|
+
atom_id=native["atom_id"],
|
|
72
|
+
stream_id=native["stream_id"],
|
|
73
|
+
value=None if value is None else g.AtomValue(**value),
|
|
74
|
+
next_version=g.AtomVersion(**native["next_version"]),
|
|
75
|
+
state=g.AtomState[native["state"]],
|
|
76
|
+
error=_error(native["error"]),
|
|
77
|
+
)
|
|
78
|
+
if message_type == "set_timer":
|
|
79
|
+
return g.AtomSetTimer(token=native["token"], ms=native["ms"])
|
|
80
|
+
if message_type == "cancel_timer":
|
|
81
|
+
return g.AtomCancelTimer(token=native["token"])
|
|
82
|
+
if message_type == "producer_stop":
|
|
83
|
+
return g.AtomProducerStop(reason=g.AtomStopReason[native["reason"]])
|
|
84
|
+
if message_type == "diagnostic":
|
|
85
|
+
return g.AtomDiagnostic(
|
|
86
|
+
severity=g.AtomSeverity[native["severity"]],
|
|
87
|
+
code=g.AtomDiagCode[native["code"]],
|
|
88
|
+
)
|
|
89
|
+
raise ValueError(f"input-only atom message {message_type!r}")
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _output_native(message: AtomOutput) -> tuple[str, dict[str, Any]]:
|
|
93
|
+
if isinstance(message, g.AtomReadResponse):
|
|
94
|
+
return "read_response", {
|
|
95
|
+
"atom_id": message.atom_id,
|
|
96
|
+
"stream_id": message.stream_id,
|
|
97
|
+
"value": None
|
|
98
|
+
if message.value is None
|
|
99
|
+
else {"version": message.value.version, "payload": message.value.payload},
|
|
100
|
+
"next_version": {"version": message.next_version.version},
|
|
101
|
+
"state": message.state.name,
|
|
102
|
+
"error": None
|
|
103
|
+
if message.error is None
|
|
104
|
+
else {"code": message.error.code.name, "message": message.error.message},
|
|
105
|
+
}
|
|
106
|
+
if isinstance(message, g.AtomSetTimer):
|
|
107
|
+
return "set_timer", {"token": message.token, "ms": message.ms}
|
|
108
|
+
if isinstance(message, g.AtomCancelTimer):
|
|
109
|
+
return "cancel_timer", {"token": message.token}
|
|
110
|
+
if isinstance(message, g.AtomProducerStop):
|
|
111
|
+
return "producer_stop", {"reason": message.reason.name}
|
|
112
|
+
return "diagnostic", {
|
|
113
|
+
"severity": message.severity.name,
|
|
114
|
+
"code": message.code.name,
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def decode_input(frame: framing.Frame) -> AtomInput:
|
|
119
|
+
try:
|
|
120
|
+
message_type = g.AtomMsgType(frame.tag).name
|
|
121
|
+
except ValueError as error:
|
|
122
|
+
raise framing.UnknownMessageTagError(frame.tag) from error
|
|
123
|
+
if message_type not in _INPUT_NAMES:
|
|
124
|
+
raise framing.FrameError(
|
|
125
|
+
f"output-only tag {message_type} on the atom input channel"
|
|
126
|
+
)
|
|
127
|
+
try:
|
|
128
|
+
native = codec.decode(_SCHEMA, _TYPE_TO_MESSAGE[message_type], frame.body)
|
|
129
|
+
return _input(message_type, native)
|
|
130
|
+
except framing.FrameError:
|
|
131
|
+
raise
|
|
132
|
+
except Exception as error:
|
|
133
|
+
raise framing.FrameError(f"malformed atom frame body: {error}") from error
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def encode_output(message: AtomOutput) -> tuple[int, bytes]:
|
|
137
|
+
message_type, native = _output_native(message)
|
|
138
|
+
return (
|
|
139
|
+
g.AtomMsgType[message_type].value,
|
|
140
|
+
codec.encode(_SCHEMA, _TYPE_TO_MESSAGE[message_type], native),
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def decode_output(frame: framing.Frame) -> AtomOutput:
|
|
145
|
+
try:
|
|
146
|
+
message_type = g.AtomMsgType(frame.tag).name
|
|
147
|
+
except ValueError as error:
|
|
148
|
+
raise framing.UnknownMessageTagError(frame.tag) from error
|
|
149
|
+
if message_type in _INPUT_NAMES:
|
|
150
|
+
raise framing.FrameError(
|
|
151
|
+
f"input-only tag {message_type} on the atom response channel"
|
|
152
|
+
)
|
|
153
|
+
try:
|
|
154
|
+
native = codec.decode(_SCHEMA, _TYPE_TO_MESSAGE[message_type], frame.body)
|
|
155
|
+
return _output(message_type, native)
|
|
156
|
+
except framing.FrameError:
|
|
157
|
+
raise
|
|
158
|
+
except Exception as error:
|
|
159
|
+
raise framing.FrameError(f"malformed atom frame body: {error}") from error
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def write_output(stream: BinaryIO, message: AtomOutput) -> None:
|
|
163
|
+
tag, body = encode_output(message)
|
|
164
|
+
framing.write_frame(stream, tag, body)
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def write_read(stream: BinaryIO, message: g.AtomReadRequest) -> None:
|
|
168
|
+
native = {
|
|
169
|
+
"atom_id": message.atom_id,
|
|
170
|
+
"stream_id": message.stream_id,
|
|
171
|
+
"version": None
|
|
172
|
+
if message.version is None
|
|
173
|
+
else {"version": message.version.version},
|
|
174
|
+
"timeout_ms": message.timeout_ms,
|
|
175
|
+
}
|
|
176
|
+
body = codec.encode(_SCHEMA, "AtomReadRequest", native)
|
|
177
|
+
framing.write_frame(stream, g.AtomMsgType.read.value, body)
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def json_to_input(message: dict[str, Any]) -> AtomInput:
|
|
181
|
+
message_type = str(message["type"])
|
|
182
|
+
if message_type not in _INPUT_NAMES:
|
|
183
|
+
raise ValueError(f"unknown atom input {message_type!r}")
|
|
184
|
+
schema_name = _TYPE_TO_MESSAGE[message_type]
|
|
185
|
+
native = jsoncodec.from_json_value(
|
|
186
|
+
_SCHEMA,
|
|
187
|
+
schema_name,
|
|
188
|
+
{key: value for key, value in message.items() if key != "type"},
|
|
189
|
+
)
|
|
190
|
+
return _input(message_type, cast(dict[str, Any], native))
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def output_to_json(message: AtomOutput) -> dict[str, Any]:
|
|
194
|
+
message_type, native = _output_native(message)
|
|
195
|
+
return {
|
|
196
|
+
"type": message_type,
|
|
197
|
+
**jsoncodec.to_json_value(
|
|
198
|
+
_SCHEMA,
|
|
199
|
+
_TYPE_TO_MESSAGE[message_type],
|
|
200
|
+
native,
|
|
201
|
+
),
|
|
202
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""Public CRDT delivery engine, wire types, and text specialization."""
|
|
2
|
+
|
|
3
|
+
from . import _generated
|
|
4
|
+
from .engine import CrdtInput, CrdtNode, CrdtOutput
|
|
5
|
+
from .text import TextProjection, encode_text_delete, encode_text_insert, project_text
|
|
6
|
+
|
|
7
|
+
__all__ = ["CrdtInput", "CrdtNode", "CrdtOutput", "TextProjection", "encode_text_delete", "encode_text_insert", "project_text", "_generated"]
|