2010-09-18 55 views
4

在所有python dbus文档中都有关于如何导出对象,接口,信号的信息,但没有任何如何导出接口属性。Python Dbus:如何导出接口属性

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

+0

究竟你 “导出接口财产” 是什么意思? – detly 2010-09-20 01:36:41

+0

我的意思是创建一个Dbus属性。但最近我发现它不可能用python做。 – deimus 2010-09-20 20:25:56

回答

11

绝对可以在Python中实现D-Bus属性! D-Bus属性只是特定接口上的方法,即org.freedesktop.DBus.Properties。界面定义为in the D-Bus specification;你可以实现它在你的类,就像你实现任何其他d-bus接口:

# 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的错误。

+3

我为我的代码添加了属性[bug 26903](https://bugs.freedesktop.org/show_bug.cgi?id=26903)。 – Teddy 2011-02-17 18:50:03

2

这个例子不工作,我想是因为:

“”” 可用属性以及它们是否是可写的可以通过调用org.freedesktop.DBus.Introspectable.Introspect来确定,见一节“组织.freedesktop.DBus.Introspectable”。 ''”

和内省数据属性缺失:

我使用的dbus-python的-1.1.1

+0

实现内省是可选的,主要是为了交互式调试:使用API​​的应用程序应该已经知道它期望的API。所以没有包含在内省数据中的属性并不理想,但不应该是一个主要问题。 (您可以手动扩展Introspect接口的实现以包含属性。) – wjt 2013-02-14 17:40:32