- January 10, 2008
- 16 (after 16 ratings)
This is a very basic -- yet fully functional -- framework for producing a loosely coupled plugin architecture. Full details of its use can be found on my blog, but the basics are listed below.
Defining a mount point for plugins
class ActionProvider: __metaclass__ = PluginMount
class Insert(ActionProvider): def perform(self): # Do stuff here class Update(ActionProvider): def perform(self): # Do stuff here
for action in ActionProvider.plugins: action.perform()
Yes, it really is that simple.
1 2 3 4 5 6 7 8 9 10 11 12 13
class PluginMount(type): def __init__(cls, name, bases, attrs): if not hasattr(cls, 'plugins'): # This branch only executes when processing the mount point itself. # So, since this is a new plugin type, not an implementation, this # class shouldn't be registered as a plugin. Instead, it sets up a # list where plugins can be registered later. cls.plugins =  else: # This must be a plugin implementation, which should be registered. # Simply appending it to the list is all that's needed to keep # track of it later. cls.plugins.append(cls)