#
# Copyright (C) 2016  Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.  You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): David Lehman <dlehman@redhat.com>
#

import dbus
import dbus.service

from .constants import BUS_NAME


class DBusObject(dbus.service.Object):
    """ Base class for dbus objects. """
    def __init__(self):
        super().__init__(bus_name=dbus.service.BusName(BUS_NAME, dbus.SystemBus()),
                         object_path=self.object_path)

    @property
    def object_path(self):
        """ The dbus object path for this instance. """
        raise NotImplementedError()

    @property
    def interface(self):
        """ The interface implemented by this class. """
        raise NotImplementedError()

    @property
    def properties(self):
        """ dict of property key/value pairs to export via dbus. """
        raise NotImplementedError()

    @dbus.service.method(dbus_interface=dbus.PROPERTIES_IFACE, in_signature='s', out_signature='a{sv}')
    def GetAll(self, interface_name):
        if interface_name != self.interface:
            raise dbus.exceptions.DBusException(
                'com.redhat.Blivet1.UnknownInterface',
                'The %s object does not implement the %s interface'
                % (self.__class__.__name__, interface_name))

        return self.properties

    @dbus.service.method(dbus_interface=dbus.PROPERTIES_IFACE, in_signature='ss', out_signature='v')
    def Get(self, interface_name, property_name):
        return self.GetAll(interface_name)[property_name]

@@ -1971,64 +1971,74 @@
 ion(
-%0A                'com.redhat.Blivet1.UnknownInterface',%0A
+'%25s.UnknownInterface' %25 BUS_NAME,%0A                                
     
@@ -2101,16 +2101,48 @@
 erface'%0A
+                                
         
@@ -2436,8 +2436,1240 @@
 y_name%5D%0A
+%0A    @dbus.service.method(dbus_interface=dbus.PROPERTIES_IFACE, in_signature='ssv')%0A    def Set(self, interface_name, property_name, new_value):%0A        if interface_name != self.interface:%0A            raise dbus.exceptions.DBusException('%25s.UnknownInterface' %25 BUS_NAME,%0A                                                'The %25s object does not implement the %25s interface'%0A                                                %25 (self.__class__.__name__, interface_name))%0A%0A        if property_name in self.properties:%0A            raise dbus.exceptions.DBusException('%25s.ReadOnlyProperty' %25 BUS_NAME,%0A                                                'The %25s property is read-only' %25 property_name)%0A        else:%0A            raise dbus.exceptions.DBusException('%25s.UnknownProperty' %25 BUS_NAME,%0A                                                'The %25s interface does not have %25s property'%0A                                                %25 (interface_name, property_name))%0A%0A        self.PropertiesChanged(interface_name, %7Bproperty_name: new_value%7D, %5B%5D)%0A%0A    @dbus.service.signal(dbus_interface=dbus.PROPERTIES_IFACE, signature='sa%7Bsv%7Das')%0A    def PropertiesChanged(self, interface_name, changed_properties, invalidated_properties):%0A        pass%0A

Clean up org.freedesktop.DBus.Properties code in DBusObject a bit.