dbus-fast 2.46.1__cp310-cp310-musllinux_1_2_aarch64.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.
- dbus_fast/__init__.py +82 -0
- dbus_fast/__version__.py +10 -0
- dbus_fast/_private/__init__.py +1 -0
- dbus_fast/_private/_cython_compat.py +14 -0
- dbus_fast/_private/address.cpython-310-aarch64-linux-gnu.so +0 -0
- dbus_fast/_private/address.pxd +15 -0
- dbus_fast/_private/address.py +117 -0
- dbus_fast/_private/constants.py +20 -0
- dbus_fast/_private/marshaller.cpython-310-aarch64-linux-gnu.so +0 -0
- dbus_fast/_private/marshaller.pxd +110 -0
- dbus_fast/_private/marshaller.py +229 -0
- dbus_fast/_private/unmarshaller.cpython-310-aarch64-linux-gnu.so +0 -0
- dbus_fast/_private/unmarshaller.pxd +261 -0
- dbus_fast/_private/unmarshaller.py +902 -0
- dbus_fast/_private/util.py +177 -0
- dbus_fast/aio/__init__.py +5 -0
- dbus_fast/aio/message_bus.py +579 -0
- dbus_fast/aio/message_reader.cpython-310-aarch64-linux-gnu.so +0 -0
- dbus_fast/aio/message_reader.pxd +13 -0
- dbus_fast/aio/message_reader.py +49 -0
- dbus_fast/aio/proxy_object.py +207 -0
- dbus_fast/auth.py +125 -0
- dbus_fast/constants.py +152 -0
- dbus_fast/errors.py +81 -0
- dbus_fast/glib/__init__.py +3 -0
- dbus_fast/glib/message_bus.py +513 -0
- dbus_fast/glib/proxy_object.py +318 -0
- dbus_fast/introspection.py +682 -0
- dbus_fast/message.cpython-310-aarch64-linux-gnu.so +0 -0
- dbus_fast/message.pxd +76 -0
- dbus_fast/message.py +387 -0
- dbus_fast/message_bus.cpython-310-aarch64-linux-gnu.so +0 -0
- dbus_fast/message_bus.pxd +75 -0
- dbus_fast/message_bus.py +1326 -0
- dbus_fast/proxy_object.py +357 -0
- dbus_fast/py.typed +0 -0
- dbus_fast/send_reply.py +61 -0
- dbus_fast/service.cpython-310-aarch64-linux-gnu.so +0 -0
- dbus_fast/service.pxd +50 -0
- dbus_fast/service.py +699 -0
- dbus_fast/signature.cpython-310-aarch64-linux-gnu.so +0 -0
- dbus_fast/signature.pxd +31 -0
- dbus_fast/signature.py +482 -0
- dbus_fast/unpack.cpython-310-aarch64-linux-gnu.so +0 -0
- dbus_fast/unpack.pxd +13 -0
- dbus_fast/unpack.py +24 -0
- dbus_fast/validators.py +199 -0
- dbus_fast-2.46.1.dist-info/METADATA +262 -0
- dbus_fast-2.46.1.dist-info/RECORD +51 -0
- dbus_fast-2.46.1.dist-info/WHEEL +4 -0
- dbus_fast-2.46.1.dist-info/licenses/LICENSE +22 -0
dbus_fast/signature.pxd
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"""cdefs for signature.py"""
|
|
2
|
+
|
|
3
|
+
import cython
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
cdef class SignatureType:
|
|
7
|
+
|
|
8
|
+
cdef public str token
|
|
9
|
+
cdef public unsigned int token_as_int
|
|
10
|
+
cdef public list children
|
|
11
|
+
cdef str _signature
|
|
12
|
+
cdef public SignatureType _child_0
|
|
13
|
+
cdef public SignatureType _child_1
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
cdef class SignatureTree:
|
|
17
|
+
|
|
18
|
+
cdef public str signature
|
|
19
|
+
cdef public list types
|
|
20
|
+
cdef public SignatureType root_type
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
cdef class Variant:
|
|
24
|
+
|
|
25
|
+
cdef public SignatureType type
|
|
26
|
+
cdef public str signature
|
|
27
|
+
cdef public object value
|
|
28
|
+
|
|
29
|
+
@cython.locals(self=Variant)
|
|
30
|
+
@staticmethod
|
|
31
|
+
cdef Variant _factory(SignatureTree signature_tree, object value)
|
dbus_fast/signature.py
ADDED
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
2
|
+
from functools import lru_cache
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from .errors import InvalidSignatureError, SignatureBodyMismatchError
|
|
6
|
+
from .validators import is_object_path_valid
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SignatureType: # noqa: PLW1641
|
|
10
|
+
"""A class that represents a single complete type within a signature.
|
|
11
|
+
|
|
12
|
+
This class is not meant to be constructed directly. Use the :class:`SignatureTree`
|
|
13
|
+
class to parse signatures.
|
|
14
|
+
|
|
15
|
+
:ivar ~.signature: The signature of this complete type.
|
|
16
|
+
:vartype ~.signature: str
|
|
17
|
+
|
|
18
|
+
:ivar children: A list of child types if this is a container type. Arrays \
|
|
19
|
+
have one child type, dict entries have two child types (key and value), and \
|
|
20
|
+
structs have child types equal to the number of struct members.
|
|
21
|
+
:vartype children: list(:class:`SignatureType`)
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
_tokens = "ybnqiuxtdsogavh({"
|
|
25
|
+
__slots__ = (
|
|
26
|
+
"_child_0",
|
|
27
|
+
"_child_1",
|
|
28
|
+
"_signature",
|
|
29
|
+
"children",
|
|
30
|
+
"token",
|
|
31
|
+
"token_as_int",
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
def __init__(self, token: str) -> None:
|
|
35
|
+
"""Init a new SignatureType."""
|
|
36
|
+
self.token: str = token
|
|
37
|
+
self.token_as_int = ord(token)
|
|
38
|
+
self.children: list[SignatureType] = []
|
|
39
|
+
self._child_0: SignatureType | None = None
|
|
40
|
+
self._child_1: SignatureType | None = None
|
|
41
|
+
self._signature: str | None = None
|
|
42
|
+
|
|
43
|
+
def __eq__(self, other: object) -> bool:
|
|
44
|
+
"""Compare this type to another type or signature string."""
|
|
45
|
+
if type(other) is SignatureType:
|
|
46
|
+
return self.signature == other.signature
|
|
47
|
+
return super().__eq__(other)
|
|
48
|
+
|
|
49
|
+
def _collapse(self) -> str:
|
|
50
|
+
"""Collapse this type into a signature string."""
|
|
51
|
+
if self.token not in "a({":
|
|
52
|
+
return self.token
|
|
53
|
+
|
|
54
|
+
signature = [self.token]
|
|
55
|
+
|
|
56
|
+
for child in self.children:
|
|
57
|
+
signature.append(child._collapse())
|
|
58
|
+
|
|
59
|
+
if self.token == "(":
|
|
60
|
+
signature.append(")")
|
|
61
|
+
elif self.token == "{":
|
|
62
|
+
signature.append("}")
|
|
63
|
+
|
|
64
|
+
return "".join(signature)
|
|
65
|
+
|
|
66
|
+
@property
|
|
67
|
+
def signature(self) -> str:
|
|
68
|
+
if self._signature is not None:
|
|
69
|
+
return self._signature
|
|
70
|
+
self._signature = self._collapse()
|
|
71
|
+
return self._signature
|
|
72
|
+
|
|
73
|
+
def _add_child(self, child: "SignatureType") -> None:
|
|
74
|
+
"""Add a child type to this type.
|
|
75
|
+
|
|
76
|
+
:param child: The child type to add.
|
|
77
|
+
:type child: :class:`SignatureType`
|
|
78
|
+
"""
|
|
79
|
+
if self._child_0 is None:
|
|
80
|
+
self._child_0 = child
|
|
81
|
+
elif self._child_1 is None:
|
|
82
|
+
self._child_1 = child
|
|
83
|
+
self.children.append(child)
|
|
84
|
+
|
|
85
|
+
@staticmethod
|
|
86
|
+
def _parse_next(signature: str) -> tuple["SignatureType", str]:
|
|
87
|
+
if not signature:
|
|
88
|
+
raise InvalidSignatureError("Cannot parse an empty signature")
|
|
89
|
+
|
|
90
|
+
token = signature[0]
|
|
91
|
+
|
|
92
|
+
if token not in SignatureType._tokens:
|
|
93
|
+
raise InvalidSignatureError(f'got unexpected token: "{token}"')
|
|
94
|
+
|
|
95
|
+
# container types
|
|
96
|
+
if token == "a":
|
|
97
|
+
self = SignatureType("a")
|
|
98
|
+
(child, signature) = SignatureType._parse_next(signature[1:])
|
|
99
|
+
if not child:
|
|
100
|
+
raise InvalidSignatureError("missing type for array")
|
|
101
|
+
self._add_child(child)
|
|
102
|
+
return (self, signature)
|
|
103
|
+
if token == "(":
|
|
104
|
+
self = SignatureType("(")
|
|
105
|
+
signature = signature[1:]
|
|
106
|
+
while True:
|
|
107
|
+
(child, signature) = SignatureType._parse_next(signature)
|
|
108
|
+
if not signature:
|
|
109
|
+
raise InvalidSignatureError('missing closing ")" for struct')
|
|
110
|
+
self._add_child(child)
|
|
111
|
+
if signature[0] == ")":
|
|
112
|
+
return (self, signature[1:])
|
|
113
|
+
elif token == "{":
|
|
114
|
+
self = SignatureType("{")
|
|
115
|
+
signature = signature[1:]
|
|
116
|
+
(key_child, signature) = SignatureType._parse_next(signature)
|
|
117
|
+
if not key_child or len(key_child.children):
|
|
118
|
+
raise InvalidSignatureError("expected a simple type for dict entry key")
|
|
119
|
+
self._add_child(key_child)
|
|
120
|
+
(value_child, signature) = SignatureType._parse_next(signature)
|
|
121
|
+
if not value_child:
|
|
122
|
+
raise InvalidSignatureError("expected a value for dict entry")
|
|
123
|
+
if not signature or signature[0] != "}":
|
|
124
|
+
raise InvalidSignatureError('missing closing "}" for dict entry')
|
|
125
|
+
self._add_child(value_child)
|
|
126
|
+
return (self, signature[1:])
|
|
127
|
+
|
|
128
|
+
# basic type
|
|
129
|
+
return (SignatureType(token), signature[1:])
|
|
130
|
+
|
|
131
|
+
def _verify_byte(self, body: Any) -> None:
|
|
132
|
+
BYTE_MIN = 0x00
|
|
133
|
+
BYTE_MAX = 0xFF
|
|
134
|
+
if not isinstance(body, int):
|
|
135
|
+
raise SignatureBodyMismatchError(
|
|
136
|
+
f'DBus BYTE type "y" must be Python type "int", got {type(body)}'
|
|
137
|
+
)
|
|
138
|
+
if body < BYTE_MIN or body > BYTE_MAX:
|
|
139
|
+
raise SignatureBodyMismatchError(
|
|
140
|
+
f"DBus BYTE type must be between {BYTE_MIN} and {BYTE_MAX}"
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
def _verify_boolean(self, body: Any) -> None:
|
|
144
|
+
if not isinstance(body, bool):
|
|
145
|
+
raise SignatureBodyMismatchError(
|
|
146
|
+
f'DBus BOOLEAN type "b" must be Python type "bool", got {type(body)}'
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
def _verify_int16(self, body: Any) -> None:
|
|
150
|
+
INT16_MIN = -0x7FFF - 1
|
|
151
|
+
INT16_MAX = 0x7FFF
|
|
152
|
+
if not isinstance(body, int):
|
|
153
|
+
raise SignatureBodyMismatchError(
|
|
154
|
+
f'DBus INT16 type "n" must be Python type "int", got {type(body)}'
|
|
155
|
+
)
|
|
156
|
+
if body > INT16_MAX or body < INT16_MIN:
|
|
157
|
+
raise SignatureBodyMismatchError(
|
|
158
|
+
f'DBus INT16 type "n" must be between {INT16_MIN} and {INT16_MAX}'
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
def _verify_uint16(self, body: Any) -> None:
|
|
162
|
+
UINT16_MIN = 0
|
|
163
|
+
UINT16_MAX = 0xFFFF
|
|
164
|
+
if not isinstance(body, int):
|
|
165
|
+
raise SignatureBodyMismatchError(
|
|
166
|
+
f'DBus UINT16 type "q" must be Python type "int", got {type(body)}'
|
|
167
|
+
)
|
|
168
|
+
if body > UINT16_MAX or body < UINT16_MIN:
|
|
169
|
+
raise SignatureBodyMismatchError(
|
|
170
|
+
f'DBus UINT16 type "q" must be between {UINT16_MIN} and {UINT16_MAX}'
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
def _verify_int32(self, body: int) -> None:
|
|
174
|
+
INT32_MIN = -0x7FFFFFFF - 1
|
|
175
|
+
INT32_MAX = 0x7FFFFFFF
|
|
176
|
+
if not isinstance(body, int):
|
|
177
|
+
raise SignatureBodyMismatchError(
|
|
178
|
+
f'DBus INT32 type "i" must be Python type "int", got {type(body)}'
|
|
179
|
+
)
|
|
180
|
+
if body > INT32_MAX or body < INT32_MIN:
|
|
181
|
+
raise SignatureBodyMismatchError(
|
|
182
|
+
f'DBus INT32 type "i" must be between {INT32_MIN} and {INT32_MAX}'
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
def _verify_uint32(self, body: Any) -> None:
|
|
186
|
+
UINT32_MIN = 0
|
|
187
|
+
UINT32_MAX = 0xFFFFFFFF
|
|
188
|
+
if not isinstance(body, int):
|
|
189
|
+
raise SignatureBodyMismatchError(
|
|
190
|
+
f'DBus UINT32 type "u" must be Python type "int", got {type(body)}'
|
|
191
|
+
)
|
|
192
|
+
if body > UINT32_MAX or body < UINT32_MIN:
|
|
193
|
+
raise SignatureBodyMismatchError(
|
|
194
|
+
f'DBus UINT32 type "u" must be between {UINT32_MIN} and {UINT32_MAX}'
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
def _verify_int64(self, body: Any) -> None:
|
|
198
|
+
INT64_MAX = 9223372036854775807
|
|
199
|
+
INT64_MIN = -INT64_MAX - 1
|
|
200
|
+
if not isinstance(body, int):
|
|
201
|
+
raise SignatureBodyMismatchError(
|
|
202
|
+
f'DBus INT64 type "x" must be Python type "int", got {type(body)}'
|
|
203
|
+
)
|
|
204
|
+
if body > INT64_MAX or body < INT64_MIN:
|
|
205
|
+
raise SignatureBodyMismatchError(
|
|
206
|
+
f'DBus INT64 type "x" must be between {INT64_MIN} and {INT64_MAX}'
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
def _verify_uint64(self, body: Any) -> None:
|
|
210
|
+
UINT64_MIN = 0
|
|
211
|
+
UINT64_MAX = 18446744073709551615
|
|
212
|
+
if not isinstance(body, int):
|
|
213
|
+
raise SignatureBodyMismatchError(
|
|
214
|
+
f'DBus UINT64 type "t" must be Python type "int", got {type(body)}'
|
|
215
|
+
)
|
|
216
|
+
if body > UINT64_MAX or body < UINT64_MIN:
|
|
217
|
+
raise SignatureBodyMismatchError(
|
|
218
|
+
f'DBus UINT64 type "t" must be between {UINT64_MIN} and {UINT64_MAX}'
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
def _verify_double(self, body: Any) -> None:
|
|
222
|
+
if not isinstance(body, (float, int)):
|
|
223
|
+
raise SignatureBodyMismatchError(
|
|
224
|
+
f'DBus DOUBLE type "d" must be Python type "float" or "int", got {type(body)}'
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
def _verify_unix_fd(self, body: Any) -> None:
|
|
228
|
+
try:
|
|
229
|
+
self._verify_uint32(body)
|
|
230
|
+
except SignatureBodyMismatchError as ex:
|
|
231
|
+
raise SignatureBodyMismatchError(
|
|
232
|
+
'DBus UNIX_FD type "h" must be a valid UINT32'
|
|
233
|
+
) from ex
|
|
234
|
+
|
|
235
|
+
def _verify_object_path(self, body: Any) -> None:
|
|
236
|
+
if not is_object_path_valid(body):
|
|
237
|
+
raise SignatureBodyMismatchError(
|
|
238
|
+
'DBus OBJECT_PATH type "o" must be a valid object path'
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
def _verify_string(self, body: Any) -> None:
|
|
242
|
+
if not isinstance(body, str):
|
|
243
|
+
raise SignatureBodyMismatchError(
|
|
244
|
+
f'DBus STRING type "s" must be Python type "str", got {type(body)}'
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
def _verify_signature(self, body: Any) -> None:
|
|
248
|
+
# I guess we could run it through the SignatureTree parser instead
|
|
249
|
+
if not isinstance(body, str):
|
|
250
|
+
raise SignatureBodyMismatchError(
|
|
251
|
+
f'DBus SIGNATURE type "g" must be Python type "str", got {type(body)}'
|
|
252
|
+
)
|
|
253
|
+
if len(body.encode()) > 0xFF:
|
|
254
|
+
raise SignatureBodyMismatchError(
|
|
255
|
+
'DBus SIGNATURE type "g" must be less than 256 bytes'
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
def _verify_array(self, body: Any) -> None:
|
|
259
|
+
child_type = self.children[0]
|
|
260
|
+
|
|
261
|
+
if child_type.token == "{":
|
|
262
|
+
if not isinstance(body, dict):
|
|
263
|
+
raise SignatureBodyMismatchError(
|
|
264
|
+
f'DBus ARRAY type "a" with DICT_ENTRY child must be Python type "dict", got {type(body)}'
|
|
265
|
+
)
|
|
266
|
+
for key, value in body.items():
|
|
267
|
+
child_type.children[0].verify(key)
|
|
268
|
+
child_type.children[1].verify(value)
|
|
269
|
+
elif child_type.token == "y":
|
|
270
|
+
if not isinstance(body, (bytearray, bytes)):
|
|
271
|
+
raise SignatureBodyMismatchError(
|
|
272
|
+
f'DBus ARRAY type "a" with BYTE child must be Python type "bytes", got {type(body)}'
|
|
273
|
+
)
|
|
274
|
+
# no need to verify children
|
|
275
|
+
else:
|
|
276
|
+
if not isinstance(body, list):
|
|
277
|
+
raise SignatureBodyMismatchError(
|
|
278
|
+
f'DBus ARRAY type "a" must be Python type "list", got {type(body)}'
|
|
279
|
+
)
|
|
280
|
+
for member in body:
|
|
281
|
+
child_type.verify(member)
|
|
282
|
+
|
|
283
|
+
def _verify_struct(self, body: Any) -> None:
|
|
284
|
+
if not isinstance(body, (list, tuple)):
|
|
285
|
+
raise SignatureBodyMismatchError(
|
|
286
|
+
f'DBus STRUCT type "(" must be Python type "list" or "tuple", got {type(body)}'
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
if len(body) != len(self.children):
|
|
290
|
+
raise SignatureBodyMismatchError(
|
|
291
|
+
'DBus STRUCT type "(" must have Python list members equal to the number of struct type members'
|
|
292
|
+
)
|
|
293
|
+
|
|
294
|
+
for i, member in enumerate(body):
|
|
295
|
+
self.children[i].verify(member)
|
|
296
|
+
|
|
297
|
+
def _verify_variant(self, body: Any) -> None:
|
|
298
|
+
# a variant signature and value is valid by construction
|
|
299
|
+
if not isinstance(body, Variant):
|
|
300
|
+
raise SignatureBodyMismatchError(
|
|
301
|
+
f'DBus VARIANT type "v" must be Python type "Variant", got {type(body)}'
|
|
302
|
+
)
|
|
303
|
+
|
|
304
|
+
def verify(self, body: Any) -> bool:
|
|
305
|
+
"""Verify that the body matches this type.
|
|
306
|
+
|
|
307
|
+
:returns: True if the body matches this type.
|
|
308
|
+
:raises:
|
|
309
|
+
:class:`SignatureBodyMismatchError` if the body does not match this type.
|
|
310
|
+
"""
|
|
311
|
+
if body is None:
|
|
312
|
+
raise SignatureBodyMismatchError('Cannot serialize Python type "None"')
|
|
313
|
+
validator = self.validators.get(self.token)
|
|
314
|
+
if validator:
|
|
315
|
+
validator(self, body)
|
|
316
|
+
else:
|
|
317
|
+
raise Exception(f"cannot verify type with token {self.token}")
|
|
318
|
+
|
|
319
|
+
return True
|
|
320
|
+
|
|
321
|
+
validators: dict[str, Callable[["SignatureType", Any], None]] = {
|
|
322
|
+
"y": _verify_byte,
|
|
323
|
+
"b": _verify_boolean,
|
|
324
|
+
"n": _verify_int16,
|
|
325
|
+
"q": _verify_uint16,
|
|
326
|
+
"i": _verify_int32,
|
|
327
|
+
"u": _verify_uint32,
|
|
328
|
+
"x": _verify_int64,
|
|
329
|
+
"t": _verify_uint64,
|
|
330
|
+
"d": _verify_double,
|
|
331
|
+
"h": _verify_uint32,
|
|
332
|
+
"o": _verify_string,
|
|
333
|
+
"s": _verify_string,
|
|
334
|
+
"g": _verify_signature,
|
|
335
|
+
"a": _verify_array,
|
|
336
|
+
"(": _verify_struct,
|
|
337
|
+
"v": _verify_variant,
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
class SignatureTree: # noqa: PLW1641
|
|
342
|
+
"""A class that represents a signature as a tree structure for conveniently
|
|
343
|
+
working with DBus signatures.
|
|
344
|
+
|
|
345
|
+
This class will not normally be used directly by the user.
|
|
346
|
+
|
|
347
|
+
:ivar types: A list of parsed complete types.
|
|
348
|
+
:vartype types: list(:class:`SignatureType`)
|
|
349
|
+
|
|
350
|
+
:ivar ~.signature: The signature of this signature tree.
|
|
351
|
+
:vartype ~.signature: str
|
|
352
|
+
|
|
353
|
+
:ivar root_type: The root type of this signature tree.
|
|
354
|
+
:vartype root_type: :class:`SignatureType
|
|
355
|
+
|
|
356
|
+
:raises:
|
|
357
|
+
:class:`InvalidSignatureError` if the given signature is not valid.
|
|
358
|
+
"""
|
|
359
|
+
|
|
360
|
+
__slots__ = ("root_type", "signature", "types")
|
|
361
|
+
|
|
362
|
+
def __init__(self, signature: str = "") -> None:
|
|
363
|
+
self.signature = signature
|
|
364
|
+
|
|
365
|
+
self.types: list[SignatureType] = []
|
|
366
|
+
|
|
367
|
+
if len(signature) > 0xFF:
|
|
368
|
+
raise InvalidSignatureError("A signature must be less than 256 characters")
|
|
369
|
+
|
|
370
|
+
while signature:
|
|
371
|
+
(type_, signature) = SignatureType._parse_next(signature)
|
|
372
|
+
self.types.append(type_)
|
|
373
|
+
|
|
374
|
+
self.root_type = self.types[0] if self.types else None
|
|
375
|
+
|
|
376
|
+
def __eq__(self, other: object) -> bool:
|
|
377
|
+
if type(other) is SignatureTree:
|
|
378
|
+
return self.signature == other.signature
|
|
379
|
+
return super().__eq__(other)
|
|
380
|
+
|
|
381
|
+
def verify(self, body: list[Any]) -> bool:
|
|
382
|
+
"""Verifies that the give body matches this signature tree
|
|
383
|
+
|
|
384
|
+
:param body: the body to verify for this tree
|
|
385
|
+
:type body: list(Any)
|
|
386
|
+
|
|
387
|
+
:returns: True if the signature matches the body or an exception if not.
|
|
388
|
+
|
|
389
|
+
:raises:
|
|
390
|
+
:class:`SignatureBodyMismatchError` if the signature does not match the body.
|
|
391
|
+
"""
|
|
392
|
+
if not isinstance(body, list):
|
|
393
|
+
raise SignatureBodyMismatchError(
|
|
394
|
+
f"The body must be a list (got {type(body)})"
|
|
395
|
+
)
|
|
396
|
+
if len(body) != len(self.types):
|
|
397
|
+
raise SignatureBodyMismatchError(
|
|
398
|
+
f"The body has the wrong number of types (got {len(body)}, expected {len(self.types)})"
|
|
399
|
+
)
|
|
400
|
+
for i, type_ in enumerate(self.types):
|
|
401
|
+
type_.verify(body[i])
|
|
402
|
+
|
|
403
|
+
return True
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
class Variant: # noqa: PLW1641
|
|
407
|
+
"""A class to represent a DBus variant (type "v").
|
|
408
|
+
|
|
409
|
+
This class is used in message bodies to represent variants. The user can
|
|
410
|
+
expect a value in the body with type "v" to use this class and can
|
|
411
|
+
construct this class directly for use in message bodies sent over the bus.
|
|
412
|
+
|
|
413
|
+
:ivar signature: The signature for this variant. Must be a single complete type.
|
|
414
|
+
:vartype signature: str or SignatureTree or SignatureType
|
|
415
|
+
|
|
416
|
+
:ivar value: The value of this variant. Must correspond to the signature.
|
|
417
|
+
:vartype value: Any
|
|
418
|
+
|
|
419
|
+
:raises:
|
|
420
|
+
:class:`InvalidSignatureError` if the signature is not valid.
|
|
421
|
+
:class:`SignatureBodyMismatchError` if the signature does not match the body.
|
|
422
|
+
"""
|
|
423
|
+
|
|
424
|
+
__slots__ = ("signature", "type", "value")
|
|
425
|
+
|
|
426
|
+
def __init__(
|
|
427
|
+
self,
|
|
428
|
+
signature: str | SignatureTree | SignatureType,
|
|
429
|
+
value: Any,
|
|
430
|
+
verify: bool = True,
|
|
431
|
+
) -> None:
|
|
432
|
+
"""Init a new Variant."""
|
|
433
|
+
if type(signature) is SignatureTree:
|
|
434
|
+
signature_tree = signature
|
|
435
|
+
self.signature = signature_tree.signature
|
|
436
|
+
self.type = signature_tree.types[0]
|
|
437
|
+
elif type(signature) is SignatureType:
|
|
438
|
+
signature_tree = None
|
|
439
|
+
self.signature = signature.signature
|
|
440
|
+
self.type = signature
|
|
441
|
+
elif type(signature) is str:
|
|
442
|
+
signature_tree = get_signature_tree(signature)
|
|
443
|
+
self.signature = signature
|
|
444
|
+
self.type = signature_tree.types[0]
|
|
445
|
+
else:
|
|
446
|
+
raise TypeError(
|
|
447
|
+
"signature must be a SignatureTree, SignatureType, or a string"
|
|
448
|
+
)
|
|
449
|
+
self.value = value
|
|
450
|
+
if verify:
|
|
451
|
+
if signature_tree and len(signature_tree.types) != 1:
|
|
452
|
+
raise ValueError(
|
|
453
|
+
"variants must have a signature for a single complete type"
|
|
454
|
+
)
|
|
455
|
+
self.type.verify(value)
|
|
456
|
+
|
|
457
|
+
@staticmethod
|
|
458
|
+
def _factory(signature_tree: SignatureTree, value: Any) -> "Variant":
|
|
459
|
+
self = Variant.__new__(Variant)
|
|
460
|
+
self.signature = signature_tree.signature
|
|
461
|
+
self.type = signature_tree.root_type
|
|
462
|
+
self.value = value
|
|
463
|
+
return self
|
|
464
|
+
|
|
465
|
+
def __eq__(self, other: object) -> bool:
|
|
466
|
+
if type(other) is Variant:
|
|
467
|
+
return self.signature == other.signature and self.value == other.value
|
|
468
|
+
return super().__eq__(other)
|
|
469
|
+
|
|
470
|
+
def __repr__(self) -> str:
|
|
471
|
+
return f"<dbus_fast.signature.Variant ('{self.type.signature}', {self.value})>"
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
get_signature_tree = lru_cache(maxsize=None)(SignatureTree)
|
|
475
|
+
"""Get a signature tree for the given signature.
|
|
476
|
+
|
|
477
|
+
:param signature: The signature to get a tree for.
|
|
478
|
+
:type signature: str
|
|
479
|
+
|
|
480
|
+
:returns: The signature tree for the given signature.
|
|
481
|
+
:rtype: :class:`SignatureTree`
|
|
482
|
+
"""
|
|
Binary file
|
dbus_fast/unpack.pxd
ADDED
dbus_fast/unpack.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from .signature import Variant
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def unpack_variants(data: Any) -> Any:
|
|
7
|
+
"""Unpack variants and remove signature info.
|
|
8
|
+
|
|
9
|
+
This function should only be used to unpack
|
|
10
|
+
unmarshalled data as the checks are not
|
|
11
|
+
idiomatic.
|
|
12
|
+
"""
|
|
13
|
+
return _unpack_variants(data)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _unpack_variants(data: Any) -> Any:
|
|
17
|
+
if type(data) is dict:
|
|
18
|
+
return {k: _unpack_variants(v) for k, v in data.items()}
|
|
19
|
+
if type(data) is list:
|
|
20
|
+
return [_unpack_variants(item) for item in data]
|
|
21
|
+
if type(data) is Variant:
|
|
22
|
+
var = data
|
|
23
|
+
return _unpack_variants(var.value)
|
|
24
|
+
return data
|