hikari-risa 1.0.0__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.
- hikari_risa-1.0.0.dist-info/METADATA +83 -0
- hikari_risa-1.0.0.dist-info/RECORD +43 -0
- hikari_risa-1.0.0.dist-info/WHEEL +4 -0
- hikari_risa-1.0.0.dist-info/entry_points.txt +2 -0
- hikari_risa-1.0.0.dist-info/licenses/LICENSE +21 -0
- risa/__init__.py +49 -0
- risa/__main__.py +145 -0
- risa/_about.py +31 -0
- risa/binding.py +251 -0
- risa/client.py +657 -0
- risa/codecs.py +204 -0
- risa/context.py +693 -0
- risa/di.py +75 -0
- risa/dispatch.py +828 -0
- risa/errors.py +603 -0
- risa/internal/__init__.py +22 -0
- risa/internal/codec.py +547 -0
- risa/internal/constants.py +36 -0
- risa/internal/leases.py +197 -0
- risa/internal/locks.py +87 -0
- risa/internal/reader.py +88 -0
- risa/internal/registry.py +208 -0
- risa/internal/wire.py +118 -0
- risa/py.typed +0 -0
- risa/state/__init__.py +25 -0
- risa/state/anchor.py +279 -0
- risa/state/backend.py +155 -0
- risa/state/message.py +278 -0
- risa/state/placement.py +104 -0
- risa/state/schema.py +447 -0
- risa/state/serde.py +69 -0
- risa/state/stateless.py +94 -0
- risa/state/stored.py +444 -0
- risa/store/__init__.py +30 -0
- risa/store/memory.py +152 -0
- risa/store/postgres.py +311 -0
- risa/store/protocol.py +172 -0
- risa/store/redis.py +254 -0
- risa/store/testing.py +341 -0
- risa/ui/__init__.py +26 -0
- risa/ui/build.py +353 -0
- risa/ui/nodes.py +918 -0
- risa/view.py +466 -0
risa/codecs.py
ADDED
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# Copyright (c) 2025 Rayakame
|
|
2
|
+
#
|
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
# furnished to do so, subject to the following conditions:
|
|
9
|
+
#
|
|
10
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
11
|
+
# copies or substantial portions of the Software.
|
|
12
|
+
#
|
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
19
|
+
# SOFTWARE.
|
|
20
|
+
"""How `InMessage` state is written into `custom_id` characters.
|
|
21
|
+
|
|
22
|
+
`BASE85`, the default, is built in and keeps every character printable ASCII.
|
|
23
|
+
`BASE32768` and `URO14` come with the `[radixly]` extra and carry more than twice as
|
|
24
|
+
many bytes per character.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
from __future__ import annotations
|
|
28
|
+
|
|
29
|
+
import base64
|
|
30
|
+
import functools
|
|
31
|
+
import importlib
|
|
32
|
+
import typing
|
|
33
|
+
|
|
34
|
+
import msgspec
|
|
35
|
+
|
|
36
|
+
from risa import errors
|
|
37
|
+
from risa.internal import wire
|
|
38
|
+
|
|
39
|
+
if typing.TYPE_CHECKING:
|
|
40
|
+
import collections.abc
|
|
41
|
+
|
|
42
|
+
__all__ = (
|
|
43
|
+
"BASE85",
|
|
44
|
+
"BASE32768",
|
|
45
|
+
"RADIXLY_EXTRA",
|
|
46
|
+
"URO14",
|
|
47
|
+
"Codec",
|
|
48
|
+
"by_tag",
|
|
49
|
+
"ensure_available",
|
|
50
|
+
"register",
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
RADIXLY_EXTRA: typing.Final[str] = "radixly"
|
|
54
|
+
"""The name of the extra (`hikari-risa[radixly]`) that `BASE32768` and `URO14` need."""
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class _RadixlyModule(typing.Protocol):
|
|
58
|
+
def encode(self, data: bytes, /) -> str: ...
|
|
59
|
+
|
|
60
|
+
def decode(self, text: str, /) -> bytes: ...
|
|
61
|
+
|
|
62
|
+
def max_bytes(self, chars: int, /) -> int: ...
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class Codec(msgspec.Struct, frozen=True):
|
|
66
|
+
"""A way of writing bytes as `custom_id` characters.
|
|
67
|
+
|
|
68
|
+
Attributes
|
|
69
|
+
----------
|
|
70
|
+
tag
|
|
71
|
+
One character, written ahead of the state, that names the codec on the wire.
|
|
72
|
+
name
|
|
73
|
+
A human-readable name for messages and logs.
|
|
74
|
+
encode
|
|
75
|
+
Turns bytes into text.
|
|
76
|
+
decode
|
|
77
|
+
Turns that text back into the same bytes.
|
|
78
|
+
max_bytes
|
|
79
|
+
How many bytes fit into a given number of characters.
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
tag: str
|
|
83
|
+
name: str
|
|
84
|
+
encode: collections.abc.Callable[[bytes], str]
|
|
85
|
+
decode: collections.abc.Callable[[str], bytes]
|
|
86
|
+
max_bytes: collections.abc.Callable[[int], int]
|
|
87
|
+
|
|
88
|
+
def __post_init__(self) -> None:
|
|
89
|
+
if len(self.tag) != 1 or self.tag not in wire.ALPHABET:
|
|
90
|
+
msg = f"a codec tag is exactly one character of the wire alphabet, got {self.tag!r}"
|
|
91
|
+
raise ValueError(msg)
|
|
92
|
+
if not self.name:
|
|
93
|
+
msg = "a codec needs a name"
|
|
94
|
+
raise ValueError(msg)
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _b85_encode(data: bytes) -> str:
|
|
98
|
+
return base64.b85encode(data).decode("ascii")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _b85_decode(text: str) -> bytes:
|
|
102
|
+
if len(text) % 5 == 1:
|
|
103
|
+
msg = f"{len(text)} characters is not a base85 length"
|
|
104
|
+
raise ValueError(msg)
|
|
105
|
+
return base64.b85decode(text)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _b85_max_bytes(chars: int) -> int:
|
|
109
|
+
return chars // 5 * 4 + max(0, chars % 5 - 1)
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def _radixly_module(name: str) -> _RadixlyModule:
|
|
113
|
+
try:
|
|
114
|
+
module = importlib.import_module(f"radixly.{name}")
|
|
115
|
+
except ImportError as exc:
|
|
116
|
+
raise errors.CodecUnavailableError(name, RADIXLY_EXTRA) from exc
|
|
117
|
+
return typing.cast("_RadixlyModule", module)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def _radixly_encode(name: str, data: bytes) -> str:
|
|
121
|
+
return _radixly_module(name).encode(data)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _radixly_decode(name: str, text: str) -> bytes:
|
|
125
|
+
return _radixly_module(name).decode(text)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _radixly_max_bytes(name: str, chars: int) -> int:
|
|
129
|
+
return _radixly_module(name).max_bytes(chars)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _radixly(tag: str, name: str) -> Codec:
|
|
133
|
+
return Codec(
|
|
134
|
+
tag=tag,
|
|
135
|
+
name=name,
|
|
136
|
+
encode=functools.partial(_radixly_encode, name),
|
|
137
|
+
decode=functools.partial(_radixly_decode, name),
|
|
138
|
+
max_bytes=functools.partial(_radixly_max_bytes, name),
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
BASE85: typing.Final[Codec] = Codec(
|
|
143
|
+
tag="a", name="base85", encode=_b85_encode, decode=_b85_decode, max_bytes=_b85_max_bytes
|
|
144
|
+
)
|
|
145
|
+
"""The default codec: printable ASCII, built in, needs no extra."""
|
|
146
|
+
BASE32768: typing.Final[Codec] = _radixly("w", "base32768")
|
|
147
|
+
"""15 bits per character, the densest codec; needs the `[radixly]` extra."""
|
|
148
|
+
URO14: typing.Final[Codec] = _radixly("u", "uro14")
|
|
149
|
+
"""14 bits per CJK character behind a length prefix; needs the `[radixly]` extra."""
|
|
150
|
+
|
|
151
|
+
_REGISTRY: typing.Final[dict[str, Codec]] = {c.tag: c for c in (BASE85, BASE32768, URO14)}
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def register(codec: Codec) -> None:
|
|
155
|
+
"""Make `codec` known under its tag, so state written with it can be read back.
|
|
156
|
+
|
|
157
|
+
Parameters
|
|
158
|
+
----------
|
|
159
|
+
codec
|
|
160
|
+
The codec to register, under its one-character `tag`.
|
|
161
|
+
|
|
162
|
+
Raises
|
|
163
|
+
------
|
|
164
|
+
ValueError
|
|
165
|
+
If a different codec already has the tag.
|
|
166
|
+
"""
|
|
167
|
+
existing = _REGISTRY.get(codec.tag)
|
|
168
|
+
if existing is None:
|
|
169
|
+
_REGISTRY[codec.tag] = codec
|
|
170
|
+
elif existing != codec:
|
|
171
|
+
msg = f"codec tag {codec.tag!r} already belongs to {existing.name!r}"
|
|
172
|
+
raise ValueError(msg)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def by_tag(tag: str) -> Codec | None:
|
|
176
|
+
"""Return the codec registered under `tag`, or `None`.
|
|
177
|
+
|
|
178
|
+
Parameters
|
|
179
|
+
----------
|
|
180
|
+
tag
|
|
181
|
+
The one-character tag a codec writes ahead of its state.
|
|
182
|
+
|
|
183
|
+
Returns
|
|
184
|
+
-------
|
|
185
|
+
:
|
|
186
|
+
The codec, or `None` if no codec has the tag.
|
|
187
|
+
"""
|
|
188
|
+
return _REGISTRY.get(tag)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def ensure_available(codec: Codec) -> None:
|
|
192
|
+
"""Raise `CodecUnavailableError` if `codec` needs an extra that is not installed.
|
|
193
|
+
|
|
194
|
+
Parameters
|
|
195
|
+
----------
|
|
196
|
+
codec
|
|
197
|
+
The codec to check. `BASE85` always passes.
|
|
198
|
+
|
|
199
|
+
Raises
|
|
200
|
+
------
|
|
201
|
+
CodecUnavailableError
|
|
202
|
+
If the codec needs the `[radixly]` extra and it is not installed.
|
|
203
|
+
"""
|
|
204
|
+
codec.max_bytes(1)
|