dbus-python 如何 return 字典数组
dbus-python how to return array of dictionaries
我在 Ubuntu 16.04 上使用 Python Dbus。我想 return 通过 DBus 向我的客户提供字典列表,但似乎只能 return 一个字符串数组。如果我将我的 dbus 签名装饰器更改为 'as{v}',我会得到一个异常:"ValueError: Corrupt type signature"。我如何 return DBus 上的字典列表?
@dbus.service.method("com.example.service.BtScanList", in_signature='', out_signature='as')
def getScanList(self):
btMsg("Starting BT Scan List...")
# Populate device lists ( returns dictionary --> { 'mac_address' : xxx , 'name' : xxx }
self.discoveredDevs = self.getScannedDevices()
returnList = []
for dev in self.discoveredDevs:
returnList.append(dev["name"])
return returnList
编辑:这也不起作用:
@dbus.service.method("com.example.service.BtScanList", in_signature='', out_signature='a{sv}')
def getScanList(self):
btMsg("Starting BT Scan List...")
# Populate device lists ( returns dictionary --> { 'mac_address' : xxx , 'name' : xxx }
self.discoveredDevs = self.getScannedDevices()
returnList = dbus.Array()
for dev in self.discoveredDevs:
btMsg(dev)
returnList.append(dbus.Dictionary(dev, signature='sv'))
return returnList
我明白了,答案在这里:
@dbus.service.method("com.example.service.BtPairedList", in_signature='', out_signature='aa{ss}')
def getPairedList(self):
btMsg("Starting BT Paired List...")
# Populate device lists ( returns dictionary --> { 'mac_address' : xxx , 'name' : xxx }
self.pairedDevs = self.getPairedDevices()
returnList = dbus.Array()
for dev in self.pairedDevs:
btMsg(dev)
returnList.append(dbus.Dictionary(dev, signature='sv'))
return returnList
我在 Ubuntu 16.04 上使用 Python Dbus。我想 return 通过 DBus 向我的客户提供字典列表,但似乎只能 return 一个字符串数组。如果我将我的 dbus 签名装饰器更改为 'as{v}',我会得到一个异常:"ValueError: Corrupt type signature"。我如何 return DBus 上的字典列表?
@dbus.service.method("com.example.service.BtScanList", in_signature='', out_signature='as')
def getScanList(self):
btMsg("Starting BT Scan List...")
# Populate device lists ( returns dictionary --> { 'mac_address' : xxx , 'name' : xxx }
self.discoveredDevs = self.getScannedDevices()
returnList = []
for dev in self.discoveredDevs:
returnList.append(dev["name"])
return returnList
编辑:这也不起作用:
@dbus.service.method("com.example.service.BtScanList", in_signature='', out_signature='a{sv}')
def getScanList(self):
btMsg("Starting BT Scan List...")
# Populate device lists ( returns dictionary --> { 'mac_address' : xxx , 'name' : xxx }
self.discoveredDevs = self.getScannedDevices()
returnList = dbus.Array()
for dev in self.discoveredDevs:
btMsg(dev)
returnList.append(dbus.Dictionary(dev, signature='sv'))
return returnList
我明白了,答案在这里:
@dbus.service.method("com.example.service.BtPairedList", in_signature='', out_signature='aa{ss}')
def getPairedList(self):
btMsg("Starting BT Paired List...")
# Populate device lists ( returns dictionary --> { 'mac_address' : xxx , 'name' : xxx }
self.pairedDevs = self.getPairedDevices()
returnList = dbus.Array()
for dev in self.pairedDevs:
btMsg(dev)
returnList.append(dbus.Dictionary(dev, signature='sv'))
return returnList