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

Commit

Permalink
Refactor GetManagedObjects to ObjectManager class
Browse files Browse the repository at this point in the history
  • Loading branch information
kalkin committed Sep 28, 2016
1 parent 4d503eb commit 63f2eb6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
2 changes: 1 addition & 1 deletion qubesdbus/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def __init__(self, bus, bus_name, bus_path, data):
self.properties = data
bus_path = '/'.join([bus_path, 'domains', str(data['qid'])])
name = data['name']
super(Domain, self).__init__(name, data, bus=bus, bus_name=bus_name,
super(Domain, self).__init__(name, 'org.qubes.Domain1', data, bus=bus, bus_name=bus_name,
bus_path=bus_path)

@dbus.service.signal(
Expand Down
27 changes: 9 additions & 18 deletions qubesdbus/domain_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import qubesdbus.serialize
from qubesdbus.domain import Domain
from qubesdbus.service import PropertiesObject
from qubesdbus.service import ObjectManager, PropertiesObject

try:
# Check mypy types. pylint: disable=ungrouped-imports, unused-import
Expand All @@ -46,7 +46,7 @@
log.propagate = True


class DomainManager(PropertiesObject):
class DomainManager(PropertiesObject, ObjectManager):
''' The `DomainManager` is the equivalent to the `qubes.Qubes` object for
managing domains. Implements:
* `org.freedesktop.DBus.ObjectManager` interface for acquiring all the
Expand All @@ -57,18 +57,9 @@ class DomainManager(PropertiesObject):

def __init__(self, data, domains):
# type: (Dict[dbus.String, Any], List[Dict[Union[str,dbus.String], Any]]) -> None
super(DomainManager, self).__init__('DomainManager1', data)
self.domains = [self._proxify_domain(vm) for vm in domains]

@dbus.service.method(dbus_interface="org.freedesktop.DBus.ObjectManager")
def GetManagedObjects(self):
''' Returns the domain objects paths and their supported interfaces and
properties.
'''
return {"%s/domains/%s" % (self.bus_path, d.properties['qid']):
# pylint: disable=protected-access
"%s.domains.%s" % (self.bus_name._name, d.properties['qid'])
for d in self.domains}
super(DomainManager, self).__init__('DomainManager1',
'org.qubes.DomainManager1', data)
self.managed_objects = [self._proxify_domain(vm) for vm in domains]

@dbus.service.method(dbus_interface='org.qubes.DomainManager1',
in_signature='a{sv}b')
Expand All @@ -82,9 +73,9 @@ def AddDomain(self, vm, execute=False):
log.error('Creating domains via DBus is not implemented yet')
return False
else:
vm['qid'] = len(self.domains)
vm['qid'] = len(self.managed_objects)
domain = self._proxify_domain(vm)
self.domains.append(domain)
self.managed_objects.append(domain)
log.info('Added domain %s', vm['name'])
# pylint: disable=protected-access
self.DomainAdded("org.qubes.DomainManager1", domain._object_path)
Expand All @@ -111,11 +102,11 @@ def RemoveDomain(self, vm_dbus_path, execute=False):
if execute:
log.error('Creating domains via DBus is not implemented yet')
return False
for vm in self.domains:
for vm in self.managed_objects:
# pylint: disable=protected-access
if vm._object_path == vm_dbus_path:
vm.remove_from_connection()
self.domains.remove(vm)
self.managed_objects.remove(vm)
self.DomainRemoved("org.qubes.DomainManager1", vm._object_path)
return True
return False
Expand Down
24 changes: 8 additions & 16 deletions qubesdbus/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,30 @@
import logging
import sys

import dbus.service
import qubes
from gi.repository import GLib
from systemd.journal import JournalHandler

import qubesdbus.serialize
from qubesdbus.service import DbusServiceObject, PropertiesObject
from qubesdbus.service import (DbusServiceObject, ObjectManager,
PropertiesObject)

log = logging.getLogger('org.qubes.Labels1')
log.addHandler(JournalHandler(SYSLOG_IDENTIFIER='qubesdbus.labels'))
log.setLevel(logging.INFO)


class Labels(DbusServiceObject):
class Labels(DbusServiceObject, ObjectManager):
''' A `org.freedesktop.DBus.ObjectManager` interface implementation, for
acquiring all the labels.
'''

def __init__(self, labels_data):
super(Labels, self).__init__()
self.labels = dbus.Array()
for data in labels_data:
proxy = Label(self.bus, self.bus_name, self.bus_path, data)
self.labels.append(proxy)
self.managed_objects = [self._new_label(d) for d in labels_data]

@dbus.service.method(dbus_interface="org.freedesktop.DBus.ObjectManager")
def GetManagedObjects(self):
''' Returns all the labels.
'''
return {"%s/labels/%s" % (self.bus_path, l.properties['name']):
"%s.labels.%s" % (self.bus_name, l.properties['name'])
for l in self.labels}
def _new_label(self, label_data):
return Label(self.bus, self.bus_name, self.bus_path, label_data)


class Label(PropertiesObject):
Expand All @@ -66,8 +58,8 @@ class Label(PropertiesObject):
def __init__(self, bus, bus_name, bus_path, data):
bus_path = '/'.join([bus_path, 'labels', data['name']])
name = data['name']
super(Label, self).__init__(name, data, bus=bus, bus_name=bus_name,
bus_path=bus_path)
super(Label, self).__init__(name, 'org.qubes.Label1', data, bus=bus,
bus_name=bus_name, bus_path=bus_path)


def main(args=None):
Expand Down
33 changes: 31 additions & 2 deletions qubesdbus/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

# pylint: disable=missing-docstring,invalid-name
# pylint: disable=invalid-name

''' Service classes '''

from __future__ import absolute_import

Expand All @@ -42,6 +44,25 @@
pass


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)
self.managed_objects = [] # type: PropertiesObject

@dbus.service.method(dbus_interface="org.freedesktop.DBus.ObjectManager",
out_signature="a{oa{sa{sv}}}")
def GetManagedObjects(self):
''' Returns the domain objects paths and their supported interfaces and
properties.
''' # pylint: disable=protected-access
return {o._object_path: o.properties_iface()
for o in self.managed_objects}


class DbusServiceObject(dbus.service.Object):
def __init__(self, bus=None, bus_name=None, bus_path=None):
# type: (SessionBus, BusName, str) -> None
Expand Down Expand Up @@ -69,9 +90,10 @@ def __init__(self, bus=None, bus_name=None, bus_path=None):
class PropertiesObject(DbusServiceObject):
# pylint: disable=invalid-name

def __init__(self, name, data, *args, **kwargs):
def __init__(self, name, iface, data, *args, **kwargs):
self.properties = data
self.id = name
self.iface = iface
self.log = logging.getLogger(name)
self.log.addHandler(
JournalHandler(level=logging.DEBUG, SYSLOG_IDENTIFIER='qubesdbus.'
Expand Down Expand Up @@ -106,6 +128,13 @@ def Set(self, _, name, value): # type: (str, dbus.String, Any) -> None
@dbus.service.signal(dbus_interface='org.freedesktop.DBus.Properties',
signature="sa{sv}as")
def PropertiesChanged(self, _, changed_properties, __=None):
''' This signal is emitted when a property changes '''
# type: (str, Dict[dbus.String, Any], List[dbus.String]) -> None
for name, value in changed_properties.items():
self.log.debug('%s: Property %s changed %s', self.id, name, value)

def properties_iface(self):
''' A helper for wrapping the interface around properties. Used by
`ObjectManager.GetManagedObjects`
'''
return {self.iface: self.properties}

0 comments on commit 63f2eb6

Please sign in to comment.