pydbus:如何发布对象?
pydbus: How to publish an object?
我想在会话总线上发布一个 python 对象,使用 pydbus 和 python 2.7 .我是 pydbus 的新手,所以我坚持使用示例并完成了教程。但是,我没有设法用 pydbus 启动一个简单的测试服务器。
我写了一个简单的class,只有一个方法,其中returns一个字符串。我指定了一个接口并创建了一个事件循环。
代码:
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
我做错了什么?如果有人能帮我找出我的错误,那就太好了。
要注册一个 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
如自述文件中所述:
"Since 0.5, it supports publishing objects on the bus - however this requires GLib 2.46 or newer."
遗憾的是,无法使用较旧的 GLib 发布对象。
我想在会话总线上发布一个 python 对象,使用 pydbus 和 python 2.7 .我是 pydbus 的新手,所以我坚持使用示例并完成了教程。但是,我没有设法用 pydbus 启动一个简单的测试服务器。
我写了一个简单的class,只有一个方法,其中returns一个字符串。我指定了一个接口并创建了一个事件循环。
代码:
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
我做错了什么?如果有人能帮我找出我的错误,那就太好了。
要注册一个 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
如自述文件中所述: "Since 0.5, it supports publishing objects on the bus - however this requires GLib 2.46 or newer."
遗憾的是,无法使用较旧的 GLib 发布对象。