Ejemplo n.º 1
0
    def finalize(self):
        """
        Finalize the registration, instantiate the plugins.

        `API.bootstrap` will automatically be called if it hasn't been
        already.
        """
        self.__doing('finalize')
        self.__do_if_not_done('load_plugins')

        production_mode = self.is_production_mode()
        plugins = {}
        plugin_info = {}

        for base in self.bases:
            name = base.__name__
            sub_d = self.__plugins.get(base, {})

            members = []
            for klass in sub_d.itervalues():
                try:
                    instance = plugins[klass]
                except KeyError:
                    instance = plugins[klass] = klass(self)
                members.append(instance)
                plugin_info.setdefault(
                    '%s.%s' % (klass.__module__, klass.__name__),
                    []).append(name)

            if not production_mode:
                assert not hasattr(self, name)
            setattr(self, name, NameSpace(members))

        for klass, instance in plugins.iteritems():
            if not production_mode:
                assert instance.api is self
            if klass.finalize_early or not self.env.plugins_on_demand:
                instance.ensure_finalized()
                if not production_mode:
                    assert islocked(instance)

        self.__finalized = True
        self.plugins = tuple((k, tuple(v)) for k, v in plugin_info.iteritems())

        if not production_mode:
            lock(self)
Ejemplo n.º 2
0
    def finalize(self):
        """
        Finalize the registration, instantiate the plugins.

        `API.bootstrap` will automatically be called if it hasn't been
        already.
        """
        self.__doing('finalize')
        self.__do_if_not_done('load_plugins')

        class PluginInstance(object):
            """
            Represents a plugin instance.
            """

            i = 0

            def __init__(self, klass):
                self.created = self.next()
                self.klass = klass
                self.instance = klass()
                self.bases = []

            @classmethod
            def next(cls):
                cls.i += 1
                return cls.i

        class PluginInfo(ReadOnly):
            def __init__(self, p):
                assert isinstance(p, PluginInstance)
                self.created = p.created
                self.name = p.klass.__name__
                self.module = str(p.klass.__module__)
                self.plugin = '%s.%s' % (self.module, self.name)
                self.bases = tuple(b.__name__ for b in p.bases)
                if not is_production_mode(self):
                    lock(self)

        plugins = {}
        tofinalize = set()

        def plugin_iter(base, subclasses):
            for klass in subclasses:
                assert issubclass(klass, base)
                if klass not in plugins:
                    plugins[klass] = PluginInstance(klass)
                p = plugins[klass]
                if not is_production_mode(self):
                    assert base not in p.bases
                p.bases.append(base)
                if klass.finalize_early or not self.env.plugins_on_demand:
                    tofinalize.add(p)
                yield p.instance

        production_mode = is_production_mode(self)
        for base, subclasses in self.__registry.iter(*self.__allowed):
            name = base.__name__
            namespace = NameSpace(plugin_iter(base, subclasses))
            if not production_mode:
                assert not (name in self.__d or hasattr(self, name))
            self.__d[name] = namespace
            object.__setattr__(self, name, namespace)

        for p in plugins.itervalues():
            p.instance.set_api(self)
            if not production_mode:
                assert p.instance.api is self

        for p in tofinalize:
            p.instance.ensure_finalized()
            if not production_mode:
                assert islocked(p.instance) is True
        object.__setattr__(self, '_API__finalized', True)
        tuple(PluginInfo(p) for p in plugins.itervalues())
        object.__setattr__(self, 'plugins',
                           tuple(PluginInfo(p) for p in plugins.itervalues()))
Ejemplo n.º 3
0
    def finalize(self):
        """
        Finalize the registration, instantiate the plugins.

        `API.bootstrap` will automatically be called if it hasn't been
        already.
        """
        self.__doing('finalize')
        self.__do_if_not_done('load_plugins')

        class PluginInstance(object):
            """
            Represents a plugin instance.
            """

            i = 0

            def __init__(self, klass):
                self.created = self.next()
                self.klass = klass
                self.instance = klass()
                self.bases = []

            @classmethod
            def next(cls):
                cls.i += 1
                return cls.i

        class PluginInfo(ReadOnly):
            def __init__(self, p):
                assert isinstance(p, PluginInstance)
                self.created = p.created
                self.name = p.klass.__name__
                self.module = str(p.klass.__module__)
                self.plugin = '%s.%s' % (self.module, self.name)
                self.bases = tuple(b.__name__ for b in p.bases)
                if not is_production_mode(self):
                    lock(self)

        plugins = {}
        tofinalize = set()
        def plugin_iter(base, subclasses):
            for klass in subclasses:
                assert issubclass(klass, base)
                if klass not in plugins:
                    plugins[klass] = PluginInstance(klass)
                p = plugins[klass]
                if not is_production_mode(self):
                    assert base not in p.bases
                p.bases.append(base)
                if klass.finalize_early or not self.env.plugins_on_demand:
                    tofinalize.add(p)
                yield p.instance

        production_mode = is_production_mode(self)
        for base, subclasses in self.__registry.iter(*self.__allowed):
            name = base.__name__
            namespace = NameSpace(
                plugin_iter(base, subclasses)
            )
            if not production_mode:
                assert not (
                    name in self.__d or hasattr(self, name)
                )
            self.__d[name] = namespace
            object.__setattr__(self, name, namespace)

        for p in plugins.itervalues():
            p.instance.set_api(self)
            if not production_mode:
                assert p.instance.api is self

        for p in tofinalize:
            p.instance.ensure_finalized()
            if not production_mode:
                assert islocked(p.instance) is True
        object.__setattr__(self, '_API__finalized', True)
        tuple(PluginInfo(p) for p in plugins.itervalues())
        object.__setattr__(self, 'plugins',
            tuple(PluginInfo(p) for p in plugins.itervalues())
        )