Python Dbus:如何导出接口属性 [英] Python Dbus : How to export Interface property
问题描述
在所有python dbus文档中,都有关于如何导出对象,接口,信号的信息,但是没有如何导出接口属性的信息。
In all python dbus documentations there are info on how to export objects, interfaces, signals, but there is nothing how to export interface property.
任何想法如何去做 ?
Any ideas how to do that ?
推荐答案
使用Python实现D-Bus属性绝对有可能! D-Bus属性只是特定接口上的方法,即 org.freedesktop.DBus.Properties
。该接口在D-Bus规范中 ;您可以像实现任何其他D-Bus接口一样在您的类上实现它:
It's definitely possible to implement D-Bus properties in Python! D-Bus properties are just methods on a particular interface, namely org.freedesktop.DBus.Properties
. The interface is defined in the D-Bus specification; you can implement it on your class just like you implement any other 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应该
dbus-python ought to make it easier to implement properties, but it currently is very lightly maintained at best.
如果有人幻想潜水并帮助解决此类问题,那么他们将是最多的人。欢迎。甚至在文档中添加此样板的扩展版本也是一个开始,因为这是一个非常常见的问题。如果您有兴趣,可以将补丁发送到 D-Bus邮件列表,或附加到错误 在FreeDesktop Bugtracker上针对dbus-python提交的错误。
If someone fancied diving in and helping fix up stuff like this, they'd be most welcome. Even adding an expanded version of this boilerplate to the documentation would be a start, as this is quite a frequently asked question. If you're interested, patches could be sent to the D-Bus mailing list, or attached to bugs filed against dbus-python on the FreeDesktop bugtracker.
这篇关于Python Dbus:如何导出接口属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!