Created
January 9, 2011 18:30
-
-
Save saghul/771884 to your computer and use it in GitHub Desktop.
Example of a plugin architecture with metaclasses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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