Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
Add Device & DeviceCollection DBus serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kalkin committed Nov 7, 2016
1 parent 9e7346e commit 88fa439
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion qubesdbus/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
''' Collection of serialization helpers '''

import re

import dbus
import qubes
import qubes.vm.qubesvm
Expand Down Expand Up @@ -72,6 +74,14 @@ def domain_data(vm):
return result


def devices_data(app):
result = []
for vm in app.domains:
for dev_class, dev_collection in vm.devices.items():
result += serialize_val(dev_collection)
return result


def label_data(lab):
''' Serialize a `qubes.Label` to a dictionary '''
# type: (Label) -> Dict[dbus.String, Any]
Expand Down Expand Up @@ -99,15 +109,30 @@ def serialize_val(value):
elif isinstance(value, int):
return dbus.Int32(value)
elif callable(value):
return serialize_val(value())
return serialize_val(value)
elif isinstance(value, qubes.Label):
return label_path(value)
elif isinstance(value, qubes.vm.qubesvm.QubesVM):
return domain_path(value)
elif isinstance(value, qubes.devices.DeviceCollection):
return dbus.Array(device_collection_data(value), signature='a{sv}')
elif isinstance(value, qubes.devices.DeviceInfo):
return dbus.Dictionary(device_collection_data(value), signature='sv')
elif isinstance(value, re._pattern_type):
return dbus.String(value.pattern)
else:
return dbus.String(value)


def device_collection_data(collection):
return [device_data(dev) for dev in collection.available()]


def device_data(dev):
return {serialize_val(prop): serialize_val(getattr(dev, prop))
for prop in dir(dev) if not prop.startswith('_')}


def label_path(label):
# type: (Label) -> dbus.ObjectPath
''' Return the D-Bus object path for a `qubes.Label` '''
Expand Down

0 comments on commit 88fa439

Please sign in to comment.