Allow "third party" plugins for my Django app, that don't require config changes?
I would like to know of a good approach, or programming "pattern" to apply, to allow users of my Django application to drop in "plugins" or "extensions" for my app, without having to add anything to 'installed apps' or edit any configuration.
WordPress plugins probably being the best example. New options/settings/menus become available, but I don't have to edit the WordPress config or core files - they just show up in my admin.
In Django/Python, what approach or programming "pattern" would you use to begin to develop a type of plugin architecture?
I know that WordPress (the example given) is a 'platform' itself, and provides a plugin API, etc. My question is about the patterns involved, and the early stages - preparing for plugins before the application is built, rather than trying to add that functionality later.
To be specific, my app accepts "content", and I'd like to provide a way for users to drop in mo开发者_如何学编程dular "transformers" that provide additional outputs of that content (not just a filter) that may or may not need to accept a few basic settings.
My suggestion would be to use the Django signal system. Your app should send signals when certain actions occur and then listen to them when the next stage happens. If someone wants to change the behaviour of your app, all they would need to do would be to listen to the relevant signals that your app sends out and modify the data appropriately.
This allows for very easy expansion and change of applications without needing to even touch your apps source code.
I would make a point though, if someone is using your app it is unlikely to be installed in the project folder so requiring them to place code your apps folder is a bad idea. It will most likely be installed as a module within a virtualenv. Using the signals method I outlined above the person using your app can then just add the handlers in the app in their project that it makes most sense to place the handlers in. Thus they never need to touch your apps source code directory.
There is a django-plugins in pypi that allows you to define "plugin points", making it easier to allow your app to have plugins.
See: http://pypi.python.org/pypi/django-plugins
精彩评论