开发者

Adding methods to a dbus object in python

I need to create a dbus object in python with method names that are decided at runtime.

The code I've tried is basically this:

import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
import gobject

DBusGMainLoop(set_as_default=True)
gobject.threads_init()

class greg(dbus.service.Object):
        def __init__(self):
                dbus.service.Object.__init__(self, bus, "/greg")

        @dbu开发者_JS百科s.service.method(
                dbus_interface="com.blah.blah",
                in_signature="",
                out_signature="")
        def dance(self):
                print "*busts a move*"

def func(self):
    pass
func = dbus.service.method(
        dbus_interface="com.blah.blah",
        in_signature="",
        out_signature="")(func)
setattr(greg, "do_nothing", func)

bus = dbus.SystemBus()
busname = dbus.service.BusName("com.blah.blah", bus)
obj = greg()
loop = gobject.MainLoop()
loop.run()

In this case the function 'dance' is available on the interface but the function 'do_nothing' is not. I don't understand why? Is there a way to do what I'm trying to achieve?


I'm guessing that the do_nothing method is available, but not visible. Have you tried to call it blindly?

What is visible is what is returned by the Introspect method, which in turn depends on the _dbus_class_table class attribute, which you therefore need to update to have Introspect return the updated list of D-Bus methods.


func() has no dbus service header, so it is not recognized. How can you set "do_nothing" to your function when the greg object contains no such attribute?

Check whether the object has the attribute to ensure that your statement will complete successfully.

print(hasattr(greg, "do_nothing"))

Also, it would be appreciated if you could pay more attention to python code style guidelines in the future: http://www.python.org/dev/peps/pep-0008/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜