Python Plugin FrameworkPyPlugin is a simple plugin framework whitch is designed to have little dependency and easily to integrate with other system. The plugin framework provides: - A specification to define a plugin interface
- A specification to create customized plugin managers
- Some commonly used plugin managers
Plugin managerThere are some commonly used plugin managers in `pkg/plugins/manager.py`. To use one of them, just: from pkg.plugins.manager import DefaultPluginManager
manager = DefaultPluginManager()
manager.loadPlugins()
plugins = manager.getPlugins()
# After some plugin change, refresh them
manager.plguins = []
manager.loadPlugins()
plugins = manager.getPlugins()
Plugin interfaceYou can define a plugin interface in `pkg/plugins/interface.py`, then implement them in your plugins. Writing pluginsThere are two basic rules for writing plugins: - Plugin class should subclass Plugin.
- Plugin class should at least implement one interface in interface.
Register pluginsImportant note: different plugin managers may use different means to locate and load plugins. For EntryPointPluginManager, it must be part of a package that uses setuptools, and the plugin must be included in the entry points defined in the setup.py for the package: setup(name="Some plugin",
...
entry_points = {
"package.plugins": [
"someplugin = someplugin:SomePlugin"
]
},
...
)
Once the package is installed with install or develop, nose will be able to load the plugin. For DirectoryPluginManager, it must be placed under directory plugins/. Every plugin file should have a __all__ attribute containing the implemented plugin class. Use pluginsAfter you have written you plugin and registered into the framework, the plugin manager should be able to load it. Then you can get a proxy of your plugin and call the implemented APIs defined in the interface: from pkg.plugins.manager import PluginProxy
try:
plugin = PluginProxy("interface", "name")
except NotImplementedError:
# exception handling
# call plugin method
plugin.method(args)
Reference- python-nose
- setuptools
|