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.
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-310-darwin.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-310-darwin.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-310-darwin.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 +578 -0
  18. dbus_fast/aio/message_reader.cpython-310-darwin.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 +208 -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 +686 -0
  29. dbus_fast/message.cpython-310-darwin.so +0 -0
  30. dbus_fast/message.pxd +76 -0
  31. dbus_fast/message.py +389 -0
  32. dbus_fast/message_bus.cpython-310-darwin.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-310-darwin.so +0 -0
  39. dbus_fast/service.pxd +50 -0
  40. dbus_fast/service.py +727 -0
  41. dbus_fast/signature.cpython-310-darwin.so +0 -0
  42. dbus_fast/signature.pxd +31 -0
  43. dbus_fast/signature.py +484 -0
  44. dbus_fast/unpack.cpython-310-darwin.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.1.2.dist-info/METADATA +262 -0
  49. dbus_fast-3.1.2.dist-info/RECORD +51 -0
  50. dbus_fast-3.1.2.dist-info/WHEEL +6 -0
  51. dbus_fast-3.1.2.dist-info/licenses/LICENSE +22 -0
@@ -0,0 +1,318 @@
1
+ import xml.etree.ElementTree as ET
2
+
3
+ from .. import introspection as intr
4
+ from ..constants import ErrorType
5
+ from ..errors import DBusError
6
+ from ..message import Message
7
+ from ..message_bus import BaseMessageBus
8
+ from ..proxy_object import BaseProxyInterface, BaseProxyObject
9
+ from ..signature import Variant
10
+ from ..unpack import unpack_variants as unpack
11
+
12
+ # glib is optional
13
+ try:
14
+ from gi.repository import GLib
15
+ except ImportError:
16
+ pass
17
+
18
+
19
+ class ProxyInterface(BaseProxyInterface):
20
+ """A class representing a proxy to an interface exported on the bus by
21
+ another client for the GLib :class:`MessageBus <dbus_fast.glib.MessageBus>`
22
+ implementation.
23
+
24
+ This class is not meant to be constructed directly by the user. Use
25
+ :func:`ProxyObject.get_interface()
26
+ <dbus_fast.glib.ProxyObject.get_interface>` on a GLib proxy
27
+ object to get a proxy interface.
28
+
29
+ This class exposes methods to call DBus methods, listen to signals, and get
30
+ and set properties on the interface that are created dynamically based on
31
+ the introspection data passed to the proxy object that made this proxy
32
+ interface.
33
+
34
+ A *method call* takes this form:
35
+
36
+ .. code-block:: python3
37
+
38
+ def callback(error: Exception, result: list(Any)):
39
+ pass
40
+
41
+ interface.call_[METHOD](*args, callback)
42
+ result = interface.call_[METHOD]_sync(*args)
43
+
44
+ Where ``METHOD`` is the name of the method converted to snake case.
45
+
46
+ To call a method, provide ``*args`` that correspond to the *in args* of the
47
+ introspection method definition.
48
+
49
+ To *asynchronously* call a method, provide a callback that takes an error
50
+ as the first argument and a list as the second argument. If the call
51
+ completed successfully, ``error`` will be :class:`None`. If the service
52
+ returns an error, it will be a :class:`DBusError <dbus_fast.DBusError>`
53
+ with information about the error returned from the bus. The result will be
54
+ a list of values that correspond to the *out args* of the introspection
55
+ method definition.
56
+
57
+ To *synchronously* call a method, use the ``call_[METHOD]_sync()`` form.
58
+ The ``result`` corresponds to the *out arg* of the introspection method
59
+ definition. If the method has more than one otu arg, they are returned
60
+ within a :class:`list`.
61
+
62
+ To *listen to a signal* use this form:
63
+
64
+ .. code-block:: python3
65
+
66
+ interface.on_[SIGNAL](callback)
67
+
68
+ To *stop listening to a signal* use this form:
69
+
70
+ .. code-block:: python3
71
+
72
+ interface.off_[SIGNAL](callback)
73
+
74
+ Where ``SIGNAL`` is the name of the signal converted to snake case.
75
+
76
+ DBus signals are exposed with an event-callback interface. The provided
77
+ ``callback`` will be called when the signal is emitted with arguments that
78
+ correspond to the *out args* of the interface signal definition.
79
+
80
+ To *get or set a property* use this form:
81
+
82
+ .. code-block:: python3
83
+
84
+ def get_callback(error: Exception, value: Any):
85
+ pass
86
+
87
+ def set_callback(error: Exception)
88
+ pass
89
+
90
+ interface.get_[PROPERTY](get_callback)
91
+ value: Any = interface.get_[PROPERTY]_sync()
92
+
93
+ interface.set_[PROPERTY](set_callback)
94
+ interface.set_[PROPERTY]_sync(value)
95
+
96
+ Where ``PROPERTY`` is the name of the property converted to snake case.
97
+
98
+ The ``value`` must correspond to the type of the property in the interface
99
+ definition.
100
+
101
+ To asynchronously get or set a property, provide a callback that takes an
102
+ :class:`Exception` as the first argument. If the call completed
103
+ successfully, ``error`` will be :class:`None`. If the service returns an
104
+ error, it will be a :class:`DBusError <dbus_fast.DBusError>` with
105
+ information about the error returned from the bus.
106
+
107
+ If the service returns an error for a synchronous DBus call, a
108
+ :class:`DBusError <dbus_fast.DBusError>` will be raised with information
109
+ about the error.
110
+ """
111
+
112
+ def _add_method(self, intr_method):
113
+ in_len = len(intr_method.in_args)
114
+ out_len = len(intr_method.out_args)
115
+
116
+ def method_fn(*args, unpack_variants: bool = False):
117
+ if len(args) != in_len + 1:
118
+ raise TypeError(
119
+ f"method {intr_method.name} expects {in_len} arguments and a callback (got {len(args)} args)"
120
+ )
121
+
122
+ args = list(args)
123
+ # TODO type check: this callback takes two parameters
124
+ # (MessageBus.check_callback(cb))
125
+ callback = args.pop()
126
+
127
+ def call_notify(msg, err):
128
+ if err:
129
+ callback([], err)
130
+ return
131
+
132
+ try:
133
+ BaseProxyInterface._check_method_return(
134
+ msg, intr_method.out_signature
135
+ )
136
+ except DBusError as e:
137
+ err = e
138
+
139
+ if unpack_variants:
140
+ callback(unpack(msg.body), err)
141
+ else:
142
+ callback(msg.body, err)
143
+
144
+ self.bus.call(
145
+ Message(
146
+ destination=self.bus_name,
147
+ path=self.path,
148
+ interface=self.introspection.name,
149
+ member=intr_method.name,
150
+ signature=intr_method.in_signature,
151
+ body=list(args),
152
+ ),
153
+ call_notify,
154
+ )
155
+
156
+ def method_fn_sync(*args, unpack_variants: bool = False):
157
+ main = GLib.MainLoop()
158
+ call_error = None
159
+ call_body = None
160
+
161
+ def callback(body, err):
162
+ nonlocal call_error
163
+ nonlocal call_body
164
+ call_error = err
165
+ call_body = body
166
+ main.quit()
167
+
168
+ method_fn(*args, callback)
169
+
170
+ main.run()
171
+
172
+ if call_error:
173
+ raise call_error
174
+
175
+ if not out_len:
176
+ return None
177
+
178
+ if unpack_variants:
179
+ call_body = unpack(call_body)
180
+
181
+ if out_len == 1:
182
+ return call_body[0]
183
+ return call_body
184
+
185
+ method_name = f"call_{BaseProxyInterface._to_snake_case(intr_method.name)}"
186
+ method_name_sync = f"{method_name}_sync"
187
+
188
+ setattr(self, method_name, method_fn)
189
+ setattr(self, method_name_sync, method_fn_sync)
190
+
191
+ def _add_property(self, intr_property):
192
+ def property_getter(callback, *, unpack_variants: bool = False):
193
+ def call_notify(msg, err):
194
+ if err:
195
+ callback(None, err)
196
+ return
197
+
198
+ try:
199
+ BaseProxyInterface._check_method_return(msg)
200
+ except Exception as e:
201
+ callback(None, e)
202
+ return
203
+
204
+ variant = msg.body[0]
205
+ if variant.signature != intr_property.signature:
206
+ err = DBusError(
207
+ ErrorType.CLIENT_ERROR,
208
+ 'property returned unexpected signature "{variant.signature}"',
209
+ msg,
210
+ )
211
+ callback(None, err)
212
+ return
213
+ if unpack_variants:
214
+ callback(unpack(variant.value), None)
215
+ else:
216
+ callback(variant.value, None)
217
+
218
+ self.bus.call(
219
+ Message(
220
+ destination=self.bus_name,
221
+ path=self.path,
222
+ interface="org.freedesktop.DBus.Properties",
223
+ member="Get",
224
+ signature="ss",
225
+ body=[self.introspection.name, intr_property.name],
226
+ ),
227
+ call_notify,
228
+ )
229
+
230
+ def property_getter_sync(*, unpack_variants: bool = False):
231
+ property_value = None
232
+ reply_error = None
233
+
234
+ main = GLib.MainLoop()
235
+
236
+ def callback(value, err):
237
+ nonlocal property_value
238
+ nonlocal reply_error
239
+ property_value = value
240
+ reply_error = err
241
+ main.quit()
242
+
243
+ property_getter(callback)
244
+ main.run()
245
+ if reply_error:
246
+ raise reply_error
247
+ if unpack_variants:
248
+ return unpack(property_value)
249
+ return property_value
250
+
251
+ def property_setter(value, callback):
252
+ def call_notify(msg, err):
253
+ if err:
254
+ callback(None, err)
255
+ return None
256
+ try:
257
+ BaseProxyInterface._check_method_return(msg)
258
+ except Exception as e:
259
+ callback(None, e)
260
+ return None
261
+
262
+ return callback(None, None)
263
+
264
+ variant = Variant(intr_property.signature, value)
265
+ self.bus.call(
266
+ Message(
267
+ destination=self.bus_name,
268
+ path=self.path,
269
+ interface="org.freedesktop.DBus.Properties",
270
+ member="Set",
271
+ signature="ssv",
272
+ body=[self.introspection.name, intr_property.name, variant],
273
+ ),
274
+ call_notify,
275
+ )
276
+
277
+ def property_setter_sync(val):
278
+ reply_error = None
279
+
280
+ main = GLib.MainLoop()
281
+
282
+ def callback(value, err):
283
+ nonlocal reply_error
284
+ reply_error = err
285
+ main.quit()
286
+
287
+ property_setter(val, callback)
288
+ main.run()
289
+ if reply_error:
290
+ raise reply_error
291
+
292
+ snake_case = super()._to_snake_case(intr_property.name)
293
+ setattr(self, f"get_{snake_case}", property_getter)
294
+ setattr(self, f"get_{snake_case}_sync", property_getter_sync)
295
+ setattr(self, f"set_{snake_case}", property_setter)
296
+ setattr(self, f"set_{snake_case}_sync", property_setter_sync)
297
+
298
+
299
+ class ProxyObject(BaseProxyObject):
300
+ """The proxy object implementation for the asyncio :class:`MessageBus <dbus_fast.aio.MessageBus>`.
301
+
302
+ For more information, see the :class:`BaseProxyObject <dbus_fast.proxy_object.BaseProxyObject>`.
303
+ """
304
+
305
+ def __init__(
306
+ self,
307
+ bus_name: str,
308
+ path: str,
309
+ introspection: intr.Node | str | ET.Element,
310
+ bus: BaseMessageBus,
311
+ ):
312
+ super().__init__(bus_name, path, introspection, bus, ProxyInterface)
313
+
314
+ def get_interface(self, name: str) -> ProxyInterface:
315
+ return super().get_interface(name)
316
+
317
+ def get_children(self) -> list["ProxyObject"]:
318
+ return super().get_children()