# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

"""
Code for generating classes suitable for invoking dbus-python methods.
"""

from ._errors import DPClientGenerationError
from ._errors import DPClientRuntimeError


def gmo_reader_builder(spec):
    """
    Returns a function that builds a method interface based on 'spec'.
    This method interface is a simple one to return the values of
    properties from a table generated by a GetManagedObjects() method call
    for the object that implements the given interface.

    :param spec: the interface specification
    :type spec: Element
    """

    interface_name = spec.attrib.get('name')
    if interface_name is None: # pragma: no cover
        raise DPClientGenerationError("No name found for interface.")

    def builder(namespace):
        """
        The property class's namespace.

        :param namespace: the class's namespace
        """

        def build_property(name):
            """
            Build a single property getter for this class.

            :param str name: the property name

            :returns: the value of the property
            :rtype: object
            """

            def dbus_func(self): # pragma: no cover
                """
                The property getter.
                """
                # pylint: disable=protected-access
                try:
                    return self._table[interface_name][name]
                except KeyError:
                    raise DPClientRuntimeError(
                       "No entry found for interface %s and property %s" %
                       (interface_name, name)
                    )

            return dbus_func

        for prop in spec.findall('./property'):
            name = prop.attrib.get('name')
            namespace[name] = build_property(name)

        def __init__(self, table):
            """
            The initalizer for this class.
            """
            self._table = table # pylint: disable=protected-access

        namespace['__init__'] = __init__

    return builder

Add a method for generating GetManagedObjects() data wrappers
Add a method for generating GetManagedObjects() data wrappers

Signed-off-by: mulhern <7b51bcf507bcd7afb72bf8663752c0ddbeb517f6@redhat.com>
