Skip to content

Instantly share code, notes, and snippets.

@saghul
Created January 9, 2011 18:30
Show Gist options
  • Save saghul/771884 to your computer and use it in GitHub Desktop.
Save saghul/771884 to your computer and use it in GitHub Desktop.
Example of a plugin architecture with metaclasses
from application.python.util import Singleton
class PluginRegistry(object):
__metaclass__ = Singleton
def __init__(self):
self.plugins = []
def __iter__(self):
return iter(self.plugins)
def add(self, cls):
if cls not in self.plugins:
self.plugins.append(cls)
class Plugin(type):
"""Metaclass for adding a plugin to the registry"""
def __init__(cls, name, bases, dic):
super(Plugin, cls).__init__(name, bases, dic)
PluginRegistry().add(cls)
# Example plugin class
class MyPlugin(object):
__metaclass__ = Plugin
def __init__(self):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment