dbus-fast 2.45.1__cp311-cp311-macosx_11_0_arm64.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 dbus-fast might be problematic. Click here for more details.

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