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