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

Commit

Permalink
Fix serialization issues
Browse files Browse the repository at this point in the history
  • Loading branch information
kalkin committed Sep 28, 2016
1 parent 07e68b3 commit 505732b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
5 changes: 2 additions & 3 deletions qubesdbus/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ def forward_vm_event(self, vm, event, *args, **kwargs):
return
elif event.startswith('property-set:') and not self.new_vm:
proxy = vm_proxy(vm.qid)
property_set(proxy, args[0],
qubesdbus.serialize.serialize_val(args[1]))
property_set(proxy, args[0], qubesdbus.serialize.serialize_val(args[1]))
log.info('VM: %s %s %s %s', vm, event, args, kwargs)
elif event == 'domain-init' and vm.storage is None:
# looks like a new vm is created
Expand All @@ -87,7 +86,7 @@ def forward_vm_event(self, vm, event, *args, **kwargs):
proxy = app_proxy()
func = proxy.get_dbus_method('AddDomain',
'org.qubes.DomainManager1')
data = qubesdbus.serialize.serialize_val(vm)
data = qubesdbus.serialize.domain_data(vm)
create = False
if not func(data, create):
log.error('Could not add vm via to dbus DomainManager')
Expand Down
32 changes: 20 additions & 12 deletions qubesdbus/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

''' Collection of serialization helpers '''

import dbus
import qubes
import qubes.vm.qubesvm
Expand Down Expand Up @@ -95,16 +97,17 @@ def qubes_data(app):
for name in QUBES_PROPERTIES:
key = dbus.String(name)
try:
result[key] = dbus.String(getattr(app, name))
result[key] = serialize_val(getattr(app, name))
except AttributeError:
result[key] = dbus.String('')

return result


def domain_data(vm):
''' Serializes a `qubes.vm.qubesvm.QubesVM` to a dictionary '''
# type: (QubesVM) -> Dict[dbus.String, Any]
result = dbus.Dictionary({'state': 'halted'}, signature='sv')
result = dbus.Dictionary({'state': 'halted'}, signature='ss')
for name in DOMAIN_PROPERTIES:
if name in DOMAIN_STATE_PROPERTIES:
key = 'state'
Expand All @@ -115,13 +118,8 @@ def domain_data(vm):

key = dbus.String(key)
try:
tmp = getattr(vm, name)
if tmp is None:
value = dbus.String('')
if isinstance(tmp, qubes.vm.qubesvm.QubesVM):
value = serialize_val(str(tmp))
else:
value = serialize_val(tmp)

value = serialize_val(getattr(vm, name))

result[key] = value
except (AttributeError, libvirtError):
Expand All @@ -130,6 +128,7 @@ def domain_data(vm):


def label_data(lab):
''' Serialize a `qubes.Label` to a dictionary '''
# type: (Label) -> Dict[dbus.String, Any]
result = {}
for name in dir(lab):
Expand All @@ -144,7 +143,10 @@ def label_data(lab):


def serialize_val(value):
''' Serialize a property value '''
# pylint: disable=too-many-return-statements
if value is None:
return dbus.String('')
if isinstance(value, dict):
return dbus.Dictionary(value, signature='sv')
elif isinstance(value, bool):
Expand All @@ -156,12 +158,18 @@ def serialize_val(value):
elif isinstance(value, qubes.Label):
return label_path(value)
elif isinstance(value, qubes.vm.qubesvm.QubesVM):
return domain_data(value)
elif isinstance(value, qubes.Qubes):
return qubes_data(value)
return domain_path(value)
else:
return dbus.String(value)


def label_path(label):
# type: (qubes.Label) -> dbus.ObjectPath
''' Return the D-Bus object path for a `qubes.Label` '''
return dbus.ObjectPath('/org/qubes/Labels1/labels/' + label.name)


def domain_path(vm):
''' Return the D-Bus object path for a `qubes.vm.qubesvm.QubesVM` '''
# type: (qubes.vm.qubesvm.QubesVM) -> dbus.ObjectPath
return dbus.ObjectPath('/org/qubes/DomainManager1/domains/' + str(vm.qid))
12 changes: 5 additions & 7 deletions qubesdbus/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# pylint: disable=invalid-name

''' Service classes '''

from __future__ import absolute_import
Expand Down Expand Up @@ -48,6 +47,7 @@ class ObjectManager(object):
''' Provides a class implementing the `org.freedesktop.DBus.ObjectManager`
interface.
'''

# pylint: disable=too-few-public-methods
def __init__(self, *args, **kwargs):
super(ObjectManager, self).__init__(*args, **kwargs)
Expand All @@ -66,6 +66,7 @@ def GetManagedObjects(self):
class DbusServiceObject(dbus.service.Object):
''' A class implementing a useful shortcut for writing own D-Bus Services
'''

def __init__(self, bus=None, bus_name=None, bus_path=None):
# type: (SessionBus, BusName, str) -> None
if bus is not None:
Expand Down Expand Up @@ -109,14 +110,11 @@ def Get(self, _, property_name):
''' Returns the property value '''
return self.properties[property_name]

@dbus.service.method(dbus_interface="org.freedesktop.DBus.Properties")
@dbus.service.method(dbus_interface="org.freedesktop.DBus.Properties",
in_signature='s', out_signature='a{sv}')
def GetAll(self, _):
''' Returns all properties and their values '''
# According to the dbus spec we should be able to return types not only
# string, but it doesn't work. We need to serialize everything to string
# ☹
return dbus.Dictionary({k: dbus.String(v)
for k, v in self.properties.items()})
return self.properties

@dbus.service.method(dbus_interface="org.freedesktop.DBus.Properties")
def Set(self, _, name, value): # type: (str, dbus.String, Any) -> None
Expand Down

0 comments on commit 505732b

Please sign in to comment.