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