dbus-fast 3.0.0__cp314-cp314-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.

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-314-aarch64-linux-musl.so +0 -0
  6. dbus_fast/_private/address.pxd +15 -0
  7. dbus_fast/_private/address.py +119 -0
  8. dbus_fast/_private/constants.py +20 -0
  9. dbus_fast/_private/marshaller.cpython-314-aarch64-linux-musl.so +0 -0
  10. dbus_fast/_private/marshaller.pxd +110 -0
  11. dbus_fast/_private/marshaller.py +231 -0
  12. dbus_fast/_private/unmarshaller.cpython-314-aarch64-linux-musl.so +0 -0
  13. dbus_fast/_private/unmarshaller.pxd +261 -0
  14. dbus_fast/_private/unmarshaller.py +904 -0
  15. dbus_fast/_private/util.py +177 -0
  16. dbus_fast/aio/__init__.py +5 -0
  17. dbus_fast/aio/message_bus.py +569 -0
  18. dbus_fast/aio/message_reader.cpython-314-aarch64-linux-musl.so +0 -0
  19. dbus_fast/aio/message_reader.pxd +13 -0
  20. dbus_fast/aio/message_reader.py +51 -0
  21. dbus_fast/aio/proxy_object.py +207 -0
  22. dbus_fast/auth.py +125 -0
  23. dbus_fast/constants.py +152 -0
  24. dbus_fast/errors.py +81 -0
  25. dbus_fast/glib/__init__.py +3 -0
  26. dbus_fast/glib/message_bus.py +513 -0
  27. dbus_fast/glib/proxy_object.py +318 -0
  28. dbus_fast/introspection.py +682 -0
  29. dbus_fast/message.cpython-314-aarch64-linux-musl.so +0 -0
  30. dbus_fast/message.pxd +76 -0
  31. dbus_fast/message.py +389 -0
  32. dbus_fast/message_bus.cpython-314-aarch64-linux-musl.so +0 -0
  33. dbus_fast/message_bus.pxd +75 -0
  34. dbus_fast/message_bus.py +1332 -0
  35. dbus_fast/proxy_object.py +357 -0
  36. dbus_fast/py.typed +0 -0
  37. dbus_fast/send_reply.py +61 -0
  38. dbus_fast/service.cpython-314-aarch64-linux-musl.so +0 -0
  39. dbus_fast/service.pxd +50 -0
  40. dbus_fast/service.py +701 -0
  41. dbus_fast/signature.cpython-314-aarch64-linux-musl.so +0 -0
  42. dbus_fast/signature.pxd +31 -0
  43. dbus_fast/signature.py +484 -0
  44. dbus_fast/unpack.cpython-314-aarch64-linux-musl.so +0 -0
  45. dbus_fast/unpack.pxd +13 -0
  46. dbus_fast/unpack.py +28 -0
  47. dbus_fast/validators.py +199 -0
  48. dbus_fast-3.0.0.dist-info/METADATA +262 -0
  49. dbus_fast-3.0.0.dist-info/RECORD +51 -0
  50. dbus_fast-3.0.0.dist-info/WHEEL +4 -0
  51. dbus_fast-3.0.0.dist-info/licenses/LICENSE +22 -0
dbus_fast/service.py ADDED
@@ -0,0 +1,701 @@
1
+ # cython: freethreading_compatible = True
2
+
3
+ from __future__ import annotations
4
+
5
+ import asyncio
6
+ import copy
7
+ import inspect
8
+ from collections.abc import Callable
9
+ from functools import wraps
10
+ from typing import TYPE_CHECKING, Any, Protocol
11
+
12
+ from . import introspection as intr
13
+ from ._private.util import (
14
+ parse_annotation,
15
+ replace_fds_with_idx,
16
+ replace_idx_with_fds,
17
+ signature_contains_type,
18
+ )
19
+ from .constants import PropertyAccess
20
+ from .errors import SignalDisabledError
21
+ from .message import Message
22
+ from .send_reply import SendReply
23
+ from .signature import (
24
+ SignatureBodyMismatchError,
25
+ SignatureTree,
26
+ Variant,
27
+ get_signature_tree,
28
+ )
29
+
30
+ if TYPE_CHECKING:
31
+ from .message_bus import BaseMessageBus
32
+
33
+ str_ = str
34
+
35
+ HandlerType = Callable[[Message, SendReply], None]
36
+
37
+
38
+ class _MethodCallbackProtocol(Protocol):
39
+ def __call__(self, interface: ServiceInterface, *args: Any) -> Any: ...
40
+
41
+
42
+ class _Method:
43
+ def __init__(
44
+ self, fn: _MethodCallbackProtocol, name: str, disabled: bool = False
45
+ ) -> None:
46
+ in_signature = ""
47
+ out_signature = ""
48
+
49
+ inspection = inspect.signature(fn)
50
+
51
+ in_args: list[intr.Arg] = []
52
+ for i, param in enumerate(inspection.parameters.values()):
53
+ if i == 0:
54
+ # first is self
55
+ continue
56
+ annotation = parse_annotation(param.annotation)
57
+ if not annotation:
58
+ raise ValueError(
59
+ "method parameters must specify the dbus type string as an annotation"
60
+ )
61
+ in_args.append(intr.Arg(annotation, intr.ArgDirection.IN, param.name))
62
+ in_signature += annotation
63
+
64
+ out_args: list[intr.Arg] = []
65
+ out_signature = parse_annotation(inspection.return_annotation)
66
+ if out_signature:
67
+ for type_ in get_signature_tree(out_signature).types:
68
+ out_args.append(intr.Arg(type_, intr.ArgDirection.OUT))
69
+
70
+ self.name = name
71
+ self.fn = fn
72
+ self.disabled = disabled
73
+ self.introspection = intr.Method(name, in_args, out_args)
74
+ self.in_signature = in_signature
75
+ self.out_signature = out_signature
76
+ self.in_signature_tree = get_signature_tree(in_signature)
77
+ self.out_signature_tree = get_signature_tree(out_signature)
78
+
79
+
80
+ def dbus_method(name: str | None = None, disabled: bool = False) -> Callable:
81
+ """A decorator to mark a class method of a :class:`ServiceInterface` to be a DBus service method.
82
+
83
+ The parameters and return value must each be annotated with a signature
84
+ string of a single complete DBus type.
85
+
86
+ This class method will be called when a client calls the method on the DBus
87
+ interface. The parameters given to the function come from the calling
88
+ client and will conform to the dbus-fast type system. The parameters
89
+ returned will be returned to the calling client and must conform to the
90
+ dbus-fast type system. If multiple parameters are returned, they must be
91
+ contained within a :class:`list`.
92
+
93
+ The decorated method may raise a :class:`DBusError <dbus_fast.DBusError>`
94
+ to return an error to the client.
95
+
96
+ :param name: The member name that DBus clients will use to call this method. Defaults to the name of the class method.
97
+ :type name: str
98
+ :param disabled: If set to true, the method will not be visible to clients.
99
+ :type disabled: bool
100
+
101
+ :example:
102
+
103
+ ::
104
+
105
+ @dbus_method()
106
+ def echo(self, val: 's') -> 's':
107
+ return val
108
+
109
+ @dbus_method()
110
+ def echo_two(self, val1: 's', val2: 'u') -> 'su':
111
+ return [val1, val2]
112
+
113
+ .. versionadded:: v2.46.0
114
+ In older versions, this was named ``@method``. The old name still exists.
115
+ """
116
+ if name is not None and type(name) is not str:
117
+ raise TypeError("name must be a string")
118
+ if type(disabled) is not bool:
119
+ raise TypeError("disabled must be a bool")
120
+
121
+ def decorator(fn: Callable) -> Callable:
122
+ @wraps(fn)
123
+ def wrapped(*args: Any, **kwargs: Any) -> None:
124
+ fn(*args, **kwargs)
125
+
126
+ fn_name = name if name else fn.__name__
127
+ wrapped.__dict__["__DBUS_METHOD"] = _Method(fn, fn_name, disabled=disabled)
128
+
129
+ return wrapped
130
+
131
+ return decorator
132
+
133
+
134
+ method = dbus_method # backward compatibility alias
135
+
136
+
137
+ class _Signal:
138
+ def __init__(self, fn: Callable, name: str, disabled: bool = False) -> None:
139
+ inspection = inspect.signature(fn)
140
+
141
+ args = []
142
+ signature = ""
143
+ signature_tree = None
144
+
145
+ return_annotation = parse_annotation(inspection.return_annotation)
146
+
147
+ if return_annotation:
148
+ signature = return_annotation
149
+ signature_tree = get_signature_tree(signature)
150
+ for type_ in signature_tree.types:
151
+ args.append(intr.Arg(type_, intr.ArgDirection.OUT))
152
+ else:
153
+ signature = ""
154
+ signature_tree = get_signature_tree("")
155
+
156
+ self.signature = signature
157
+ self.signature_tree = signature_tree
158
+ self.name = name
159
+ self.disabled = disabled
160
+ self.introspection = intr.Signal(self.name, args)
161
+
162
+
163
+ def dbus_signal(name: str | None = None, disabled: bool = False) -> Callable:
164
+ """A decorator to mark a class method of a :class:`ServiceInterface` to be a DBus signal.
165
+
166
+ The signal is broadcast on the bus when the decorated class method is
167
+ called by the user.
168
+
169
+ If the signal has an out argument, the class method must have a return type
170
+ annotation with a signature string of a single complete DBus type and the
171
+ return value of the class method must conform to the dbus-fast type system.
172
+ If the signal has multiple out arguments, they must be returned within a
173
+ ``list``.
174
+
175
+ :param name: The member name that will be used for this signal. Defaults to
176
+ the name of the class method.
177
+ :type name: str
178
+ :param disabled: If set to true, the signal will not be visible to clients.
179
+ :type disabled: bool
180
+
181
+ :example:
182
+
183
+ ::
184
+
185
+ @dbus_signal()
186
+ def string_signal(self, val) -> 's':
187
+ return val
188
+
189
+ @dbus_signal()
190
+ def two_strings_signal(self, val1, val2) -> 'ss':
191
+ return [val1, val2]
192
+
193
+ .. versionadded:: v2.46.0
194
+ In older versions, this was named ``@signal``. The old name still exists.
195
+ """
196
+ if name is not None and type(name) is not str:
197
+ raise TypeError("name must be a string")
198
+ if type(disabled) is not bool:
199
+ raise TypeError("disabled must be a bool")
200
+
201
+ def decorator(fn: Callable) -> Callable:
202
+ fn_name = name if name else fn.__name__
203
+ signal = _Signal(fn, fn_name, disabled)
204
+
205
+ @wraps(fn)
206
+ def wrapped(self, *args: Any, **kwargs: Any) -> Any:
207
+ if signal.disabled:
208
+ raise SignalDisabledError("Tried to call a disabled signal")
209
+ result = fn(self, *args, **kwargs)
210
+ ServiceInterface._handle_signal(self, signal, result)
211
+ return result
212
+
213
+ wrapped.__dict__["__DBUS_SIGNAL"] = signal
214
+
215
+ return wrapped
216
+
217
+ return decorator
218
+
219
+
220
+ signal = dbus_signal # backward compatibility alias
221
+
222
+
223
+ class _Property(property):
224
+ def set_options(self, options: dict[str, Any]) -> None:
225
+ self.options = getattr(self, "options", {})
226
+ for k, v in options.items():
227
+ self.options[k] = v
228
+
229
+ if "name" in options and options["name"] is not None:
230
+ self.name = options["name"]
231
+ else:
232
+ self.name = self.prop_getter.__name__
233
+
234
+ if "access" in options:
235
+ self.access = PropertyAccess(options["access"])
236
+ else:
237
+ self.access = PropertyAccess.READWRITE
238
+
239
+ self.disabled = options.get("disabled", False)
240
+ self.introspection = intr.Property(self.name, self.signature, self.access)
241
+
242
+ self.__dict__["__DBUS_PROPERTY"] = True
243
+
244
+ def __init__(self, fn, *args, **kwargs):
245
+ self.prop_getter = fn
246
+ self.prop_setter = None
247
+
248
+ inspection = inspect.signature(fn)
249
+ if len(inspection.parameters) != 1:
250
+ raise ValueError('the property must only have the "self" input parameter')
251
+
252
+ return_annotation = parse_annotation(inspection.return_annotation)
253
+
254
+ if not return_annotation:
255
+ raise ValueError(
256
+ "the property must specify the dbus type string as a return annotation string"
257
+ )
258
+
259
+ self.signature = return_annotation
260
+ tree = get_signature_tree(return_annotation)
261
+
262
+ if len(tree.types) != 1:
263
+ raise ValueError("the property signature must be a single complete type")
264
+
265
+ self.type = tree.types[0]
266
+
267
+ if "options" in kwargs:
268
+ options = kwargs["options"]
269
+ self.set_options(options)
270
+ del kwargs["options"]
271
+
272
+ super().__init__(fn, *args, **kwargs)
273
+
274
+ def setter(self, fn, **kwargs):
275
+ # XXX The setter decorator seems to be recreating the class in the list
276
+ # of class members and clobbering the options so we need to reset them.
277
+ # Why does it do that?
278
+ result = super().setter(fn, **kwargs)
279
+ result.prop_setter = fn
280
+ result.set_options(self.options)
281
+ return result
282
+
283
+
284
+ def dbus_property(
285
+ access: PropertyAccess = PropertyAccess.READWRITE,
286
+ name: str | None = None,
287
+ disabled: bool = False,
288
+ ) -> Callable:
289
+ """A decorator to mark a class method of a :class:`ServiceInterface` to be a DBus property.
290
+
291
+ The class method must be a Python getter method with a return annotation
292
+ that is a signature string of a single complete DBus type. When a client
293
+ gets the property through the ``org.freedesktop.DBus.Properties``
294
+ interface, the getter will be called and the resulting value will be
295
+ returned to the client.
296
+
297
+ If the property is writable, it must have a setter method that takes a
298
+ single parameter that is annotated with the same signature. When a client
299
+ sets the property through the ``org.freedesktop.DBus.Properties``
300
+ interface, the setter will be called with the value from the calling
301
+ client.
302
+
303
+ The parameters of the getter and the setter must conform to the dbus-fast
304
+ type system. The getter or the setter may raise a :class:`DBusError
305
+ <dbus_fast.DBusError>` to return an error to the client.
306
+
307
+ :param name: The name that DBus clients will use to interact with this
308
+ property on the bus.
309
+ :type name: str
310
+ :param disabled: If set to true, the property will not be visible to
311
+ clients.
312
+ :type disabled: bool
313
+
314
+ :example:
315
+
316
+ ::
317
+
318
+ @dbus_property()
319
+ def string_prop(self) -> 's':
320
+ return self._string_prop
321
+
322
+ @string_prop.setter
323
+ def string_prop(self, val: 's'):
324
+ self._string_prop = val
325
+ """
326
+ if type(access) is not PropertyAccess:
327
+ raise TypeError("access must be a PropertyAccess class")
328
+ if name is not None and type(name) is not str:
329
+ raise TypeError("name must be a string")
330
+ if type(disabled) is not bool:
331
+ raise TypeError("disabled must be a bool")
332
+
333
+ def decorator(fn: Callable) -> _Property:
334
+ options = {"name": name, "access": access, "disabled": disabled}
335
+ return _Property(fn, options=options)
336
+
337
+ return decorator
338
+
339
+
340
+ def _real_fn_result_to_body(
341
+ result: Any | None,
342
+ signature_tree: SignatureTree,
343
+ replace_fds: bool,
344
+ ) -> tuple[list[Any], list[int]]:
345
+ out_len = len(signature_tree.types)
346
+ if result is None:
347
+ final_result = []
348
+ elif out_len == 1:
349
+ final_result = [result]
350
+ else:
351
+ result_type = type(result)
352
+ if result_type is not list and result_type is not tuple:
353
+ raise SignatureBodyMismatchError(
354
+ "Expected signal to return a list or tuple of arguments"
355
+ )
356
+ final_result = result
357
+
358
+ if out_len != len(final_result):
359
+ raise SignatureBodyMismatchError(
360
+ f"Signature and function return mismatch, expected "
361
+ f"{len(signature_tree.types)} arguments but got {len(result)}" # type: ignore[arg-type]
362
+ )
363
+
364
+ if not replace_fds:
365
+ return final_result, []
366
+ return replace_fds_with_idx(signature_tree, final_result)
367
+
368
+
369
+ class ServiceInterface:
370
+ """An abstract class that can be extended by the user to define DBus services.
371
+
372
+ Instances of :class:`ServiceInterface` can be exported on a path of the bus
373
+ with the :class:`export <dbus_fast.message_bus.BaseMessageBus.export>`
374
+ method of a :class:`MessageBus <dbus_fast.message_bus.BaseMessageBus>`.
375
+
376
+ Use the :func:`@dbus_method <dbus_fast.service.dbus_method>`, :func:`@dbus_property
377
+ <dbus_fast.service.dbus_property>`, and :func:`@dbus_signal
378
+ <dbus_fast.service.dbus_signal>` decorators to mark class methods as DBus
379
+ methods, properties, and signals respectively.
380
+
381
+ :ivar name: The name of this interface as it appears to clients. Must be a
382
+ valid interface name.
383
+ :vartype name: str
384
+ """
385
+
386
+ def __init__(self, name: str) -> None:
387
+ # TODO cannot be overridden by a dbus member
388
+ self.name = name
389
+ self.__methods: list[_Method] = []
390
+ self.__properties: list[_Property] = []
391
+ self.__signals: list[_Signal] = []
392
+ self.__buses: set[BaseMessageBus] = set()
393
+ self.__handlers: dict[BaseMessageBus, dict[_Method, HandlerType]] = {}
394
+ # Map of methods by bus of name -> method, handler
395
+ self.__handlers_by_name_signature: dict[
396
+ BaseMessageBus, dict[str, tuple[_Method, HandlerType]]
397
+ ] = {}
398
+ for _, member in inspect.getmembers(type(self)):
399
+ member_dict = getattr(member, "__dict__", {})
400
+ if type(member) is _Property:
401
+ # XXX The getter and the setter may show up as different
402
+ # members if they have different names. But if they have the
403
+ # same name, they will be the same member. So we try to merge
404
+ # them together here. I wish we could make this cleaner.
405
+ found = False
406
+ for prop in self.__properties:
407
+ if prop.prop_getter is member.prop_getter:
408
+ found = True
409
+ if member.prop_setter is not None:
410
+ prop.prop_setter = member.prop_setter
411
+
412
+ if not found:
413
+ self.__properties.append(member)
414
+ elif "__DBUS_METHOD" in member_dict:
415
+ method = member_dict["__DBUS_METHOD"]
416
+ assert type(method) is _Method
417
+ self.__methods.append(method)
418
+ elif "__DBUS_SIGNAL" in member_dict:
419
+ signal = member_dict["__DBUS_SIGNAL"]
420
+ assert type(signal) is _Signal
421
+ self.__signals.append(signal)
422
+
423
+ # validate that writable properties have a setter
424
+ for prop in self.__properties:
425
+ if prop.access.writable() and prop.prop_setter is None:
426
+ raise ValueError(
427
+ f'property "{prop.name}" is writable but does not have a setter'
428
+ )
429
+
430
+ def emit_properties_changed(
431
+ self, changed_properties: dict[str, Any], invalidated_properties: list[str] = []
432
+ ) -> None:
433
+ """Emit the ``org.freedesktop.DBus.Properties.PropertiesChanged`` signal.
434
+
435
+ This signal is intended to be used to alert clients when a property of
436
+ the interface has changed.
437
+
438
+ :param changed_properties: The keys must be the names of properties exposed by this bus. The values must be valid for the signature of those properties.
439
+ :type changed_properties: dict(str, Any)
440
+ :param invalidated_properties: A list of names of properties that are now invalid (presumably for clients who cache the value).
441
+ :type invalidated_properties: list(str)
442
+ """
443
+ # TODO cannot be overridden by a dbus member
444
+ variant_dict = {}
445
+
446
+ for prop in ServiceInterface._get_properties(self):
447
+ if prop.name in changed_properties:
448
+ variant_dict[prop.name] = Variant(
449
+ prop.signature, changed_properties[prop.name]
450
+ )
451
+
452
+ body = [self.name, variant_dict, invalidated_properties]
453
+ for bus in ServiceInterface._get_buses(self):
454
+ bus._interface_signal_notify(
455
+ self,
456
+ "org.freedesktop.DBus.Properties",
457
+ "PropertiesChanged",
458
+ "sa{sv}as",
459
+ body,
460
+ )
461
+
462
+ def introspect(self) -> intr.Interface:
463
+ """Get introspection information for this interface.
464
+
465
+ This might be useful for creating clients for the interface or examining the introspection output of an interface.
466
+
467
+ :returns: The introspection data for the interface.
468
+ :rtype: :class:`dbus_fast.introspection.Interface`
469
+ """
470
+ # TODO cannot be overridden by a dbus member
471
+ return intr.Interface(
472
+ self.name,
473
+ methods=[
474
+ method.introspection
475
+ for method in ServiceInterface._get_methods(self)
476
+ if not method.disabled
477
+ ],
478
+ signals=[
479
+ signal.introspection
480
+ for signal in ServiceInterface._get_signals(self)
481
+ if not signal.disabled
482
+ ],
483
+ properties=[
484
+ prop.introspection
485
+ for prop in ServiceInterface._get_properties(self)
486
+ if not prop.disabled
487
+ ],
488
+ )
489
+
490
+ @staticmethod
491
+ def _get_properties(interface: ServiceInterface) -> list[_Property]:
492
+ return interface.__properties
493
+
494
+ @staticmethod
495
+ def _get_methods(interface: ServiceInterface) -> list[_Method]:
496
+ return interface.__methods
497
+
498
+ @staticmethod
499
+ def _get_signals(interface: ServiceInterface) -> list[_Signal]:
500
+ return interface.__signals
501
+
502
+ @staticmethod
503
+ def _get_buses(interface: ServiceInterface) -> set[BaseMessageBus]:
504
+ return interface.__buses
505
+
506
+ @staticmethod
507
+ def _get_handler(
508
+ interface: ServiceInterface, method: _Method, bus: BaseMessageBus
509
+ ) -> HandlerType:
510
+ return interface.__handlers[bus][method]
511
+
512
+ @staticmethod
513
+ def _get_enabled_handler_by_name_signature(
514
+ interface: ServiceInterface,
515
+ bus: BaseMessageBus,
516
+ name: str_,
517
+ signature: str_,
518
+ ) -> HandlerType | None:
519
+ handlers = interface.__handlers_by_name_signature[bus]
520
+ if (method_handler := handlers.get(name)) is None:
521
+ return None
522
+ method = method_handler[0]
523
+ if method.disabled:
524
+ return None
525
+ return method_handler[1] if method.in_signature == signature else None
526
+
527
+ @staticmethod
528
+ def _add_bus(
529
+ interface: ServiceInterface,
530
+ bus: BaseMessageBus,
531
+ maker: Callable[[ServiceInterface, _Method], HandlerType],
532
+ ) -> None:
533
+ if bus in interface.__buses:
534
+ raise ValueError(
535
+ "Same interface instance cannot be added to the same bus twice"
536
+ )
537
+ interface.__buses.add(bus)
538
+ interface.__handlers[bus] = {
539
+ method: maker(interface, method) for method in interface.__methods
540
+ }
541
+ interface.__handlers_by_name_signature[bus] = {
542
+ method.name: (method, handler)
543
+ for method, handler in interface.__handlers[bus].items()
544
+ }
545
+
546
+ @staticmethod
547
+ def _remove_bus(interface: ServiceInterface, bus: BaseMessageBus) -> None:
548
+ interface.__buses.remove(bus)
549
+ del interface.__handlers[bus]
550
+
551
+ @staticmethod
552
+ def _msg_body_to_args(msg: Message) -> list[Any]:
553
+ return ServiceInterface._c_msg_body_to_args(msg)
554
+
555
+ @staticmethod
556
+ def _c_msg_body_to_args(msg: Message) -> list[Any]:
557
+ # https://github.com/cython/cython/issues/3327
558
+ if not signature_contains_type(msg.signature_tree, msg.body, "h"):
559
+ return msg.body
560
+
561
+ # XXX: This deep copy could be expensive if messages are very
562
+ # large. We could optimize this by only copying what we change
563
+ # here.
564
+ return replace_idx_with_fds(
565
+ msg.signature_tree, copy.deepcopy(msg.body), msg.unix_fds
566
+ )
567
+
568
+ @staticmethod
569
+ def _fn_result_to_body(
570
+ result: Any | None,
571
+ signature_tree: SignatureTree,
572
+ replace_fds: bool = True,
573
+ ) -> tuple[list[Any], list[int]]:
574
+ return _real_fn_result_to_body(result, signature_tree, replace_fds)
575
+
576
+ @staticmethod
577
+ def _c_fn_result_to_body(
578
+ result: Any | None,
579
+ signature_tree: SignatureTree,
580
+ replace_fds: bool,
581
+ ) -> tuple[list[Any], list[int]]:
582
+ """The high level interfaces may return single values which may be
583
+ wrapped in a list to be a message body. Also they may return fds
584
+ directly for type 'h' which need to be put into an external list."""
585
+ # https://github.com/cython/cython/issues/3327
586
+ return _real_fn_result_to_body(result, signature_tree, replace_fds)
587
+
588
+ @staticmethod
589
+ def _handle_signal(
590
+ interface: ServiceInterface, signal: _Signal, result: Any | None
591
+ ) -> None:
592
+ body, fds = ServiceInterface._fn_result_to_body(result, signal.signature_tree)
593
+ for bus in ServiceInterface._get_buses(interface):
594
+ bus._interface_signal_notify(
595
+ interface, interface.name, signal.name, signal.signature, body, fds
596
+ )
597
+
598
+ @staticmethod
599
+ def _get_property_value(
600
+ interface: ServiceInterface,
601
+ prop: _Property,
602
+ callback: Callable[[ServiceInterface, _Property, Any, Exception | None], None],
603
+ ) -> None:
604
+ # XXX MUST CHECK TYPE RETURNED BY GETTER
605
+ try:
606
+ if inspect.iscoroutinefunction(prop.prop_getter):
607
+ task: asyncio.Task = asyncio.ensure_future(prop.prop_getter(interface))
608
+
609
+ def get_property_callback(task_: asyncio.Task) -> None:
610
+ try:
611
+ result = task_.result()
612
+ except Exception as e:
613
+ callback(interface, prop, None, e)
614
+ return
615
+
616
+ callback(interface, prop, result, None)
617
+
618
+ task.add_done_callback(get_property_callback)
619
+ return
620
+
621
+ callback(
622
+ interface, prop, getattr(interface, prop.prop_getter.__name__), None
623
+ )
624
+ except Exception as e:
625
+ callback(interface, prop, None, e)
626
+
627
+ @staticmethod
628
+ def _set_property_value(
629
+ interface: ServiceInterface,
630
+ prop: _Property,
631
+ value: Any,
632
+ callback: Callable[[ServiceInterface, _Property, Exception | None], None],
633
+ ) -> None:
634
+ # XXX MUST CHECK TYPE TO SET
635
+ try:
636
+ if inspect.iscoroutinefunction(prop.prop_setter):
637
+ task: asyncio.Task = asyncio.ensure_future(
638
+ prop.prop_setter(interface, value)
639
+ )
640
+
641
+ def set_property_callback(task_: asyncio.Task) -> None:
642
+ try:
643
+ task_.result()
644
+ except Exception as e:
645
+ callback(interface, prop, e)
646
+ return
647
+
648
+ callback(interface, prop, None)
649
+
650
+ task.add_done_callback(set_property_callback)
651
+ return
652
+
653
+ setattr(interface, prop.prop_setter.__name__, value)
654
+ callback(interface, prop, None)
655
+ except Exception as e:
656
+ callback(interface, prop, e)
657
+
658
+ @staticmethod
659
+ def _get_all_property_values(
660
+ interface: ServiceInterface,
661
+ callback: Callable[[ServiceInterface, Any, Any, Exception | None], None],
662
+ user_data: Any | None = None,
663
+ ) -> None:
664
+ result: dict[str, Variant | None] = {}
665
+ result_error = None
666
+
667
+ for prop in ServiceInterface._get_properties(interface):
668
+ if prop.disabled or not prop.access.readable():
669
+ continue
670
+ result[prop.name] = None
671
+
672
+ if not result:
673
+ callback(interface, result, user_data, None)
674
+ return
675
+
676
+ def get_property_callback(
677
+ interface: ServiceInterface,
678
+ prop: _Property,
679
+ value: Any,
680
+ e: Exception | None,
681
+ ) -> None:
682
+ nonlocal result_error
683
+ if e is not None:
684
+ result_error = e
685
+ del result[prop.name]
686
+ else:
687
+ try:
688
+ result[prop.name] = Variant(prop.signature, value)
689
+ except SignatureBodyMismatchError as exc:
690
+ result_error = exc
691
+ del result[prop.name]
692
+
693
+ if any(v is None for v in result.values()):
694
+ return
695
+
696
+ callback(interface, result, user_data, result_error)
697
+
698
+ for prop in ServiceInterface._get_properties(interface):
699
+ if prop.disabled or not prop.access.readable():
700
+ continue
701
+ ServiceInterface._get_property_value(interface, prop, get_property_callback)