How to provide an API to extend a Python program using plugins?

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.

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.

+5
source share
2 answers

, , . (, ) ( ). , . ( ) , . , . , , .

, , , (, __plug_file.py__ __plug_other.py__).

+2

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

0

All Articles