代码之家  ›  专栏  ›  技术社区  ›  deimus

pythondbus:如何导出接口属性

  •  5
  • deimus  · 技术社区  · 14 年前

    你知道怎么做吗?

    2 回复  |  直到 9 年前
        1
  •  13
  •   ΦXocę 웃 Пepeúpa ツ    5 年前

    在Python中实现D-Bus属性绝对是可能的!D-Bus属性只是特定接口上的方法,即 org.freedesktop.DBus.Properties . 接口已定义 in the D-Bus specification

    # 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
    

    如果有人想潜进去帮忙修理这样的东西,他们是最受欢迎的。即使在文档中添加此样板文件的扩展版本也只是一个开始,因为这是一个非常常见的问题。如果您感兴趣,可以将补丁发送到 D-Bus mailing list ,或附加到Bug filed against dbus-python on the FreeDesktop bugtracker .

        2
  •  2
  •   Paolo Patruno    12 年前

    ''' 可以通过调用org.freedesktop.DBus网站.内省。内省,请参阅名为org.freedesktop.DBus网站.内省。 '''

    我使用dbus-python-1.1.1