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

Commit

Permalink
Rework label serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kalkin committed Sep 27, 2016
1 parent 7138049 commit 56ced94
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 47 deletions.
51 changes: 16 additions & 35 deletions qubesdbus/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,20 @@
from gi.repository import GLib
from systemd.journal import JournalHandler

from qubesdbus.service import _DbusServiceObject
try:
# Check for mypy dependencies pylint: disable=ungrouped-imports
from typing import Any # pylint: disable=unused-import
except ImportError:
pass
import qubesdbus.serialize
from qubesdbus.service import DbusServiceObject, PropertiesObject

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


class Labels(_DbusServiceObject):
def __init__(self, app):
class Labels(DbusServiceObject):
def __init__(self, labels_data):
super(Labels, self).__init__()
self.labels = dbus.Array()
for label in app.labels.values():
proxy = Label(label, self.bus, self.bus_name, self.bus_path)
for data in labels_data:
proxy = Label(self.bus, self.bus_name, self.bus_path, data)
self.labels.append(proxy)

@dbus.service.method(dbus_interface="org.freedesktop.DBus.ObjectManager")
Expand All @@ -56,26 +52,13 @@ def GetManagedObjects(self):
"%s.labels.%s" % (self.bus_name, l.properties['name'])
for l in self.labels}

class Label(dbus.service.Object):
def __init__(self, label, bus, bus_name, path_prefix):
self.bus_path = '/'.join([path_prefix, 'labels', label.name])
self.bus_name = bus_name
self.bus = bus
self.properties = {} # type: Dict[str,Any]
self.identifier = label.name
for p_name in dir(label):
if p_name.startswith('_') or callable(getattr(label, p_name)):
continue
try:
value = getattr(label, p_name)
self.properties[p_name] = dbus.String(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")
def Get(self, _, property_name):
return self.properties[property_name]
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)

@dbus.service.method(dbus_interface="org.freedesktop.DBus.Properties")
def GetAll(self, _):
Expand All @@ -84,17 +67,15 @@ def GetAll(self, _):
return dbus.Dictionary({k: dbus.String(v)
for k, v in self.properties.items()})

@dbus.service.method(dbus_interface="org.freedesktop.DBus.Properties")
def Set(self, _, property_name, value):
log.info('%s: Property changed %s = %s', self.identifier,
property_name, value)
self.properties[property_name] = value

def main(args=None):
''' Main function ''' # pylint: disable=unused-argument
loop = GLib.MainLoop()
app = qubes.Qubes()
_ = Labels(app)

labels_data = [qubesdbus.serialize.label(label)
for label in app.labels.values()]
_ = Labels(labels_data)
print("Service running...")
loop.run()
print("Service stopped")
Expand Down
16 changes: 15 additions & 1 deletion qubesdbus/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# 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
from qubes import Qubes, Label
except ImportError:
pass

Expand Down Expand Up @@ -128,3 +128,17 @@ def domain(vm):
except AttributeError:
result[key] = dbus.String('')
return result


def label(lab):
# type: (Label) -> Dict[dbus.String, Any]
result = {}
for name in dir(lab):
if name.startswith('_') or callable(getattr(lab, name)):
continue
try:
value = getattr(lab, name)
result[name] = dbus.String(value)
except AttributeError:
result[name] = ''
return result
11 changes: 0 additions & 11 deletions qubesdbus/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,6 @@ def __init__(self, bus=None, bus_name=None, bus_path=None):
dbus.service.Object.__init__(self, self.bus_name, self.bus_path)


class _DbusServiceObject(dbus.service.Object):
def __init__(self):
self.bus_path = ''.join([PATH_PREFIX, '/', self.__class__.__name__,
str(VERSION)])
bus_name = ''.join([NAME_PREFIX, '.', self.__class__.__name__,
str(VERSION)])
self.bus = dbus.SessionBus()
self.bus_name = dbus.service.BusName(bus_name, self.bus)
dbus.service.Object.__init__(self, self.bus_name, self.bus_path)


class PropertiesObject(DbusServiceObject):
# pylint: disable=invalid-name

Expand Down

0 comments on commit 56ced94

Please sign in to comment.