Languages/Python/PyKDE DBus Tutorial: Difference between revisions

    From KDE TechBase
    (Initialized Python tutorial for DBus)
     
    (Put in a huge section on qdbusviewer)
    Line 5: Line 5:
    ____WORK IN PROGRESS____
    ____WORK IN PROGRESS____


    First off, a small introduction to DBus. DBus is an inter-process communication framework. In other words, it allows different applications to talk to each other. DBus uses the concept of signals and methods, which are similar to the Signals and Slots in Qt. You can connect signals to methods, or call methods directly.
    First off, a small introduction to DBus. DBus is an inter-process communication framework. In other words, it allows different applications to talk to each other. DBus uses the concept of signals and methods, which are similar to the Signals and Slots in Qt. (For more information on Signals and Slots in python, see http://techbase.kde.org/Development/Tutorials/Python_introduction_to_signals_and_slots) You can connect signals to methods, or call methods directly.
     
    Everything sent or received in dbus is transferred over a bus. There are two main buses available, the Session Bus and the System Bus. The former handles per-session (per-user) information while the System Bus handles systemwide notification and settings. For example, the hal subsystem provides a number of interfaces on the System Bus, while Amarok or PowerDevil provides session-specific interfaces for control over music and power options, respectively.
     
    A DBus path is made up of three parts, the Service Name, the Object Path and the Interface. The following table (taken from the Qt intro to dbus) provides some ways to distinguish between the three parts.
     
    TODO: Put in table
    D-Bus Concept
    Analogy Name format
    Service name  Network hostnames  Dot-separated ("looks like a hostname")
      Object path  URL path component  Slash-separated ("looks like a path")
      Interface  Plugin identifier  Dot-separated
     
    One of the easiest ways to discover a DBus method is by browsing for it using qdbusviewer. Run this program from your command line or KRunner (alt+f2) prompt.
     
    You should see the following screen:
    [Insert Initial qdbusviewer screen]
     
    The two tabs available are the Session Bus and the System Bus.
     
    Within each bus, the left pane shows Service Names. If you click on a service, the right pane shows information about that service.
     
    Find the org.freedesktop.PowerManagement service and select it. You show now see the following:
    [Insert org.freedesktop.PowerManagement screen]
     
    Everything you see on the right pane is the start of an Object Path. Try expanding down the modules/powerdevil/ path. You should now see the following:
    [Insert modules/powerdevil/ path screen]
     
    org.kde.PowerDevil
    org.freedesktop.DBus.Properties
    org.freedesktop.DBus.Introspectable
     
    The above entries are Interfaces. They are the things that you can actually interact with. When you expand the org.kde.PowerDevil interface, you see a number of Methods and Signals:
    [Insert powerdevil expanded screen]
     
    Clicking on a Method will call it. Try clicking on the "turnOffScreen" method. Your screen should go blank. Note: You can get your display back by wiggling your mouse.
     
    When your display comes back, you should see some information in the bottom pane.
    [Insert turnOffScreen screen]
     
    Since the method returned nothing, qdbusviewer told us that.
     
     
    -----------------------------------------------------
     
    Now, for working with methods in python and PyKDE4.
     
     
     
     
     
    references: http://doc.trolltech.com/intro-to-dbus.html

    Revision as of 18:10, 1 April 2009

    The aim of this tutorial is to give an overview of DBus in the context of python. By the end, the goal is to understand how to find DBus methods in existing applications and how to use them in PyQt4/PyKDE4. It assumes a basic working knowledge of Python and PyKDE4. (see http://www.learningpython.com/2008/09/20/an-introduction-to-pyqt/ and http://techbase.kde.org/Development/Tutorials/Python_introduction_to_signals_and_slots)

    Later additions to this tutorial may cover creating and emitting dbus signals.

    ____WORK IN PROGRESS____

    First off, a small introduction to DBus. DBus is an inter-process communication framework. In other words, it allows different applications to talk to each other. DBus uses the concept of signals and methods, which are similar to the Signals and Slots in Qt. (For more information on Signals and Slots in python, see http://techbase.kde.org/Development/Tutorials/Python_introduction_to_signals_and_slots) You can connect signals to methods, or call methods directly.

    Everything sent or received in dbus is transferred over a bus. There are two main buses available, the Session Bus and the System Bus. The former handles per-session (per-user) information while the System Bus handles systemwide notification and settings. For example, the hal subsystem provides a number of interfaces on the System Bus, while Amarok or PowerDevil provides session-specific interfaces for control over music and power options, respectively.

    A DBus path is made up of three parts, the Service Name, the Object Path and the Interface. The following table (taken from the Qt intro to dbus) provides some ways to distinguish between the three parts.

    TODO: Put in table D-Bus Concept Analogy Name format Service name Network hostnames Dot-separated ("looks like a hostname")

     Object path  URL path component  Slash-separated ("looks like a path")
     Interface  Plugin identifier  Dot-separated
    

    One of the easiest ways to discover a DBus method is by browsing for it using qdbusviewer. Run this program from your command line or KRunner (alt+f2) prompt.

    You should see the following screen: [Insert Initial qdbusviewer screen]

    The two tabs available are the Session Bus and the System Bus.

    Within each bus, the left pane shows Service Names. If you click on a service, the right pane shows information about that service.

    Find the org.freedesktop.PowerManagement service and select it. You show now see the following: [Insert org.freedesktop.PowerManagement screen]

    Everything you see on the right pane is the start of an Object Path. Try expanding down the modules/powerdevil/ path. You should now see the following: [Insert modules/powerdevil/ path screen]

    org.kde.PowerDevil org.freedesktop.DBus.Properties org.freedesktop.DBus.Introspectable

    The above entries are Interfaces. They are the things that you can actually interact with. When you expand the org.kde.PowerDevil interface, you see a number of Methods and Signals: [Insert powerdevil expanded screen]

    Clicking on a Method will call it. Try clicking on the "turnOffScreen" method. Your screen should go blank. Note: You can get your display back by wiggling your mouse.

    When your display comes back, you should see some information in the bottom pane. [Insert turnOffScreen screen]

    Since the method returned nothing, qdbusviewer told us that.



    Now, for working with methods in python and PyKDE4.



    references: http://doc.trolltech.com/intro-to-dbus.html