dbus-python how to return array of dictionaries

1.1k views Asked by At

I am on Ubuntu 16.04 and using Python Dbus. I want to return a list of dictionaries over DBus to my client but seem to only be able to return an array of strings. If I changed my dbus signature decorator to 'as{v}', I get an exception: "ValueError: Corrupt type signature". How can I return a list of dictionaries over 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

EDIT: This also does NOT work:

   @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
1

There are 1 answers

2
PhilBot On BEST ANSWER

I figured it out, answer is here:

   @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