Python Programming/Dbus
From Wikibooks, the open-content textbooks collection
| Previous: PyQt4 | Index | Next: pyFormex |
Dbus is a way for processes to communicate with each other. For example, programs like Pidgin instant messenger allow other programs to find out or change the status; and services like network-manager publish what internet connection is active. Programs that sometimes connenct to the internet can then pick the best time to download updates to the system.
Contents |
[edit] Buses
Messages are sent along buses. Services attach themselves to these buses, and allow clients to pass messages to and from them.
There are two main buses, the system bus and session bus. Services on the system bus affect the whole system, such as providing information about the network or disk drives. Services on the session bus provide access to programs running on the desktop, like Pidgin.
import dbus sys_bus = dbus.SystemBus()
[edit] Objects and interfaces
Services attached to a bus can be contacted using their well known name. While this could be any string, the format is normally that of a reverse domain name: an example for a spreadsheet program called "CalcProgram" from "My Corp Inc." could be "com.mycorp.CalcProgram".
Services publish objects using slash-seperated paths (this is similar to webpages). Someone on dbus can request an object if they know this path.
The object passed back is not a full object: it just refers to the service's copy of the object. It is called a proxy object.
proxy_for_cell_a2 = sys_bus.get_object('com.mycorp.CalcProgram', '/spreadsheet1/cells/a2')
Before the proxy object can be used, we need to specify what type of object it is. We do this by creating an interface object.
cell_a2 = dbus.Interface(proxy_for_cell_a2, 'com.mycorp.CalcProgram.SpreadsheetCell')
Whatever methods are set up for this type of object can be called:
cell_a2.getContents()
| Name | Example | Description |
|---|---|---|
| service well known name | com.mycorp.CalcProgram | Identifies the application |
| path of an object | /spreadsheet1/cells/a2 | Identifies an object published by a service |
| interface | com.mycorp.CalcProgram.SpreadsheetCell | Identifies what type of object we expect |
[edit] Some examples
import dbus sys_bus = dbus.SystemBus() # get an object called / in org.freedesktop.Avahi to talk to raw_server = sys_bus.get_object('org.freedesktop.Avahi', '/') # objects support interfaces. get the org.freedesktop.Avahi.Server interface to our org.freedesktop.Avahi object. server = dbus.Interface(raw_server, 'org.freedesktop.Avahi.Server') # The so-called documentation is at /usr/share/avahi/introspection/Server.introspect print server print server.GetVersionString() print server.GetHostName()
import dbus bus = dbus.SystemBus() hal_manager_object = bus.get_object('org.freedesktop.Hal', '/org/freedesktop/Hal/Manager') hal_manager_interface = dbus.Interface(hal_manager_object, 'org.freedesktop.Hal.Manager') print hal_manager_interface.GetAllDevices()
[edit] References
- http://www.amk.ca/diary/2007/04/rough_notes_python_and_dbus.html
- http://dbus.freedesktop.org/doc/dbus-tutorial.html
- http://developer.pidgin.im/wiki/DbusHowto
| Previous: PyQt4 | Index | Next: pyFormex |