Revision 4 as of 2006-09-07 19:55:44

Clear message

The [http://www.freedesktop.org/wiki/Software_2fdbus D-Bus library] is a messaging library used by the GNOME desktop for interprocess communication.

There's a Python binding for it, but documentation is non-existent so I've collected these examples in hopes that they'll be useful and that people will update them as needed.

Warning: the D-Bus APIs change a lot, breaking code. These examples were run with version 0.60 of the Python interface (0.60-6ubuntu8, on Ubuntu Dapper); they probably won't run on other versions.

This [http://thaiopensource.org/development/suriyan/wiki/DbusNotes draft tutorial] is the clearest introduction to D-Bus that I've seen.

Introspection

# You must initialize the gobject/dbus support for threading
# before doing anything.
import gobject
gobject.threads_init()

from dbus import glib
glib.init_threads()

# Create a session bus.
import dbus
bus = dbus.SessionBus()

# Create an object that will proxy for a particular remote object.
remote_object = bus.get_object("org.freedesktop.DBus", # Connection name
                               "/org/freedesktop/DBus" # Object's path
                              )

# Introspection returns an XML document containing information
# about the methods supported by an interface.
print ("Introspection data:\n")
print remote_object.Introspect()

Output: {{{Introspection data:

<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"> <node>

}}}

If you get any of the connection or object names wrong, the exceptions you get are very obscure. For example, when I add an incorrect trailing slash to the object in the example above (making "/org/freedesktop/DBus/"), the exceptions are: {{{8223: arguments to dbus_message_new_method_call() were incorrect, assertion "_dbus_check_is_valid_path (path)" failed in file dbus-message.c line 797. This is normally a bug in some application using the D-BUS library. 8223: arguments to dbus_message_set_destination() were incorrect, assertion "message != NULL" failed in file dbus-message.c line 2728. This is normally a bug in some application using the D-BUS library. 8223: arguments to dbus_connection_send_with_reply() were incorrect, assertion "message != NULL" failed in file dbus-connection.c line 2430. This is normally a bug in some application using the D-BUS library.

Traceback (most recent call last):

AttributeError: 'NoneType' object has no attribute 'block' }}}

Notice that the exception is triggered in the .Introspect() call, not in the erroneous bus.get_object() call.

Calling an interface method

After executing the introspection example:

# Get a particular interface
iface = dbus.Interface(remote_object, 'org.freedesktop.DBus')
print iface.ListNames()

Output: {{{[u'org.freedesktop.DBus', u':1.3', u'org.freedesktop.Notifications', u'org.gnome.PowerManager', u':1.24', u':1.4', u':1.0', u'org.gnome.ScreenSaver', u':1.5', u':1.1', u':1.48', u':1.2']

}}}

Unable to edit the page? See the FrontPage for instructions.