Plugin architecture - Plugin Manager vs inspecting from plugins import *

后端 未结 3 756
悲哀的现实
悲哀的现实 2020-12-29 14:55

I\'m currently writing an application which allows the user to extend it via a \'plugin\' type architecture. They can write additional python classes based on a BaseClass o

3条回答
  •  星月不相逢
    2020-12-29 15:03

    The approach from will-hart was the most useful one to me! For i needed more control I wrapped the Plugin Base class in a function like:

    def get_plugin_base(name='Plugin',
                           cls=object,
                           metaclass=PluginMount):
    
        def iter_func(self):
            for mod in self._models:
                yield mod
    
        bases = not isinstance(cls, tuple) and (cls,) or cls
    
        class_dict = dict(
            _models=None,
            session=None
        )
    
        class_dict['__iter__'] = iter_func
    
        return metaclass(name, bases, class_dict)
    

    and then:

    from plugin import get_plugin_base
    Plugin = get_plugin_base()
    

    This allows to add additional baseclasses or switching to another metaclass.

提交回复
热议问题