Proposal: Plugins should be modules
Posted: Sun Jun 10, 2007 9:10 am
Currently plugins are implemented as directory which contains a 'plugin.py' file, which is executed when the plugin loads. This registers the plugin class and defines some parameters. However this tends impose artificial limitations on the plugin (e.g. normal imports don't work as expected), and makes breaking the module up into smaller pieces more difficult (because any other *.py files in the module are not on the python path and therefore can't be imported).
I'd like to propose that the plugin structure be changed such that each plugin is a standalone python module. It would work like this:
I'd like to propose that the plugin structure be changed such that each plugin is a standalone python module. It would work like this:
- The plugin manager adds the plugin directories to the python path
- The plugin manager then scans the root of each directory and imports each module
- The module __init__.py contains some standard-named variables that plugin manager can register the plugin with (description, author, etc).
- The __init__.py would also contain 'enable', 'disable' and 'configure' functions that would invoked at the appropriate points.
Code: Select all
_plugins = {}
def loadPlugins(self, pdir='plugins'):
sys.path.append(pdir)
for path, dirs, files in os.walk(pdir):
if '__init__.py' in files:
elems = path.split('/')
modname = '.'.join(elems)
# Import the found module. Note that the last
# parameter is important otherwise only the base
# modules (ie. 'plugins') is imported. This appears
# to be by design.
mod = __import__(modname, globals(), locals(), [''])
if 'init' in dir(mod):
plugin = mod.init(self)
if plugin:
self._plugins[modname] = plugin