I was wondering how I can provide an API for my Python program so that others can extend it with plugins. I thought of something like from myProgram.plugins import aClassToExtendByOthers, registerThatClass. But I do not know how to ensure this.
from myProgram.plugins import aClassToExtendByOthers, registerThatClass
I could use the exec statement in my function loadPluginsfor each plugin in the plugins folder, but that would not include importing materials that I would like to give people the opportunity to write these plugins.
loadPlugins
, , . (, ) ( ). , . ( ) , . , . , , .
, , , (, __plug_file.py__ __plug_other.py__).
__plug_file.py__
__plug_other.py__
imp (. docs.python.org)
sys.path.insert(0, pluginsDir) lst = map(lambda x: os.path.splitext(os.path.basename(x))[0], glob.glob(os.path.join(pluginsDir, "*.py"))) for module in lst: try: f, fn, d = imp.find_module(module,[pluginsDir]) loaded = imp.load_module(module, f, fn, d)
. ojuba
http://git.ojuba.org/cgit/occ/tree/OjubaControlCenter/loader.py