My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads

Python Plugin Framework

PyPlugin 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 manager

There 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 interface

You can define a plugin interface in `pkg/plugins/interface.py`, then implement them in your plugins.

Writing plugins

There are two basic rules for writing plugins:

  1. Plugin class should subclass Plugin.
  2. Plugin class should at least implement one interface in interface.

Register plugins

Important 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 plugins

After 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

  1. python-nose
  2. setuptools
Powered by Google Project Hosting