Proxy Objects & Interfaces In DBussy

Lawrence D'Oliveiro ldo at geek-central.gen.nz
Wed May 24 11:26:34 UTC 2017


The dbus-python binding builds its proxy interface objects using a
construct like

    conn.get_object(«bus_name», «object_path») \
        .get_interface(«interface_name»)

onto which you can append the appropriate method call.

I decided it was more natural to make use of mapping-indexing syntax.
To break it down into separate steps, you create a BusPeer object as

    peer = conn[«bus_name»]

from which you get a proxy for the object at a specified path as

    obj = peer[«object_path»]

(No D-Bus traffic has happened as yet; the object might or might not
exist.) Then, from this you get the proxy interface with

    iface = obj.get_interface(«interface_name»)

This is the point where D-Bus traffic occurs to introspect the actual
proxy interface object. This then supports proxy method calls and proxy
property accesses in the usual way.

Of course, you can combine the steps into one, so the equivalent to the
original dbus-python expression is

    conn[«bus_name»][«object_path»].get_interface(«interface_name»)

The above proxy interface object does blocking calls. To create an
event-loop-friendly proxy interface, use the event-loop-friendly access
call:

    iface = await obj.get_interface_async(«interface_name»)

I also thought it would be handy to provide an alternative route
through the D-Bus access hierarchy: instead of bus name → object path →
interface name, you can go bus name → interface name → object path. In
this route, you construct a “root proxy” from the BusPeer:

    iface_root = peer.get_interface(«some_path», «interface_name»)

or

    iface_root = await peer.get_interface_async(«some_path», «interface_name»)

Here «some_path» is any valid object path that implements the
interface, just to do the introspection (the result should be valid
for any other object path that implements the same interface). The
result can be specialized for the actual object path you want to access
thus

    iface = iface_root[«object_path»]


More information about the dbus mailing list