[dbus-python] dynamically export methods
Colin Walters
walters at verbum.org
Wed May 13 06:38:11 PDT 2009
On Tue, May 12, 2009 at 6:39 PM, Olaf Lüke <olaf at uni-paderborn.de> wrote:
> Has someone an idea how to do this? Is there another approach to export
> methods with dbus-python than with decorators that i missed? It is
> pretty painful to create appropriate classes, functions and decorators
> at runtime...
I ran into this problem a while ago...*looks* here's what I came up with:
class UiProxy(dbus.service.Object):
def __init__(self, factory, bus_name, ui_iface, ui_opath):
super(UiProxy, self).__init__(dbus.service.BusName(bus_name,
bus=dbus.SessionBus()), ui_opath)
self.__winfactory = factory
# This is a disturbing hack. But it works.
def RunCommand(self, timestamp, istab, cmd, cwd):
_logger.debug("Handling RunCommand method invocation ts=%r
cmd=%r cwd=%r)", timestamp, cmd, cwd)
if istab:
curwin = self.__winfactory.remote_new_tab(cmd, cwd)
else:
raise NotImplementedError('can only create new tabs')
timestamp = long(timestamp)+1
if timestamp > 0:
_logger.debug("presenting with timestamp %r", timestamp)
curwin.present_with_time(timestamp)
else:
curwin.present()
setattr(UiProxy, 'RunCommand', dbus.service.method(ui_iface,
in_signature='ubass')(RunCommand))
The key here is calling setattr() and using the dbus.service.method
decorator as a wrapper. There may be a nicer way, but my Python
meta-fu is not strong.
More information about the dbus
mailing list