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

Commit

Permalink
Fix domain serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kalkin committed Sep 27, 2016
1 parent 5c77cd3 commit 571fb66
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
2 changes: 2 additions & 0 deletions qubesdbus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

from .constants import * # pylint: disable=wildcard-import
from .proxy import QubesDbusProxy
from .domain import Domain
from .domain_manager import DomainManager
from .labels import Labels
from .constants import NAME_PREFIX, PATH_PREFIX, VERSION
from .exceptions import QubesDbusException
from . import serialize
24 changes: 2 additions & 22 deletions qubesdbus/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import logging

import dbus.service
from qubesdbus import serialize
from systemd.journal import JournalHandler

try:
Expand Down Expand Up @@ -86,34 +87,13 @@ def __init__(self, domain, bus, bus_name, path_prefix):
self.bus_path = '/'.join([path_prefix, 'domains', str(domain.qid)])
self.bus_name = bus_name
self.bus = bus
self.properties = {'state': 'halted'}
self.properties = serialize.domain(domain)
self.qid = str(domain.qid)
logger_name = 'qubesdbus.domain.' + self.qid
self.log = logging.getLogger(logger_name)
self.log.addHandler(
JournalHandler(level=logging.DEBUG, SYSLOG_IDENTIFIER=logger_name))

for p_name in DOMAIN_PROPERTIES:
try:
value = getattr(domain, p_name)
if callable(value):
value = value()
if isinstance(value, dict):
value = dbus.Dictionary(value)
elif isinstance(value, bool):
value = dbus.Boolean(value)
else:
value = dbus.String(value)
if p_name in DOMAIN_STATE_PROPERTIES:
if value:
self.properties['state'] = p_name.split('_', 1)[1]
elif p_name.startswith("is_"):
_, new_name = p_name.split("_", 1)
self.properties[new_name] = value
else:
self.properties[p_name] = value
except AttributeError:
self.properties[p_name] = ''
dbus.service.Object.__init__(self, self.bus_name, self.bus_path)

@dbus.service.method(dbus_interface="org.freedesktop.DBus.Properties")
Expand Down
41 changes: 31 additions & 10 deletions qubesdbus/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import dbus

try:
# Check for mypy dependencies pylint: disable=ungrouped-imports
from typing import Any, Callable, Tuple # pylint: disable=unused-import
# Check mypy dependencies. pylint: disable=ungrouped-imports,unused-import
from typing import Any, Callable, Tuple, List, Dict
from qubes.vm.qubesvm import QubesVM
from qubes import Qubes
except ImportError:
Expand Down Expand Up @@ -78,17 +78,16 @@
# 'volumes',
'xid', ]

_DOMAIN_STATES = ['is_halted',
'is_paused',
'is_running',
'is_qrexec_running', ]

STATE_MAP = _init_states(_DOMAIN_STATES)
DOMAIN_STATE_PROPERTIES = ['is_halted',
'is_paused',
'is_running',
'is_qrexec_running', ]


def _init_states(states):
# type: (List[str]) -> Dict[str,Callable[[str, bool], Tuple[str,dbus.Boolean]]]
result = {} # type: Dict[str,Callable[[str, bool], Tuple[str,dbus.Boolean]]]
result = {
} # type: Dict[str,Callable[[str, bool], Tuple[str,dbus.Boolean]]]

def qubes(app):
''' Serialize `qubes.Qubes` to a dictionary '''
Expand All @@ -106,4 +105,26 @@ def qubes(app):
def domain(vm):
''' Serializes a `qubes.vm.qubesvm.QubesVM` to a dictionary '''
# type: (QubesVM) -> Dict[dbus.String, Any]
pass
result = {dbus.String('state'): 'halted'}
for name in DOMAIN_PROPERTIES:
if name in DOMAIN_STATE_PROPERTIES:
key = 'state'
elif name.startswith("is_"):
_, key = name.split("_", 1)
else:
key = name

key = dbus.String(key)
try:
value = getattr(vm, name)
if isinstance(value, dict):
value = dbus.Dictionary(value)
elif isinstance(value, bool):
value = dbus.Boolean(value)
else:
value = dbus.String(value)

result[key] = value
except AttributeError:
result[key] = dbus.String('')
return result

0 comments on commit 571fb66

Please sign in to comment.