view MoinMoin/converter/ @ 1613:bc8a2546468b

Clean up registry code. Quite some boilerplate codes were removed. The signature of several `_factory` and `Entry.__call__` were changed a bit as well to make things a bit more uniform.
author Cheer Xiao <>
date Wed, 01 Aug 2012 00:21:03 +0800
parents 4ac437141bbe
children 5b5e7190452f
line wrap: on
line source
# Copyright: 2008 MoinMoin:BastianBlank
# License: GNU GPL v2 (or any later version), see LICENSE.txt for details.

MoinMoin - Converter support

Converters are used to convert between formats or between different featuresets
of one format.

There are usually three types of converters:

- Between an input format like Moin Wiki or Creole and the internal tree
- Between the internal tree and an output format like HTML.
- Between different featuresets of the internal tree representation like URI
  types or macro expansion.

TODO: Merge with new-style macros.

from collections import namedtuple

from ..util.registry import RegistryBase
from ..util.pysupport import load_package_modules

class RegistryConverter(RegistryBase):
    class Entry(namedtuple('Entry', 'factory type_input type_output priority')):
        def __call__(self, type_input, type_output, **kw):
            if (self.type_output.issupertype(type_output) and
                return self.factory(type_input, type_output, **kw)

        def __lt__(self, other):
            if isinstance(other, self.__class__):
                if self.priority < other.priority:
                    return True
                if self.type_output != other.type_output:
                    return other.type_output.issupertype(self.type_output)
                if self.type_input != other.type_input:
                    return other.type_input.issupertype(self.type_input)
                return False
            return NotImplemented

    def register(self, factory, type_input, type_output, priority=RegistryBase.PRIORITY_MIDDLE):
        Register a factory

        :param factory: Factory to register. Callable, must return an object.
        return self._register(self.Entry(factory, type_input, type_output, priority))

default_registry = RegistryConverter()
load_package_modules(__name__, __path__)