Package: extensions
extensionsis a simple plugin system inspired from setuptools entry points[1]. It allows an application to define and/or use plugins.Code base :http://bitbucket.org/tarek/extensionsWebsite :http://pypi.python.org/pypi/extensionsFeedback :tarek@ziade.orgContentsHow to define a pluginUsing a configuration fileHow to use a pluginIterate over registered pluginsThe Plugin objectDistribute your pluginsCompatibility with setuptools entry pointsReferencesHow to define a pluginA plugin can be any callable object . It has to be registered through theextensionsregistry.For example, let’s take a simple function that calculates the average of
some numbers, and let’s save it into a file calledextensions.pyin a
package calledmyapp:def average(*args):
     return sum(args) / len(args)This function can be registered in the plugin system using theregisterfunction. Plugins have anameand belong to agroup. For our
example, the group can bemyapp.operatorand the nameaverage:from extensions import register

# usage : register(group, name, location)
register('myapp.operator', 'average', 'myapp.extensions:average')The third parameter gives the location of the callable, with the formmodule_name:attrs, wheremodule_nameis the full name of the module,
andattrsthe attributes in the module.Notice that the group name includes the name of the package, which is a good
practice to avoid collisions since the group names are global to all applications
that useextensions.Using a configuration fileThere’s another way to register your plugins by using a configuration file.
You can write a ini-like file, using groups for sections title, andname = locationfor the values.For example, if you create aoperators.cfgfile with this content:[myapp.operator]

average = myapp.extensions:averageYou will be able to load it usingregister_file:from extensions import register_file

register_file('operators.cfg')This function will scan your file and register the plugins for you.How to use a pluginIterate over registered pluginsextensionsprovides agetfunction that allows you to iterate over all
registered plugins for a given group:from extensions import get

for plugin in get(group='myapp.operator'):
    print plugin.nameYou can also give thenameto the function:for plugin in get(group='myapp.operator', name='average'):
     print plugin.nameOr even iterate overallplugins:from itertools import islice

for plugin in islice(get(), 5):
    print plugin.nameThe Plugin objectThe objects returned by thegetfunction arePluginclass instances.ThePluginclass provides one method calledload, that returns
the registered object, so you can use it# let's get the plugin `average` of the group `myapp.operator`
plugin = get(group='myapp.operator', name='average').next()

# let's load it
func = plugin.load()

# let's use it now
average = func(1, 2, 3)Plugin also provides anameand agroupattribute, that corresponds
to thenameof the registered plugin, and to itsgroup.Distribute your pluginsIf you want to distribute your plugins, you just have to import the module
that registers the plugins into yoursetup.pyfile:from distutils.core import setup
from myapp import plugins  # registers the plugins

setup(name='myapp', version='1.0'
      packages=['myapp'])This will register the plugins when the package is installed by creating
a special file calledPLUGINSinto the.egg-infodirectory created when
your package is installed.Compatibility with setuptools entry pointsextensionsis fully compatible with setuptools entry points. So you can iterate
and use entry points defined in third-party applications that are installed
in your Python.If you want to iterate through setuptools entry points, use theconsume_entry_pointsoption when you call thegetfunction:plugins = get(consume_entry_points=True)This will iterate overextensionspluginsandsetuptools entry points.References[1]http://peak.telecommunity.com/DevCenter/setuptools#extensible-applications-and-frameworks