[Tutor] Plugin system - how to manage plugin files?
quasipedia at gmail.com
Wed Jul 28 17:07:44 CEST 2010
This is more of a python software design question rather than a
question on the Python syntax alone. I hope it is fine to ask here.
I am working on a small project, that I will release under a GPL
license, whose main design guiding principle is extensibility. Thus I am
trying to make as easy as possible for other devs to create plugins.
I implemented my plugin system by defining a metaclass, that register -
with a class method - the class name of any plugin that gets loaded (all
plugins inherit from a class defined in the core code). Basically this
means that a developer will have to define his class as:
And the main program will know of his presence because "PluginMyName"
will be included in the list available at "MotherOfAllPlugins.plugins".
Being the mounting method a class method, all plugins get registered
when their class code is loaded into memory (so even before an object of
that class is instantiated).
The system works good enough for me (although I am always open to
suggestions on how to improve the architecture!) but I am now wondering
what would be the best way to manage the plugin files (i.e. where and
how the files containing the plugins should be stored).
So far I am using - for developing purposes - a package that I called
"plugins". I put all my pluginX.py files in the package directory and I
simply have to issue "import plugins" in the main.py file, for all the
plugins to get mounted properly. However this system is only good while
I am developing and testing the code, as:
1. Plugins might come with their own unittests, and I do not want to
load those in memory.
2. All plugins are currently loaded into memory, but indeed there are
certain plugins which are alternative versions of the same feature, so
you really just need to know that you can switch between the two, but
you want to load into memory just one.
3. At some point, I will want to have a double location for installing
plugins: a system-wide location (for example /usr/local/bin/) and a
user-specific one (for example /home/<user>/.myprogram/).
So my questions are really - perhaps - three:
1) What is the most sensible format for the plugin code (a file? a
package? A simple directory of files?)
2) Which is a smart way to use Python introspection to recognise the
presence of plugins without necessarily loading (importing) them.
3) Is there a standard way / best practice (under gnu/linux, at least)
to allow for plugins to be placed in two different locations?
Thank you in advance for your time and guidance,
More information about the Tutor