[Telepathy] telepathy-python -- generating method decorators

Danielle Madeley danielle.madeley at collabora.co.uk
Sat Nov 7 22:03:29 PST 2009


I had a crazy idea this afternoon.

One of the things when writing a D-Bus service in Python is that you
have to know the type signatures of the methods to pass to the
dbus.service.method decorator. This is something that's very easy to
typo, also something that's already stored in the spec.

Instead, why not generate a decorator for each signal and method in the
Telepathy spec?

For example:

    @telepathy.decorators.Telepathy.Client.Observer.ObserveChannels
    def ObserveChannels(self, account, connection, channels, dispatch_operation,
                        requests_satisfied, observer_info):

        print "Incoming channels on %s:" % (connection)
        for object, props in channels:
            print " - %s :: %s" % (props[CHANNEL + '.ChannelType'],
                                   props[CHANNEL + '.TargetID'])

Here is a hacked up version that I did to implement an Observer:

        class Decorators(object):
            methods = {
                'org.freedesktop.DBus.Properties.GetAll': [ 's', 'a{sv}' ],
                'org.freedesktop.DBus.Properties.Get': [ 'ss', 'v' ],
                'org.freedesktop.Telepathy.Client.Observer.ObserveChannels': [ 'ooa(oa{sv})oaoa{sv}', '' ],
            }
        
            def __init__(self, namespace):
                self._namespace = namespace
        
            def __getattr__(self, key):
                return Decorators('%s.%s' % (self._namespace, key))
        
            def __call__(self, func):
                iface = self._namespace.rsplit('.', 1)[0]
                in_sig, out_sig = self.methods[self._namespace]
                return dbus.service.method(dbus_interface=iface,
                                           in_signature=in_sig,
                                           out_signature=out_sig)(func)
        
            def __str__(self):
                return self._namespace
        
        decorators = Decorators('org.freedesktop')
        
Complete example attached. Thoughts?

-- 
Danielle Madeley

Collabora Ltd., Melbourne, Australia
http://www.collabora.co.uk/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: observer-test.py
Type: text/x-python
Size: 2851 bytes
Desc: not available
Url : http://lists.freedesktop.org/archives/telepathy/attachments/20091108/e8b3e109/attachment.py 


More information about the telepathy mailing list