Dbus-python how to get response with native types?

I play with dbus-python and I need to get reponses (fe dbus.Dictionary - but usually any answer) as a native Python type. Yes, you can write a recursive converter, but I think I must have missed something obvious? There should be a way to convert these monsters back to native Python types using dbus. Or is it not?

dbus.Dictionary({dbus.String(u'CanGoNext'): dbus.Boolean(True, variant_level=1), dbus.String(u'CanPause'): dbus.String(u'MinimumRate'): dbus.Int32(14, variant_level=1) ...
+5
source share
3 answers

You do not need. They are subclasses of native types, so you just use them as if they were native types:

>>> import dbus
>>> v = dbus.Dictionary({dbus.String(u'CanGoNext'): dbus.Boolean(True, variant_level=1), dbus.String(u'CanPause'): dbus.Boolean(False, variant_level=1), dbus.String(u'MinimumRate'): dbus.Int32(14, variant_level=1)})
>>> print v['MinimumRate']
14
>>> if v['CanGoNext']: print 'Go'
... 
Go
>>> if v['CanPause']: print 'Pause'
... 
>>> 
+1
source

You do not need to do anything in your code to use them, as @Martin Vidner said.

, " ", json:

import json
print(json.dumps(d, indent=2))

{
  "MinimumRate": 14, 
  "CanGoNext": 1, 
  "CanPause": 0
}

.

+1

While Martin is right, he does not work with booleans, which are passed to int using python (since bool cannot be a subclass ).

In [3]: print(dbus.Boolean(False))
0

Unfortunately, a recursive converter is the only way to preserve booleans.

Here is an example implementation:

import dbus


def python_to_dbus(data):
    '''
        convert python data types to dbus data types
    '''
    if isinstance(data, str):
        data = dbus.String(data)
    elif isinstance(data, bool):
        # python bools are also ints, order is important !
        data = dbus.Boolean(data)
    elif isinstance(data, int):
        data = dbus.Int64(data)
    elif isinstance(data, float):
        data = dbus.Double(data)
    elif isinstance(data, list):
        data = dbus.Array([python_to_dbus(value) for value in data], signature='v')
    elif isinstance(data, dict):
        data = dbus.Dictionary(data, signature='sv')
        for key in data.keys():
            data[key] = python_to_dbus(data[key])
    return data


def dbus_to_python(data):
    '''
        convert dbus data types to python native data types
    '''
    if isinstance(data, dbus.String):
        data = str(data)
    elif isinstance(data, dbus.Boolean):
        data = bool(data)
    elif isinstance(data, dbus.Int64):
        data = int(data)
    elif isinstance(data, dbus.Double):
        data = float(data)
    elif isinstance(data, dbus.Array):
        data = [dbus_to_python(value) for value in data]
    elif isinstance(data, dbus.Dictionary):
        new_data = dict()
        for key in data.keys():
            new_data[key] = dbus_to_python(data[key])
        data = new_data
    return data

To make it easier to use in a class that processes data generating dbus, you can use a decorator:

def convert_to_python(func):
    def wrapper(*args, **kwargs):
        return dbus_to_python(func(*args, **kwargs))
    return wrapper
...

@convert_to_python
def dbus_method_call(self):
    return self.dbus_proxy.Method()

Thus, any data returned by dbus_method_call above will be converted to its own python.

See this related question: dbus Option: How to save a boolean data type in Python?

0
source

All Articles