pydbus:如何发布对象?

时间:2016-06-18 14:37:27

标签: python python-2.7 dbus

我想使用 pydbus python 2.7 在会话总线上发布python对象。我是pydbus的新手,所以我坚持这个例子并完成了教程。但是,我没有设法用pydbus启动一个简单的测试服务器。

我写了一个简单的类,只有一个方法,它返回一个字符串。我指定了一个接口并创建了一个事件循环。

from pydbus import SessionBus
from gi.repository import GObject

loop = GObject.MainLoop()

class Testclass(object):
    """
    <node>
        <interface name='org.philipp.DBUSTutorial'>
            <method name='helloworld'>
                <arg type='s' name='reply' direction='out'/>
            </method>
        </interface>
    </node>
    """

    def helloworld():
        return "Hello World"

bus = SessionBus()
bus.publish("org.philipp.DBUSTutorial", Testclass())
loop.run()

执行脚本后会抛出错误。

错误消息:

Traceback (most recent call last):
  File "test.py", line 24, in <module>
    bus.publish("org.philipp.DBUSTutorial", Testclass())
  File "/usr/local/lib/python2.7/dist-packages/pydbus/publication.py", line 33, in publish
    return Publication(self, bus_name, *objects)
  File "/usr/local/lib/python2.7/dist-packages/pydbus/publication.py", line 26, in __init__
    self._at_exit(bus.register_object(path, object, node_info).__exit__)
  File "/usr/local/lib/python2.7/dist-packages/pydbus/registration.py", line 123, in register_object
    return ObjectRegistration(self.con, path, interfaces, wrapper, own_wrapper=True)
  File "/usr/local/lib/python2.7/dist-packages/pydbus/registration.py", line 103, in __init__
    ids = [con.register_object(path, interface, wrapper.call_method, wrapper.get_property, wrapper.set_property) for interface in interfaces]
TypeError: argument vtable: Expected Gio.DBusInterfaceVTable, but got pydbus.registration.instancemethod

我做错了什么?如果有人能帮助我找到我的错误,那就太好了。

2 个答案:

答案 0 :(得分:3)

如自述文件所述: “从0.5开始,它支持在总线上发布对象 - 但这需要GLib 2.46或更新版本。”

不幸的是,没有办法发布旧版GLib的对象。

答案 1 :(得分:0)

注册dbus对象:

import dbus
bus = dbus.SessionBus()

class Foo(dbus.service.Object):
"""
"""
    def __init__(self, path):
        super(Foo, self).__init__(bus, path)

    # A method
    @dbus.service.method(<dbus_interface_name>,
                         in_signature="", out_signature="")
    def foo(self):
        pass