すべての python dbus ドキュメントには、オブジェクト、インターフェイス、シグナルをエクスポートする方法に関する情報がありますが、インターフェイス プロパティをエクスポートする方法はありません。
それを行う方法はありますか?
PythonでD-Busプロパティを実装することは間違いなく可能です!D-Busプロパティは、特定のインターフェイス、つまり。の単なるメソッドorg.freedesktop.DBus.Properties
です。インターフェイスはD-Bus仕様で定義されています。他の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メーリングリストに送信するか、FreeDesktopバグトラッカーでdbus-pythonに対して提出されたバグに添付することができます。
この例は機能しません。理由は次のとおりです。
''' 使用可能なプロパティとそれらが書き込み可能かどうかは、org.freedesktop.DBus.Introspectable.Introspect を呼び出すことで判断できます。「org.freedesktop.DBus.Introspectable」というセクションを参照してください。'''
イントロスペクション データでは、プロパティが欠落しています。
私はdbus-python-1.1.1を使用しています