Beispiel #1
0
class OrderedExtensionsOption(ListOption):
    """A comma separated, ordered, list of components implementing `interface`.
    Can be empty.

    If `include_missing` is true (the default) all components implementing the
    interface are returned, with those specified by the option ordered first."""

    def __init__(self, section, name, interface, default=None,
                 include_missing=True, doc=''):
        ListOption.__init__(self, section, name, default, doc=doc)
        self.xtnpt = ExtensionPoint(interface)
        self.include_missing = include_missing

    def __get__(self, instance, owner):
        if instance is None:
            return self
        order = ListOption.__get__(self, instance, owner)
        components = []
        for impl in self.xtnpt.extensions(instance):
            if self.include_missing or impl.__class__.__name__ in order:
                components.append(impl)

        def compare(x, y):
            x, y = x.__class__.__name__, y.__class__.__name__
            if x not in order:
                return int(y in order)
            if y not in order:
                return -int(x in order)
            return cmp(order.index(x), order.index(y))
        components.sort(compare)
        return components
Beispiel #2
0
class ExtensionOption(Option):
    """Name of a component implementing `interface`. Raises a
    `ConfigurationError` if the component cannot be found in the list of
    active components implementing the interface."""

    def __init__(self, section, name, interface, default=None, doc='',
                 doc_domain='tracini', doc_args=None):
        Option.__init__(self, section, name, default, doc, doc_domain,
                        doc_args)
        self.xtnpt = ExtensionPoint(interface)

    def __get__(self, instance, owner):
        if instance is None:
            return self
        value = Option.__get__(self, instance, owner)
        for impl in self.xtnpt.extensions(instance):
            if impl.__class__.__name__ == value:
                return impl
        raise ConfigurationError(
            tag_("Cannot find an implementation of the %(interface)s "
                 "interface named %(implementation)s. Please check "
                 "that the Component is enabled or update the option "
                 "%(option)s in trac.ini.",
                 interface=tag.code(self.xtnpt.interface.__name__),
                 implementation=tag.code(value),
                 option=tag.code("[%s] %s" % (self.section, self.name))))
Beispiel #3
0
class ExtensionOption(Option):
    """Name of a component implementing `interface`. Raises a
    `ConfigurationError` if the component cannot be found in the list of
    active components implementing the interface."""

    def __init__(self, section, name, interface, default=None, doc='',
                 doc_domain='tracini'):
        Option.__init__(self, section, name, default, doc, doc_domain)
        self.xtnpt = ExtensionPoint(interface)

    def __get__(self, instance, owner):
        if instance is None:
            return self
        value = Option.__get__(self, instance, owner)
        for impl in self.xtnpt.extensions(instance):
            if impl.__class__.__name__ == value:
                return impl
        raise ConfigurationError(
            tag_("Cannot find an implementation of the %(interface)s "
                 "interface named %(implementation)s. Please check "
                 "that the Component is enabled or update the option "
                 "%(option)s in trac.ini.",
                 interface=tag.code(self.xtnpt.interface.__name__),
                 implementation=tag.code(value),
                 option=tag.code("[%s] %s" % (self.section, self.name))))
Beispiel #4
0
class OrderedExtensionsOption(ListOption):
    """A comma separated, ordered, list of components implementing
    `interface`. Can be empty.

    If `include_missing` is true (the default) all components implementing the
    interface are returned, with those specified by the option ordered first.
    """
    def __init__(self,
                 section,
                 name,
                 interface,
                 default=None,
                 include_missing=True,
                 doc='',
                 doc_domain='tracini',
                 doc_args=None):
        ListOption.__init__(self,
                            section,
                            name,
                            default,
                            doc=doc,
                            doc_domain=doc_domain,
                            doc_args=doc_args)
        self.xtnpt = ExtensionPoint(interface)
        self.include_missing = include_missing

    def __get__(self, instance, owner):
        if instance is None:
            return self
        order = ListOption.__get__(self, instance, owner)
        components = []
        implementing_classes = []
        for impl in self.xtnpt.extensions(instance):
            implementing_classes.append(impl.__class__.__name__)
            if self.include_missing or impl.__class__.__name__ in order:
                components.append(impl)
        not_found = sorted(set(order) - set(implementing_classes))
        if not_found:
            raise ConfigurationError(
                tag_(
                    "Cannot find implementation(s) of the %(interface)s "
                    "interface named %(implementation)s. Please check "
                    "that the Component is enabled or update the option "
                    "%(option)s in trac.ini.",
                    interface=tag.code(self.xtnpt.interface.__name__),
                    implementation=tag(
                        (', ' if idx != 0 else None, tag.code(impl))
                        for idx, impl in enumerate(not_found)),
                    option=tag.code("[%s] %s" % (self.section, self.name))))

        def key(impl):
            name = impl.__class__.__name__
            if name in order:
                return 0, order.index(name)
            else:
                return 1, components.index(impl)

        return sorted(components, key=key)
Beispiel #5
0
def resolve_ep_class(interface, component, clsnm, **kwargs):
    r"""Retrieve the class implementing an interface (by name)
    """
    ep = ExtensionPoint(interface)
    for c in ep.extensions(component):
        if c.__class__.__name__ == clsnm:
            return c
    else:
        if 'default' in kwargs:
            return kwargs['default']
        else:
            raise LookupError('No match found for class %s implementing %s' %
                              (clsnm, interface))
Beispiel #6
0
def resolve_ep_class(interface, component, clsnm, **kwargs):
    r"""Retrieve the class implementing an interface (by name)
    """
    ep = ExtensionPoint(interface)
    for c in ep.extensions(component):
        if c.__class__.__name__ == clsnm :
            return c
    else:
        if 'default' in kwargs:
            return kwargs['default']
        else:
            raise LookupError('No match found for class %s implementing %s' % 
                    (clsnm, interface) )
Beispiel #7
0
class OrderedExtensionsOption(ListOption):
    """A comma separated, ordered, list of components implementing
    `interface`. Can be empty.

    If `include_missing` is true (the default) all components implementing the
    interface are returned, with those specified by the option ordered first.
    """

    def __init__(self, section, name, interface, default=None,
                 include_missing=True, doc='', doc_domain='tracini'):
        ListOption.__init__(self, section, name, default, doc=doc,
                            doc_domain=doc_domain)
        self.xtnpt = ExtensionPoint(interface)
        self.include_missing = include_missing

    def __get__(self, instance, owner):
        if instance is None:
            return self
        order = ListOption.__get__(self, instance, owner)
        components = []
        implementing_classes = []
        for impl in self.xtnpt.extensions(instance):
            implementing_classes.append(impl.__class__.__name__)
            if self.include_missing or impl.__class__.__name__ in order:
                components.append(impl)
        not_found = sorted(set(order) - set(implementing_classes))
        if not_found:
            raise ConfigurationError(
                tag_("Cannot find implementation(s) of the %(interface)s "
                     "interface named %(implementation)s. Please check "
                     "that the Component is enabled or update the option "
                     "%(option)s in trac.ini.",
                     interface=tag.code(self.xtnpt.interface.__name__),
                     implementation=tag(
                         (', ' if idx != 0 else None, tag.code(impl))
                         for idx, impl in enumerate(not_found)),
                     option=tag.code("[%s] %s" % (self.section, self.name))))

        def compare(x, y):
            x, y = x.__class__.__name__, y.__class__.__name__
            if x not in order:
                return int(y in order)
            if y not in order:
                return -int(x in order)
            return cmp(order.index(x), order.index(y))
        components.sort(compare)
        return components
Beispiel #8
0
class ExtensionOption(Option):
    def __init__(self, section, name, interface, default=None, doc=''):
        Option.__init__(self, section, name, default, doc)
        self.xtnpt = ExtensionPoint(interface)

    def __get__(self, instance, owner):
        if instance is None:
            return self
        value = Option.__get__(self, instance, owner)
        for impl in self.xtnpt.extensions(instance):
            if impl.__class__.__name__ == value:
                return impl
        raise AttributeError(
            'Cannot find an implementation of the "%s" '
            'interface named "%s".  Please update the option '
            '%s.%s in trac.ini.' %
            (self.xtnpt.interface.__name__, value, self.section, self.name))
Beispiel #9
0
class ExtensionOption(Option):

    def __init__(self, section, name, interface, default=None, doc=''):
        Option.__init__(self, section, name, default, doc)
        self.xtnpt = ExtensionPoint(interface)

    def __get__(self, instance, owner):
        if instance is None:
            return self
        value = Option.__get__(self, instance, owner)
        for impl in self.xtnpt.extensions(instance):
            if impl.__class__.__name__ == value:
                return impl
        raise AttributeError('Cannot find an implementation of the "%s" '
                             'interface named "%s".  Please update the option '
                             '%s.%s in trac.ini.'
                             % (self.xtnpt.interface.__name__, value,
                                self.section, self.name))
Beispiel #10
0
class OrderedExtensionsOption(ListOption):
    """A comma separated, ordered, list of components implementing `interface`.
    Can be empty.

    If `include_missing` is true (the default) all components implementing the
    interface are returned, with those specified by the option ordered first."""
    def __init__(self,
                 section,
                 name,
                 interface,
                 default=None,
                 include_missing=True,
                 doc=''):
        ListOption.__init__(self, section, name, default, doc=doc)
        self.xtnpt = ExtensionPoint(interface)
        self.include_missing = include_missing

    def __get__(self, instance, owner):
        if instance is None:
            return self
        order = ListOption.__get__(self, instance, owner)
        components = []
        for impl in self.xtnpt.extensions(instance):
            if self.include_missing or impl.__class__.__name__ in order:
                components.append(impl)

        def compare(x, y):
            x, y = x.__class__.__name__, y.__class__.__name__
            if x not in order:
                return int(y in order)
            if y not in order:
                return -int(x in order)
            return cmp(order.index(x), order.index(y))

        components.sort(compare)
        return components