Created
January 9, 2011 18:37
-
-
Save saghul/771888 to your computer and use it in GitHub Desktop.
Example of a plugin architecture using metaclasses and class decorator
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) | |
def plugin(cls): | |
"""Class decorator for adding plugins to the registry""" | |
PluginRegistry().add(cls()) | |
return cls | |
# Example plugin class | |
@plugin | |
class MyPlugin(object): | |
__metaclass__ = Singleton | |
def __init__(self): | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment