Dbus option: how to save boolean data type in Python?

I have been experimenting with dbus recently. But I can't get my dbus service to guess the correct data types for boolean values. Consider the following example:

import gtk
import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop

class Service(dbus.service.Object):

  def __init__(self):
    bus_name = dbus.service.BusName("org.foo.bar", bus = dbus.SessionBus())
    dbus.service.Object.__init__(self, bus_name, "/org/foo/bar")


  @dbus.service.method("org.foo.bar", in_signature = "a{sa{sv}}",
    out_signature = "a{sa{sv}}")
  def perform(self, data):   
    return data


if __name__ == "__main__":
  DBusGMainLoop(set_as_default = True)
  s = Service()
  gtk.main()

This piece of code creates the dbus service, which provides an execution method that takes one parameter, which is a dictionary that maps from strings to other dictionaries, which in turn convert strings to variants. I chose this format because of the format in which my dictionaries are located:

{
  "key1": {
    "type": ("tuple", "value")
  },
  "key2": {
    "name": "John Doe",
    "gender": "male",
    "age": 23
  },
  "test": {
    "true-property": True,
    "false-property": False
  }
}

When I pass this dictionary through my service, the boolean values ​​are converted to integers. In my opinion, verification should not be so complicated. Consider this ( value- a variable that must be converted to a dbus type):

if isinstance(value, bool):
  return dbus.Boolean(value)

isinstance(value, int), . ?

+2
1

, . , dbus.Boolean(val). isinstance(value, dbus.Boolean), , dbus , .

Python dbus, DBus , . , , / DBus, dbus.*.

def perform(self, data):
    for key in ['true-property', 'false-property']:
        val = data['test'][key]
        newval = bool(val)

        print '%s type: %s' % (key, type(val))
        print 'is dbus.Boolean: %s' % isinstance(val, dbus.Boolean)
        print 'Python:', newval
        print '  Dbus:', dbus.Boolean(newval)
    return data

:

true-property type: <type 'dbus.Boolean'>
is dbus.Boolean: True
Python: True
  Dbus: 1
false-property type: <type 'dbus.Boolean'>
is dbus.Boolean: True
Python: False
  Dbus: 0
0

All Articles