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