6

In all python dbus documentations there are info on how to export objects, interfaces, signals, but there is nothing how to export interface property.

Any ideas how to do that ?

2
  • What exactly do you mean by "export interface property"? Commented Sep 20, 2010 at 1:36
  • I meant just to create a Dbus property. But recently I found that it no possible to do with python. Commented Sep 20, 2010 at 20:25

2 Answers 2

14

It's definitely possible to implement D-Bus properties in Python! D-Bus properties are just methods on a particular interface, namely org.freedesktop.DBus.Properties. The interface is defined in the D-Bus specification; you can implement it on your class just like you implement any other D-Bus interface:

# Untested, just off the top of my head

import dbus

MY_INTERFACE = 'com.example.Foo'

class Foo(dbus.service.object):
    # …

    @dbus.service.method(interface=dbus.PROPERTIES_IFACE,
                         in_signature='ss', out_signature='v')
    def Get(self, interface_name, property_name):
        return self.GetAll(interface_name)[property_name]

    @dbus.service.method(interface=dbus.PROPERTIES_IFACE,
                         in_signature='s', out_signature='a{sv}')
    def GetAll(self, interface_name):
        if interface_name == MY_INTERFACE:
            return { 'Blah': self.blah,
                     # …
                   }
        else:
            raise dbus.exceptions.DBusException(
                'com.example.UnknownInterface',
                'The Foo object does not implement the %s interface'
                    % interface_name)

    @dbus.service.method(interface=dbus.PROPERTIES_IFACE,
                         in_signature='ssv'):
    def Set(self, interface_name, property_name, new_value):
        # validate the property name and value, update internal state…
        self.PropertiesChanged(interface_name,
            { property_name: new_value }, [])

    @dbus.service.signal(interface=dbus.PROPERTIES_IFACE,
                         signature='sa{sv}as')
    def PropertiesChanged(self, interface_name, changed_properties,
                          invalidated_properties):
        pass

dbus-python ought to make it easier to implement properties, but it currently is very lightly maintained at best.

If someone fancied diving in and helping fix up stuff like this, they'd be most welcome. Even adding an expanded version of this boilerplate to the documentation would be a start, as this is quite a frequently asked question. If you're interested, patches could be sent to the D-Bus mailing list, or attached to bugs filed against dbus-python on the FreeDesktop bugtracker.

Sign up to request clarification or add additional context in comments.

1 Comment

I contributed my code for adding properties to bug 26903.
2

this example does not work I think because :

''' The available properties and whether they are writable can be determined by calling org.freedesktop.DBus.Introspectable.Introspect, see the section called “org.freedesktop.DBus.Introspectable”. '''

and in introspection data the property are missing:

I use dbus-python-1.1.1

1 Comment

Implementing introspection is optional and is mostly for the benefit of interactive debugging: an application using your API should already know the API it expects. So the properties not being included in the introspection data is not ideal, but shouldn't be a major problem. (You could probably manually extend the implementation of the Introspect interface to include the properties.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.