| 1 |
DBussy is yet another Python binding for accessing D-Bus |
| 2 |
<https://www.freedesktop.org/wiki/Software/dbus/>. I know there is |
| 3 |
already dbus-python <http://dbus.freedesktop.org/doc/dbus-python/>, |
| 4 |
among others |
| 5 |
<https://www.freedesktop.org/wiki/Software/DBusBindings/>. So why do |
| 6 |
we need another one? |
| 7 |
|
| 8 |
The main issue is one of event loops. Most of the existing bindings |
| 9 |
seem to be based around GLib. However, Python now has its own |
| 10 |
“asyncio” event-loop architecture |
| 11 |
<https://docs.python.org/3/library/asyncio.html>. This goes back to |
| 12 |
Python 3.4, but as of 3.5, you now have full-fledged coroutines |
| 13 |
(“async def” and “await”) as a language feature. |
| 14 |
|
| 15 |
Every GUI toolkit already provides its own event loop; so why did |
| 16 |
the Python developers decide to add yet another one? The answer |
| 17 |
seems clear: to provide a language-standard API for event loops, |
| 18 |
and a reference implementation for this API. It should be possible |
| 19 |
to adapt other event loops to this same API, and then Python |
| 20 |
code written to work with asyncio becomes event-loop agnostic. |
| 21 |
|
| 22 |
|
| 23 |
What Is D-Bus? |
| 24 |
============== |
| 25 |
|
| 26 |
D-Bus is a high-level interprocess communication protocol. It also |
| 27 |
provides a standard daemon, that is included with the main Linux |
| 28 |
desktop environments, that implements a set of standard “buses”: a |
| 29 |
“system” bus that is created at system boot time, and a “session” bus |
| 30 |
that belongs to each user who logs into one of these desktop |
| 31 |
environments. |
| 32 |
|
| 33 |
Processes can register their services on one of these buses--the |
| 34 |
system bus for systemwide access, or the session bus for per-user |
| 35 |
access--where other processes can find them by name and connect to |
| 36 |
them. Or they can accept connections on entirely separate networking |
| 37 |
sockets, without any dependency on the D-Bus daemon. libdbus, the |
| 38 |
reference implementation for the low-level D-Bus protocol, supports |
| 39 |
both modes of operation. |
| 40 |
|
| 41 |
D-Bus is based around the concept of passing messages conforming to |
| 42 |
a standard, extensible format. Messages are of four types: |
| 43 |
* a “method call” |
| 44 |
* a “method return” (normal response to a method call) |
| 45 |
* an “error” (abnormal response to a method call) |
| 46 |
* a “signal” notification |
| 47 |
|
| 48 |
A method-call message is how one process requests a service of another |
| 49 |
process via D-Bus; the usual response would be a method-return message |
| 50 |
in the other direction indicating the completion status of the service |
| 51 |
being performed; it is also possible to send method-call messages |
| 52 |
without expecting a reply. If there was something wrong with the |
| 53 |
method-call message (e.g. inappropriate parameters, lack of |
| 54 |
permissions), then the response would be an error message. One could |
| 55 |
also send a method-return with information indicating a failure to |
| 56 |
perform the requested service; presumably the choice between the types |
| 57 |
of response is that an error return indicates a condition that is not |
| 58 |
supposed to happen--a bug in the requesting program. |
| 59 |
|
| 60 |
Signal messages are sent as notifications of interesting events |
| 61 |
pertaining to the current session (for the session bus) or the entire |
| 62 |
system (for the system bus). They are usually not sent to a specific |
| 63 |
destination, but can be picked up by all interested processes on the |
| 64 |
bus. There are no replies to signals; if the receiving process cannot |
| 65 |
or will not process a particular message, it simply ignores it. |
| 66 |
|
| 67 |
Messages optionally include the following information: |
| 68 |
* a destination “bus name” indicating the process that is to |
| 69 |
receive the message (this is not the name of the bus, but the |
| 70 |
name of a connection to the bus) |
| 71 |
* an “object path” which looks like a POSIX absolute file name |
| 72 |
(always beginning with a slash and never ending with a slash, |
| 73 |
except for the root object “/”); the meaning of this is up to |
| 74 |
the receiving process, but it is intended to indicate some |
| 75 |
object within the hierarchy exposed by the process |
| 76 |
* an “interface name” which identifies the particular message |
| 77 |
protocol |
| 78 |
* a “method name” which identifies the particular function to be |
| 79 |
performed within that interface. |
| 80 |
|
| 81 |
Bus names and interface names look like domain names with the components |
| 82 |
reversed, so the top level is at the beginning. If you are familiar with |
| 83 |
package names in Java, they take the same form, and with the same |
| 84 |
intent: to reduce the chance of name conflicts. |
| 85 |
|
| 86 |
D-Bus also includes an extensive, but not extensible, type system for |
| 87 |
encoding data in a message. This data represents arguments to the |
| 88 |
method call or signal, return results for a method return or the error |
| 89 |
name and message for an error. A message contains a sequence of 0, 1 |
| 90 |
or more items of such data, each of which can be of various types: |
| 91 |
“basic” types (e.g. integer, float, string) or “container” types |
| 92 |
(structs, arrays, dictionaries) which in turn contain more values, |
| 93 |
each of which in turn can be of a basic or (recursively) another |
| 94 |
container type. A “signature” is a string encoding the type of a |
| 95 |
value, or sequence of values; there is also a “variant” type, which |
| 96 |
means the type of the value is encoded dynamically with the value |
| 97 |
itself, separate from the signature. |
| 98 |
|
| 99 |
The importance of type signatures is really up to the particular |
| 100 |
programs that are trying to communicate: some might insist on values |
| 101 |
exactly matching the expected type signature, whereas others might be |
| 102 |
more lenient. For example, while the D-Bus type system specifies |
| 103 |
different basic types for different sizes of integers of signed or |
| 104 |
unsigned varieties, most Python code will probably not care about the |
| 105 |
specific distinctions, and treat all these values as of type “int”. |
| 106 |
|
| 107 |
|
| 108 |
Standard Interfaces |
| 109 |
------------------- |
| 110 |
|
| 111 |
D-Bus defines some standard interfaces which are meant to be |
| 112 |
understood by most if not all services. |
| 113 |
|
| 114 |
One fundamental one is the “org.freedesktop.DBus.Introspectable” |
| 115 |
interface; this defines an “Introspect” method, that is expected to |
| 116 |
return an XML string that describes all the interfaces understood by |
| 117 |
the object identified by the object path, as well as listing all the |
| 118 |
available child objects that can be accessed by appending a slash and |
| 119 |
the child name to the parent object path, if any. Introspection is a |
| 120 |
very important part of D-Bus: it is what allows users to discover what |
| 121 |
services are available on their installations, and throw together |
| 122 |
ad-hoc scripts in Python or other high-level languages to make |
| 123 |
convenient use of such services, without having to write a lot of |
| 124 |
interfacing code. |
| 125 |
|
| 126 |
Another commonly-supported interface is called |
| 127 |
“org.freedesktop.DBus.Properties”. This one defines the concept of |
| 128 |
*properties*, which are pieces of data notionally attached to object |
| 129 |
paths, and which might be readable, writable or both. This interface |
| 130 |
defines standard methods to get a property value for an object, set a |
| 131 |
new property value, or get all properties defined on an object. It |
| 132 |
also specifies a signal that can be sent by a server process as a |
| 133 |
general notification to all peers on the bus about changes to its |
| 134 |
property values. |
| 135 |
|
| 136 |
|
| 137 |
Enter DBussy |
| 138 |
============ |
| 139 |
|
| 140 |
DBussy allows you to take advantage of asyncio, but it doesn’t force |
| 141 |
you to use it. DBussy is meant to give you access to (nearly) all the |
| 142 |
functionality of the underlying libdbus library |
| 143 |
<https://dbus.freedesktop.org/doc/api/html/index.html>. libdbus is a |
| 144 |
very low-level, very general library, designed to be called from C |
| 145 |
code, that makes no assumptions about event loops at all. Consider the |
| 146 |
basic task in a client program of sending a D-Bus request message and |
| 147 |
waiting for a reply; or consider a server waiting for a message to |
| 148 |
come in. libdbus offers 3 different ways to handle this: |
| 149 |
|
| 150 |
* poll repeatedly until the message appears |
| 151 |
* block the thread until the message comes in |
| 152 |
* specify a callback to be notified when the message is available |
| 153 |
|
| 154 |
DBussy offers access to all these ways. But it also gives you the |
| 155 |
option of engaging the asyncio event loop. This means you can be doing |
| 156 |
other things in the loop, and when a message comes in, it can be |
| 157 |
passed automatically to a callback that you previously specified. |
| 158 |
|
| 159 |
It also gives clients another way of sending a message and waiting for |
| 160 |
the reply: using a coroutine. For example |
| 161 |
|
| 162 |
async def request_reply(connection) : |
| 163 |
message = dbussy.Message.new_method_call(...) |
| 164 |
... other setup of message args etc ... |
| 165 |
reply = await connection.send_await_reply(message, timeout) |
| 166 |
... process reply ... |
| 167 |
#end request_reply |
| 168 |
|
| 169 |
loop = asyncio.get_event_loop() |
| 170 |
dbus_task = loop.create_task(request_reply(connection)) |
| 171 |
... maybe create other tasks to run while awaiting reply ... |
| 172 |
loop.run_until_complete(dbus_task) |
| 173 |
|
| 174 |
On the server side, you can correspondingly use coroutines to handle |
| 175 |
time-consuming requests without blocking the main loop. A message |
| 176 |
filter or object-path message callback can return, instead of |
| 177 |
DBUS.HANDLER_RESULT_HANDLED, a coroutine. The wrapper code will give |
| 178 |
libdbus a result of DBUS.HANDLER_RESULT_HANDLED on your behalf, after |
| 179 |
creating a task to execute the coroutine on the event loop. The |
| 180 |
coroutine can then go on to handle the actual processing of the |
| 181 |
request, and return the reply at some later stage. |
| 182 |
|
| 183 |
The dbussy module also offers several more Pythonic facilities beyond |
| 184 |
those of the underlying libdbus, including a higher-level |
| 185 |
representation of type signatures as Type objects (and subclasses |
| 186 |
thereof), and an Introspection object hierarchy that can be easily |
| 187 |
converted to and from the standard D-Bus introspection XML |
| 188 |
representation. |
| 189 |
|
| 190 |
|
| 191 |
No Type-Guessing! |
| 192 |
================= |
| 193 |
|
| 194 |
Unlike some other Python bindings for D-Bus libraries, DBussy never |
| 195 |
tries to guess how to convert Python types to D-Bus types. For |
| 196 |
example, D-Bus provides many different sizes of integer, in both |
| 197 |
signed and unsigned variants; when retrieving an integer parameter |
| 198 |
from a message, DBussy will convert all of these to Python integers, |
| 199 |
but when putting a Python integer into a message parameter, there must |
| 200 |
always be a signature somewhere specifying which specific D-Bus |
| 201 |
integer type to convert it to. |
| 202 |
|
| 203 |
Also, D-Bus variants are always represented in Python as a 2-tuple: |
| 204 |
the first element is the signature of the actual type being passed, |
| 205 |
and the second is the value of that type. |
| 206 |
|
| 207 |
|
| 208 |
Multithreading Caveats |
| 209 |
====================== |
| 210 |
|
| 211 |
While libdbus was supposed to be usable with multithreaded code, in |
| 212 |
practice this has turned out to be problematic |
| 213 |
<https://lists.freedesktop.org/archives/dbus/2018-March/017428.html>. |
| 214 |
The whole point of Python’s asyncio framework is to avoid |
| 215 |
multithreading anyway. |
| 216 |
|
| 217 |
The dbussy module does use multithreading in a very limited way, |
| 218 |
in just one place: the Connection.open_async() call. It does this |
| 219 |
to give the effect of a nonblocking equivalent to a call for which |
| 220 |
libdbus only offers a blocking version. |
| 221 |
|
| 222 |
|
| 223 |
Ravel: The Higher-Level Interface |
| 224 |
================================= |
| 225 |
|
| 226 |
Rather than directly manipulating D-Bus message objects, it is usually |
| 227 |
more convenient to have a representation where D-Bus object paths are |
| 228 |
directly mapped to Python objects, and D-Bus method and signal calls |
| 229 |
are similarly mapped to calls on methods of those Python objects. So |
| 230 |
on the client side, the local Python objects become “proxies” for the |
| 231 |
actual objects implemented by the remote server. And on the server |
| 232 |
side, the implementation of an interface can be wrapped up in an |
| 233 |
“interface class” with methods that are automatically invoked in |
| 234 |
response to incoming D-Bus requests. |
| 235 |
|
| 236 |
Interface classes can also be used on the client side: in this |
| 237 |
situation, the method calls are just stubs used for type-checking |
| 238 |
outgoing requests, while the signal definitions can be real functions |
| 239 |
which are invoked in response to incoming signal messages. Conversely, |
| 240 |
on the server side, the signal definitions are stubs used for |
| 241 |
type-checking, while the method definitions (in the D-Bus sense) are |
| 242 |
real functions implementing those calls. |
| 243 |
|
| 244 |
An interface class can also be both client-side and server-side in |
| 245 |
one, which means all the method definitions are real, none are stubs. |
| 246 |
So it can be used both for type-checking outgoing messages and |
| 247 |
handling incoming ones. For example, this is true of both the common |
| 248 |
standard interfaces (Introspectable and Properties), since most if not |
| 249 |
all peers are expected to support them. |
| 250 |
|
| 251 |
(Signal definitions are a special case: even in a |
| 252 |
client-and-server-side interface, they can be marked as stubs--as in |
| 253 |
the standard PropertyHandler interface. This allows you to register |
| 254 |
such an interface for introspection purposes, without having to accept |
| 255 |
its handling of any signals.) |
| 256 |
|
| 257 |
Both kinds of interface representations are provided by the “ravel” |
| 258 |
module--interface classes on the client or server side, and proxy |
| 259 |
interfaces on the client side. Ravel also offers different ways to |
| 260 |
construct a proxy interface: you can define it yourself, or you can |
| 261 |
have Ravel construct it automatically for you by introspecting the |
| 262 |
server-side object. |
| 263 |
|
| 264 |
Either way, you start by creating a ravel.Connection object, which is a |
| 265 |
wrapper around a lower-level dbussy.Connection object. You can get one |
| 266 |
for the session or system bus by calling ravel.session_bus() and |
| 267 |
ravel.system_bus() respectively, or you can use a ravel.Server object |
| 268 |
(wrapping around the corresponding dbussy.Server) to accept |
| 269 |
connections on your own network address, separate from the D-Bus |
| 270 |
daemon. |
| 271 |
|
| 272 |
|
| 273 |
The Client Side: Proxy Interfaces |
| 274 |
--------------------------------- |
| 275 |
|
| 276 |
Proxy interfaces can be easily constructed in different ways. One way |
| 277 |
is to start with a proxy for a bus peer with a particular name. You |
| 278 |
get one of these with an expression that treats the connection as |
| 279 |
though it were a mapping: |
| 280 |
|
| 281 |
peer = conn[«bus_name»] |
| 282 |
|
| 283 |
Then you do another lookup on this mapping to get a reference to a |
| 284 |
particular object path at that peer: |
| 285 |
|
| 286 |
obj = peer[«object_path»] |
| 287 |
|
| 288 |
Now, you can get a proxy for a desired interface thus: |
| 289 |
|
| 290 |
iface = obj.get_interface(«interface_name») |
| 291 |
|
| 292 |
which causes automatic introspection of that object path on the peer |
| 293 |
to obtain all the necessary type information for that interface (if it |
| 294 |
is not one of the standard interfaces). So calling a Python method on |
| 295 |
this object |
| 296 |
|
| 297 |
results = iface.«method»(«args») |
| 298 |
|
| 299 |
translates automatically to the corresponding D-Bus method call to |
| 300 |
that object and interface on the remote server, with full type |
| 301 |
checking done on both arguments and results. |
| 302 |
|
| 303 |
Note that the method result is always a list. |
| 304 |
|
| 305 |
D-Bus properties are automatically mapped to Python properties, |
| 306 |
so you can access their values and assign new ones in the usual |
| 307 |
Python way. For example, adding 1 to a numeric property (written |
| 308 |
out the long way to demonstrate property access on both the LHS |
| 309 |
and RHS of the assignment): |
| 310 |
|
| 311 |
iface = conn[«bus_name»][«path»] \ |
| 312 |
.get_interface(«interface_name») |
| 313 |
iface.«prop» = iface.«prop» + 1 |
| 314 |
|
| 315 |
The above are *blocking* calls, which means the current thread is |
| 316 |
blocked while waiting for the reply to the method call. If you want to |
| 317 |
do things in a more event-loop-friendly fashion, then use |
| 318 |
get_async_interface instead of get_interface, which returns a |
| 319 |
coroutine object that evaluates to an asynchronous version of the |
| 320 |
proxy object when it finally completes. Method calls and property |
| 321 |
accesses on this are automatically also coroutine calls, so you can |
| 322 |
use them in await-constructs in your coroutines, or create asyncio |
| 323 |
tasks to run them etc. |
| 324 |
|
| 325 |
Here is an example use of the above calls, which pops up a GUI |
| 326 |
notification displaying a short message for 5 seconds. Because the |
| 327 |
introspection of this interface supplies names for the arguments, it |
| 328 |
is possible to pass them to the method call by keyword: |
| 329 |
|
| 330 |
ravel.session_bus()["org.freedesktop.Notifications"]["/org/freedesktop/Notifications"] \ |
| 331 |
.get_interface("org.freedesktop.Notifications") \ |
| 332 |
.Notify \ |
| 333 |
( |
| 334 |
app_name = "test", |
| 335 |
replaces_id = 0, |
| 336 |
app_icon = "dialog-information", |
| 337 |
summary = "Hello World!", |
| 338 |
body = "DBussy works!", |
| 339 |
actions = [], |
| 340 |
hints = {}, |
| 341 |
timeout = 5000, |
| 342 |
) |
| 343 |
|
| 344 |
(But note that the argument names might differ, depending on your |
| 345 |
Linux distro version. If you get errors saying certain argument names |
| 346 |
are not understood, try it without the argument names. Or do your own |
| 347 |
introspection of the interface, to decide what argument names should |
| 348 |
be used.) |
| 349 |
|
| 350 |
|
| 351 |
Proxy Interfaces: Alternative Order |
| 352 |
----------------------------------- |
| 353 |
|
| 354 |
The above access to proxy interfaces could be described as |
| 355 |
“bus name-path-interface”, after the order in which the components |
| 356 |
are specified. Proxies can also be obtained in “bus name-interface-path” |
| 357 |
order. This can be convenient for obtaining a proxy interface object |
| 358 |
that can then be used to make calls on multiple objects. |
| 359 |
|
| 360 |
In this method, an initial *root* proxy is obtained thus: |
| 361 |
|
| 362 |
iface_root = conn[«bus_name»].get_interface \ |
| 363 |
( |
| 364 |
path = «initial_path», |
| 365 |
interface = «interface_name», |
| 366 |
) |
| 367 |
|
| 368 |
Note you need to specify an object path for this initial introspection; |
| 369 |
it is probably best to use the shortest (highest-level) path that |
| 370 |
supports that interface. Depending on the peer, the root path “/” might work. |
| 371 |
|
| 372 |
From this, you get the proxy for the interface on an actual object by |
| 373 |
using the object path as a lookup key, e.g. |
| 374 |
|
| 375 |
iface = iface_root[«path»] |
| 376 |
|
| 377 |
From here, you can invoke the method calls and access properties |
| 378 |
in the same way as before, e.g. |
| 379 |
|
| 380 |
iface.«method»(«args») |
| 381 |
... iface.«prop» ... |
| 382 |
|
| 383 |
|
| 384 |
Asynchronous Properties |
| 385 |
----------------------- |
| 386 |
|
| 387 |
As mentioned, both property and method access can be done asynchronously |
| 388 |
on an event loop. Asynchronous *reading* of a property is easy |
| 389 |
enough to express: |
| 390 |
|
| 391 |
val = await obj.«prop» |
| 392 |
|
| 393 |
But how do you do asynchronous *writing* of the property? The obvious |
| 394 |
construct |
| 395 |
|
| 396 |
await obj.«prop» = newval |
| 397 |
|
| 398 |
produces a syntax error: Python doesn’t (at least as of 3.6!) allow |
| 399 |
“await” on the left-hand side of an assignment. Instead, you |
| 400 |
write it as though it were a blocking call: |
| 401 |
|
| 402 |
obj.«prop» = newval |
| 403 |
|
| 404 |
but because the interface is defined as asynchronous, this causes |
| 405 |
a task to be queued on the event loop to oversee the completion of |
| 406 |
the set-property call, and your code can continue execution before |
| 407 |
this task completes. |
| 408 |
|
| 409 |
The main consequence of this is that any error exception will be |
| 410 |
raised asynchronously. But if you don’t like the idea that execution |
| 411 |
will be deferred, you can await the completion of all such pending |
| 412 |
property-setting calls with the following call on the root proxy: |
| 413 |
|
| 414 |
await iface_root.set_prop_flush() |
| 415 |
|
| 416 |
or on the actual proxy interface: |
| 417 |
|
| 418 |
await ravel.set_prop_flush(iface) |
| 419 |
|
| 420 |
whichever is more convenient. This means you can batch up a whole |
| 421 |
series of property-setting calls on any number of objects on the same |
| 422 |
interface and bus name, then wait for them all to complete with a |
| 423 |
single flush call. |
| 424 |
|
| 425 |
|
| 426 |
Interface Classes |
| 427 |
----------------- |
| 428 |
|
| 429 |
The more structured high-level interface offered by Ravel is built |
| 430 |
around the concept of an *interface class*, which is a Python class |
| 431 |
that represents a D-Bus interface, either as an actual implementation |
| 432 |
or as a “proxy” for making calls to another bus peer. You can then |
| 433 |
register instances of this class on a bus connection at selected |
| 434 |
points in your object path hierarchy, to handle either only specific |
| 435 |
objects at those paths or as a fallback to also deal with objects |
| 436 |
at points below those, that do not have their own instance of this |
| 437 |
class registered. |
| 438 |
|
| 439 |
An interface class is identified by applying the @ravel.interface() |
| 440 |
decorator to the class definition, specifying the kind of interface |
| 441 |
(for use client-side, server-side or both), and the interface name, e.g. |
| 442 |
|
| 443 |
@ravel.interface(ravel.INTERFACE.SERVER, name = "com.example.my_interface") |
| 444 |
class MyClass : |
| 445 |
... |
| 446 |
#end MyClass |
| 447 |
|
| 448 |
The meanings of the first, “kind”, argument to @ravel.interface are |
| 449 |
as follows: |
| 450 |
* INTERFACE.SERVER -- you are the server implementing the method |
| 451 |
calls defined by this interface. However, the signal definitions |
| 452 |
are just “stubs” used for type-checking when you send those signals |
| 453 |
over the bus. This interface definition can also be introspected |
| 454 |
to inform users about the facilities provided by the interface. |
| 455 |
* INTERFACE.CLIENT -- you are a client wanting to communicate with |
| 456 |
a server that implements this interface. The method calls are |
| 457 |
just stubs used for type-checking when you send those calls over |
| 458 |
the bus. The signal definitions can be your actual functions |
| 459 |
that you want to be invoked when those signals are received, or |
| 460 |
they can also be stubs. |
| 461 |
* INTERFACE.CLIENT_AND_SERVER -- both of the above; you implement |
| 462 |
the methods, and maybe the signals as well, and you can also use |
| 463 |
their definitions to send corresponding method and signal calls to |
| 464 |
peers that implement the same interface. The standard interfaces |
| 465 |
(Peer, Introspectable, Properties) are defined in this way. |
| 466 |
|
| 467 |
Within such a class, Python methods that are to handle D-Bus method |
| 468 |
calls are identified with the @ravel.method() decorator, e.g.: |
| 469 |
|
| 470 |
@ravel.method \ |
| 471 |
( |
| 472 |
name = ..., |
| 473 |
in_signature = ..., |
| 474 |
out_signature = ..., |
| 475 |
args_keyword = ..., |
| 476 |
arg_keys = ..., |
| 477 |
arg_attrs = ..., |
| 478 |
result_keyword = ..., |
| 479 |
result_keys = ..., |
| 480 |
result_attrs = ..., |
| 481 |
connection_keyword = ..., |
| 482 |
message_keyword = ..., |
| 483 |
path_keyword = ..., |
| 484 |
bus_keyword = ..., |
| 485 |
set_result_keyword = ..., |
| 486 |
... |
| 487 |
) |
| 488 |
def my_method(...) : |
| 489 |
... |
| 490 |
#end my_method |
| 491 |
|
| 492 |
As you can see, there are a large number of options for implementing |
| 493 |
such a method. It can also be defined as a coroutine with async def if |
| 494 |
you have an event loop attached, and Ravel will automatically queue |
| 495 |
the task for execution and await any returned result. Partial summary |
| 496 |
of arguments: |
| 497 |
* name -- the D-Bus method name. If omitted, defaults to the Python |
| 498 |
function name. |
| 499 |
* in_signature -- the D-Bus signature specifying the arguments (zero or more) |
| 500 |
to the method. |
| 501 |
* out_signature -- the D-Bus signature specifying the results (zero |
| 502 |
or more) the method will return. |
| 503 |
* args_keyword -- the name of an argument to the Python function that will |
| 504 |
be set to the arguments from the message method call. The arguments |
| 505 |
will be passed as a list, or a dict, or an attributed class, depending the |
| 506 |
specification of arg_keys and arg_attrs (see below). |
| 507 |
* path_keyword -- if specified, then the object path field from the |
| 508 |
incoming method call will be passed to the Python function as the |
| 509 |
value of the argument with this name. |
| 510 |
* message_keyword -- if specified, then the dbussy.Message object |
| 511 |
for the incoming method call will be passed to the Python function |
| 512 |
as the value of the argument with this name. |
| 513 |
* connection_keyword -- if specified, then the dbussy.Connection object |
| 514 |
will be passed to the Python function as the value of the argument |
| 515 |
with this name. |
| 516 |
* bus_keyword -- if specified, then the ravel.Connection object |
| 517 |
will be passed to the Python function as the value of the argument |
| 518 |
with this name. |
| 519 |
* set_result_keyword -- if specified, then a function of a single |
| 520 |
argument will be passed to the Python function as the value of the |
| 521 |
argument with this name; the argument passed by calling this |
| 522 |
function becomes the method result. |
| 523 |
|
| 524 |
Passing arguments: the argument with the name given by args_keyword |
| 525 |
will hold the extracted arguments from the method call message. If |
| 526 |
neither of arg_keys or arg_attrs is specified, then the arguments are |
| 527 |
passed as a list. If arg_keys is specified, then it must be a sequence |
| 528 |
of names that must match the number of types specified by the |
| 529 |
in_signature; in this case, the args will be passed as a dict with the |
| 530 |
keys given in arg_keys associated in order with the argument values. |
| 531 |
|
| 532 |
If arg_attrs is specified instead of arg_keys, then it must be a |
| 533 |
sequence of names that must match the number of types specified by the |
| 534 |
in_signature; a mutable attributed class object is created by calling |
| 535 |
ravel.def_attr_class, with the attribute names taken from arg_keys |
| 536 |
assigned in order to the argument values. |
| 537 |
|
| 538 |
Returning results: the function can return the result values to be |
| 539 |
inserted into the method-return message as the function result, by |
| 540 |
assigning to elements of a mutable result argument (passed as the |
| 541 |
argument named by result_keyword, or by calling the set_result |
| 542 |
function that was passed via the set_result_keyword (above). |
| 543 |
|
| 544 |
If neither result_keys nor result_attrs is specified, then the result |
| 545 |
is expected to be a sequence of values matching the out_signature. If |
| 546 |
it is returned as the function result, then it can be a tuple or list; |
| 547 |
but if result_keyword is specified, then the value of this is a list, |
| 548 |
and the values in the sequence must be assigned to the elements of |
| 549 |
this list in-place. |
| 550 |
|
| 551 |
If result_keys is specified, then the result is a dict mapping the |
| 552 |
names from result_keys to the values of the result sequence in order. |
| 553 |
If result_attrs is specified, then the result is a mutable attributed |
| 554 |
class object created by calling ravel.def_attr_class, mapping the |
| 555 |
names from result_attrs to the values of the result sequence in order. |
| 556 |
If result_keyword is not specified, then the result object is expected |
| 557 |
to be returned as the function result; otherwise, it is passed as the |
| 558 |
value of the argument named by result_keyword, and the handler is |
| 559 |
supposed to update its elements in-place. |
| 560 |
|
| 561 |
Signal definitions look similar, except they return no results: |
| 562 |
|
| 563 |
@ravel.signal \ |
| 564 |
( |
| 565 |
name = ..., |
| 566 |
in_signature = ..., |
| 567 |
args_keyword = ..., |
| 568 |
arg_keys = ..., |
| 569 |
arg_attrs = ..., |
| 570 |
connection_keyword = ..., |
| 571 |
message_keyword = ..., |
| 572 |
path_keyword = ..., |
| 573 |
bus_keyword = ..., |
| 574 |
stub = ..., |
| 575 |
... |
| 576 |
) |
| 577 |
def my_signal(...) : |
| 578 |
... |
| 579 |
#end my_signal |
| 580 |
|
| 581 |
Also note the “stub” argument--this has meaning on a client-side |
| 582 |
interface to indicate that the interface class does not implement |
| 583 |
the listener for the signal, but that it is registered separately |
| 584 |
with a listen_signal call. This is used for the PropertiesChanged |
| 585 |
signal in ravel.PropertyHandler (the standard handler for the |
| 586 |
DBUS.INTERFACE_PROPERTIES interface), so that you do not have |
| 587 |
to replace the class just to install your own listeners for this |
| 588 |
signal. |
| 589 |
|
| 590 |
Properties are defined by implementing getter and/or setter |
| 591 |
methods, identified by @propgetter() and @propsetter() decorators |
| 592 |
respectively: |
| 593 |
|
| 594 |
@ravel.propgetter \ |
| 595 |
( |
| 596 |
name = ..., |
| 597 |
type = ..., |
| 598 |
name_keyword = ..., |
| 599 |
connection_keyword = ..., |
| 600 |
message_keyword = ..., |
| 601 |
path_keyword = ..., |
| 602 |
bus_keyword = ..., |
| 603 |
change_notification = ... |
| 604 |
) |
| 605 |
def my_propgetter(...) : |
| 606 |
... |
| 607 |
return \ |
| 608 |
«value» |
| 609 |
#end my_propgetter |
| 610 |
|
| 611 |
@ravel.propsetter \ |
| 612 |
( |
| 613 |
name = ..., |
| 614 |
type = ..., |
| 615 |
name_keyword = ..., |
| 616 |
type_keyword = ..., |
| 617 |
value_keyword = ..., |
| 618 |
connection_keyword = ..., |
| 619 |
message_keyword = ..., |
| 620 |
path_keyword = ..., |
| 621 |
bus_keyword = ... |
| 622 |
) : |
| 623 |
def my_propsetter(...) : |
| 624 |
... |
| 625 |
#end my_propsetter |
| 626 |
|
| 627 |
Note the following arguments: |
| 628 |
* type -- the type signature for permitted property values. |
| 629 |
* change_notification -- one of the dbussy.PROP_CHANGE_NOTIFICATION values |
| 630 |
indicating whether (and what kind of) signals should be generated for |
| 631 |
changes to this property value. This is specified on the @propgetter(), |
| 632 |
because there is no point notifying about write-only properties. |
| 633 |
* type_keyword -- for passing the actual type of the new property value |
| 634 |
to the setter. |
| 635 |
* value_keyword -- for passing the new property value to the setter. |
| 636 |
|
| 637 |
Getters and setters can be coroutines. |
| 638 |
|
| 639 |
|
| 640 |
Custom User Data |
| 641 |
---------------- |
| 642 |
|
| 643 |
With Ravel’s interface classes, it is possible to attach your own |
| 644 |
user data items to arbitrary points in the object path tree. To |
| 645 |
obtain the user data dictionary for a given object path, do either |
| 646 |
|
| 647 |
user_data = bus.user_data["/com/example/myapp"] |
| 648 |
|
| 649 |
or |
| 650 |
|
| 651 |
user_data = bus.user_data["com", "example", "myapp"] |
| 652 |
|
| 653 |
The result is a dictionary into which you can insert whatever |
| 654 |
key-value pairs you like, e.g.: |
| 655 |
|
| 656 |
user_data["com.example.myapp.attribs"] = MyObj(...) |
| 657 |
|
| 658 |
|
| 659 |
Predefined Interface Classes |
| 660 |
---------------------------- |
| 661 |
|
| 662 |
Ravel provides predefined interface classes for the |
| 663 |
org.freedesktop.DBus.Peer, org.freedesktop.DBus.Introspectable and |
| 664 |
org.freedesktop.DBus.Properties interfaces, and these are |
| 665 |
automatically registered on Connection instances. The Peer interface |
| 666 |
is just a stub, since the actual implementation is hard-coded into |
| 667 |
libdbus itself; it is there to provide automatic introspection of this |
| 668 |
interface. |
| 669 |
|
| 670 |
The ravel.IntrospectionHandler class defines the standard |
| 671 |
Introspectable interface, and provides automatic introspection of all |
| 672 |
interfaces registered with a ravel.Connection (including itself and |
| 673 |
the other standard interfaces). It extracts the information specified |
| 674 |
to the class, method, signal and property-handler decorators, and |
| 675 |
generates the appropriate XML form for returning to D-Bus queries. |
| 676 |
|
| 677 |
The ravel.PropertyHandler class defines the standard Properties |
| 678 |
interface, and automatically dispatches to @propgetter() and |
| 679 |
@propsetter() methods as defined in your registered interface classes. |
| 680 |
|
| 681 |
The ravel.ManagedObjectsHandler class defines the standard |
| 682 |
ObjectManager interface. It handles sending out of notifications as |
| 683 |
you call the object_added() and object_removed() methods on a |
| 684 |
Connection. It is not automatically registered on a Connection object; |
| 685 |
you have to add it manually. If you already have a ravel.Connection |
| 686 |
object «conn», then you can do so with |
| 687 |
|
| 688 |
«conn».register_additional_standard(managed_objects = True) |
| 689 |
|
| 690 |
Alternatively, you can request this registration at the time of |
| 691 |
creating the Connection object in one step, for example with |
| 692 |
|
| 693 |
bus = ravel.session_bus(managed_objects = True) |
| 694 |
|
| 695 |
|
| 696 |
DBussy Examples |
| 697 |
=============== |
| 698 |
|
| 699 |
Sample code illustrating how to use DBussy/Ravel is available in my |
| 700 |
dbussy_examples repo on GitLab <https://gitlab.com/ldo/dbussy_examples> |
| 701 |
and BitBucket <https://bitbucket.org/ldo17/dbussy_examples>. |
| 702 |
|
| 703 |
|
| 704 |
How Do You Pronounce “DBussy”? |
| 705 |
============================== |
| 706 |
|
| 707 |
The name is a pun on “dbus” and the name of French Impressionist |
| 708 |
composer Claude Debussy. The most natural way to pronounce it would be |
| 709 |
the same as his name. At least, that’s my story, and I’m sticking to |
| 710 |
it. |
| 711 |
|
| 712 |
|
| 713 |
Lawrence D'Oliveiro <[email protected]> |
| 714 |
2023 September 27 |