gpt4 book ai didi

Python Dbus : How to export Interface property

转载 作者:太空狗 更新时间:2023-10-30 00:28:47 27 4
gpt4 key购买 nike

在所有 python dbus 文档中都有关于如何导出对象、接口(interface)、信号的信息,但没有关于如何导出接口(interface)属性的信息。

任何想法如何做到这一点?

最佳答案

绝对有可能在 Python 中实现 D-Bus 属性! D-Bus 属性只是特定接口(interface)上的方法,即 org.freedesktop.DBus.Properties .接口(interface)定义in the D-Bus specification ;您可以像实现任何其他 D-Bus 接口(interface)一样在您的类中实现它:

# Untested, just off the top of my head

import dbus

MY_INTERFACE = 'com.example.Foo'

class Foo(dbus.service.object):
# …

@dbus.service.method(interface=dbus.PROPERTIES_IFACE,
in_signature='ss', out_signature='v')
def Get(self, interface_name, property_name):
return self.GetAll(interface_name)[property_name]

@dbus.service.method(interface=dbus.PROPERTIES_IFACE,
in_signature='s', out_signature='a{sv}')
def GetAll(self, interface_name):
if interface_name == MY_INTERFACE:
return { 'Blah': self.blah,
# …
}
else:
raise dbus.exceptions.DBusException(
'com.example.UnknownInterface',
'The Foo object does not implement the %s interface'
% interface_name)

@dbus.service.method(interface=dbus.PROPERTIES_IFACE,
in_signature='ssv'):
def Set(self, interface_name, property_name, new_value):
# validate the property name and value, update internal state…
self.PropertiesChanged(interface_name,
{ property_name: new_value }, [])

@dbus.service.signal(interface=dbus.PROPERTIES_IFACE,
signature='sa{sv}as')
def PropertiesChanged(self, interface_name, changed_properties,
invalidated_properties):
pass

dbus-python 应该使实现属性变得更容易,但目前充其量是非常轻微的维护。

如果有人愿意参与其中并帮助解决此类问题,我们将非常欢迎他们。甚至将此样板文件的扩展版本添加到文档中也是一个开始,因为这是一个经常被问到的问题。如果您有兴趣,可以将补丁发送至 D-Bus mailing list ,或附加到错误 filed against dbus-python on the FreeDesktop bugtracker .

关于Python Dbus : How to export Interface property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3740903/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com