Differences between revisions 2 and 3
Revision 2 as of 2006-09-07 19:42:11
Size: 1889
Comment:
Revision 3 as of 2006-09-07 19:49:19
Size: 2166
Comment:
Deletions are marked like this. Additions are marked like this.
Line 25: Line 25:
remote_object = bus.get_object("org.freedesktop.DBus",
                               "/org/freedesktop/DBus")
remote_object = bus.get_object("org.freedesktop.DBus", # Connection name
                               "/org/freedesktop/DBus" # Object's path
                              
)
Line 53: Line 54:


= Calling an interface method =

After executing the introspection example:
{{{
# Get a particular interface
iface = dbus.Interface(remote_object, 'org.freedesktop.DBus')
print iface.ListNames()
}}}

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>

  • <interface name="org.freedesktop.DBus.Introspectable">

    • <method name="Introspect">

      • <arg name="data" direction="out" type="s"/>

      </method>

    </interface> <interface name="org.freedesktop.DBus">

    • <method name="RequestName">

      • <arg direction="in" type="s"/> <arg direction="in" type="u"/> <arg direction="out" type="u"/>

      </method>

  • ..

}}}

Calling an interface method

After executing the introspection example:

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

DbusExamples (last edited 2024-02-19 18:33:59 by Igo95862)

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