使用pythons Gio-Bindings在DBus上注册对象

时间:2015-01-19 17:26:52

标签: python dbus gio

我正在研究现有C-Project的Python-Clone。 C-Project连接到自定义DBus并在那里提供一个用于获取回调的对象。

我尝试使用Python复制这个代码,该代码基本上归结为:

def vtable_method_call_cb(connection, sender, object_path, interface_name, method_name, parameters, invocation, user_data):
    print('vtable_method_call_cb: %s' % method_name)

connection = Gio.DBusConnection.new_for_address_sync(
    "unix:abstract=gstswitch",
    Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT,
    None,
    None)

node_info = Gio.DBusNodeInfo.new_for_xml(introspection_xml)

vtable = Gio.DBusInterfaceVTable()
vtable.method_call(vtable_method_call_cb)
vtable.get_property(None)
vtable.set_property(None)

connection.register_object(
    "/info/duzy/gst/switch/SwitchUI",
    node_info.interfaces[0],
    vtable,
    None,
    None)

vtable.method_call调用时创建vtable时代码失败(但get_property也失败了,当我对一个调用发表评论时)以下日志/回溯:

** (process:18062): WARNING **: Field method_call: Interface type 2 should have is_pointer set
Traceback (most recent call last):
  File "moo.py", line 69, in <module>
    vtable.method_call(vtable_method_call_cb)
RuntimeError: unable to get the value

我无法在python中使用register_object()找到代码,因此我不确定Gio的这一部分是否可用或者是否完整。

1 个答案:

答案 0 :(得分:3)

这肯定不是你想听到的,但你在GDBus Python绑定中遇到4 year old bug,这使得无法在总线上注册对象。很久以前就提出了一个补丁,但每次看起来它实际上都要降落时,一些GNOME开发人员发现了他/她不喜欢它的东西,提出了一个新的补丁......并没有发生任何事情明年的大部分时间。这个周期已经发生了3次,我不知道是否有很多希望它会很快被打破......

基本上GNOME开发人员本身或多或少suggested that people use dbus-python until this issue is finally fixed,所以我猜你应该指向here instead。 : - /

顺便说一句:我认为你的源代码是错误的(除了它不会以任何方式工作的事实)。为了创建VTable,你实际上会写这样的东西,我想:

vtable = Gio.DBusInterfaceVTable()
vtable.method_call  = vtable_method_call_cb
vtable.get_property = None
vtable.set_property = None

但是由于绑定被破坏,你只是在这里用abort()交换例外......: - (

如果补丁实际上以当前形式进入python-gi,则vtable将完全转储(YEAH!)并且connection.register_object调用将变为:

connection.register_object_with_closures(
    "/info/duzy/gst/switch/SwitchUI",
    node_info.interfaces[0],
    vtable_method_call_cb, # vtable.method_call
    None,                  # vtable.get_property
    None)                  # vtable.set_property

更新

看来现在终于修复了!您现在可以使用g_dbus_connection_register_object_with_closures导出对象:

def vtable_method_call_cb(connection, sender, object_path, interface_name, method_name, parameters, invocation, user_data):
    print('vtable_method_call_cb: %s' % method_name)

connection = Gio.DBusConnection.new_for_address_sync(
    "unix:abstract=gstswitch",
    Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT,
    None,
    None)

node_info = Gio.DBusNodeInfo.new_for_xml(introspection_xml)

connection.register_object(
    "/info/duzy/gst/switch/SwitchUI",
    node_info.interfaces[0],
    vtable_method_call_cb,
    None,
    None)