插件管理器

import osfrom functools import partialfrom pluginbase import PluginBase# For easier usage calculate the path relative to here.here = os.path.abspath(os.path.dirname(__file__))# 函数包装，重新定义函数签名，减少调用参数get_path = partial(os.path.join, here)print('here:', here)print('get_path:', get_path)# Setup a plugin base for "example.modules" and make sure to load# all the default built-in plugins from the builtin_plugins folder.plugin_base = PluginBase(package='example.plugins', searchpath=[get_path('./builtin_plugins')])classApplication(object):"""Represents a simple example application."""def__init__(self, name):# Each application has a name self.name = name# And a dictionary where it stores "formatters". These will be# functions provided by plugins which format strings. self.formatters = {}# and a source which loads the plugins from the "{app_name}/plugins"# folder. We also pass the application name as identifier. This# is optional but by doing this out plugins have consistent# internal module names which allows pickle to work. self.source = plugin_base.make_plugin_source( searchpath=[get_path('./%s/plugins' % name)], identifier=self.name)# Here we list all the plugins the source knows about, load them# and the use the "setup" function provided by the plugin to# initialize the plugin.for plugin_name in self.source.list_plugins(): plugin = self.source.load_plugin(plugin_name) plugin.setup(self)defregister_formatter(self, name, formatter):"""A function a plugin can use to register a formatter.""" self.formatters[name] = formatter