#
# Copyright (c) 2013 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
#
import types

import dbus
import dbus.service
import dbus.glib

from subscription_manager.base_plugin import SubManPlugin
requires_api_version = "1.0"

# imported to support logging in subscription-manager >= 1.17.10-1 from Bug 1334916: Move logging configuration to rhsm.conf
import logging
log = logging.getLogger('rhsm-app.' + __name__)


class AllSlotsTestPlugin(SubManPlugin):
    """Plugin with hooks for all slots"""
    name = "all_slots_test"
    all_slots = True

    def __init__(self, conf=None):
        super(AllSlotsTestPlugin, self).__init__(conf)

    def __getattr__(self, attrname):
        if attrname.endswith('_hook'):
            # if we get asked for a hook method, create one based on
            # "handler", set a slot_name attribute on it, and bind it
            # to our class with correct attribute name
            def handler(self, conduit):
                  conduit.log.info("Running handler for %s from slot %s defined in %s." % (attrname, handler.slot_name, self.name))
                  log.info        ("Running handler for %s from slot %s defined in %s." % (attrname, handler.slot_name, self.name))

            # add a slot_name attr to the handler method obj itself
            setattr(handler, 'slot_name', attrname[:-5])

            # create a new instance of it, and bind it to self
            new_hook = types.MethodType(handler, self, AllSlotsTestPlugin)

            # make the accesor point to the new method
            setattr(self, attrname, new_hook)

            # might we well return what we set via now working
            # attr lookup
            return getattr(self, attrname)

        # not a hook name, so legit attr error
        raise AttributeError



