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)
|
More like this
- HTTP (basic) auth enabled (new-style) syndication framework feed class by hupf 2 years, 5 months ago
- integrated jinja2 which could use generic view ,my djangojinja2.py by jasongreen 3 years, 4 months ago
- EasyFeed class by limodou 6 years, 2 months ago
- Using descriptors for lazy attribute caching by djypsy 5 years, 9 months ago
- A action decorator for URLs by Batiste 5 years, 1 month ago
Comments
This is just unbelievable awesome. Great snippet and weblog post!
#
Help...
I received this error:
Here is the code....
#
This is a little late, but just informational for everyone else.
The poster above me encountered the error because action (which is a class), is not being instantiated first.
So the fix is: if name == 'main': for action in ActionProvider.plugins: action().perform()
Really simple. :) And this idea really rocks.
#
Oops, here's the code again, correctly formatted.
#
Hi, really nice and neat solution. Just a thing, I don't see a way to do this if your plugins are defined in different module files without having to look for them somewhere. Just an example:
main.py plugins/ metaclass.py plugin.py init.py plugin1.py plugin2.py etc..
and in init.py: all = [p for p in glob.glob(....)...]
which quite destroy the benefit not to look for anything. Am I missing something ?
Thanks for the idea anyway !!
#