Writing a HAL/DBUS Python program

David Eriksson twogood at users.sourceforge.net
Wed Jan 12 05:20:06 PST 2005


[Returning to list in case someone might find it useful.]

On Wed, 2005-01-12 at 05:05 -0800, Jono Bacon wrote:
> Hi David,
> 
> > I suggest that you look at the script code for
> > hal-device-manager. It's
> > Python, HAL, D-BUS, GNOME and Glade, all you could
> > wish for! :-)
> > 
> > See the files in tools/device-manager/ in the "hal"
> > CVS module or source
> > code packaage.
> 
> I have been digging around this code in
> /usr/share/hal/device-manager/ in Ubuntu, and I am
> only having so much luck in understanding how to do
> this.
> 
> What I was ideally looking for is a simply python
> example that basically connects to HAL, and basically
> prints a line of text when a USB mass storage device
> is plugged in. This would give me the ability to see
> where to begin and learn more about the system.
> 
> IF anyone has the ability to write this (it does not
> need to be graphical or anything) that would be great.
> I am quite keen on documenting this to help others get
> going with Python+HAL programming.

Look at the __init__ method of DeviceManager.py, it has all you need to
listen for HAL events:

        self.bus = dbus.Bus(dbus.Bus.TYPE_SYSTEM)
        self.hal_service = self.bus.get_service("org.freedesktop.Hal")
        self.hal_manager = self.hal_service.get_object("/org/freedesktop/Hal/Manager",
                                         "org.freedesktop.Hal.Manager")

        # gdl_changed will be invoked when the Global Device List is changed
        # per the hal spec
        self.bus.add_signal_receiver(self.gdl_changed,
				     "DeviceAdded",
                                     "org.freedesktop.Hal.Manager",
                                     "org.freedesktop.Hal",
                                     "/org/freedesktop/Hal/Manager")
        self.bus.add_signal_receiver(self.gdl_changed,
				     "DeviceRemoved",
                                     "org.freedesktop.Hal.Manager",
                                     "org.freedesktop.Hal",
                                     "/org/freedesktop/Hal/Manager")

But instead of gdl_changed, you can have your own functuon, or two
different functions, but the gdl_changed implementation shows how you
get the UDI for the device that changed:

    def gdl_changed(self, dbus_if, dbus_member, dbus_svc, dbus_obj_path, dbus_message):
        """This method is called when a HAL device is added or removed."""

        if dbus_member=="DeviceAdded":
            [device_udi] = dbus_message.get_args_list()
            print "\nDeviceAdded, udi=%s"%(device_udi)
	    self.add_device_signal_recv (device_udi)
            self.update_device_list()
        elif dbus_member=="DeviceRemoved":
            [device_udi] = dbus_message.get_args_list()
            print "\nDeviceRemoved, udi=%s"%(device_udi)
	    self.remove_device_signal_recv (device_udi)
            self.update_device_list()

And this is how you get an object from an UDI:

                   device_udi_obj = self.hal_service.get_object(device_udi,
                                                   "org.freedesktop.Hal.Device")

Now use the HAL API to get all properties for a device as a dict:

          properties = device_dbus_obj.GetAllProperties()

The properties are the same as you see in the Advanced tab for a node in
the HAL device tree, for example properties["info.parent"] is set for
all devices but the root of the tree... etc...

-- 
Regards,
               -\- David Eriksson -/-

        SynCE - http://synce.sourceforge.net
      ScummVM - http://scummvm.sourceforge.net
     Desquirr - http://desquirr.sourceforge.net

_______________________________________________
hal mailing list
hal at lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/hal



More information about the Hal mailing list