开发者

does anybody know a simple base for a plug in python application?

I'm discovering python, and I want to create a plug in application, I guess this is simple using python, but it will be nice create a generic start point application.

To be more specific, it could be a file.py that reads a XML or INI file to get the plug in's directory path, then load all .py files as pl开发者_如何学JAVAug in

Any Ideas?


Since you are just starting out, I think it would be best to do all the work yourself so you really understand what is happening. It's not rocket science, and actually makes a fun learning experience IMO.

Start by forcing a hard-coded path to plugins. For example, ~/.myapp/plugins. In there, assume each .py file is a plugin. Require that each file in that directory implement a simple interface such as a known command you can call to create an instance of that plugin.

For example, a plugin might look like:

# MyPlugin.py
from myapp.plugin import Plugin # a base class you define

def create():
    return MyPlugin()

class MyPlugin(Plugin):
    ...

With that, you would load it with something like this:

import imp, os.path
filename=os.path.split(pathname)[-1]
modulename = os.path.splitext(filename)[0]
try:
    module = imp.load_source(modulename, pathname)
    plugin = module.create()
except ImportError, e:
    print "Error importing plugin '%s': %s" % (filename, str(e))

You now have an instance of your plugin class running, and a handle to it in the local variable plugin.

See? You don't need a fancy plugin framework to get started.

This isn't the only way to do it, and it's probably not even the best way. But once you get something like this working you can hammer out the details for what works best for you and your app.


I guess this depends on your level of "simple", but trac has a nice plug-in framework.

http://trac.edgewall.org/wiki/TracDev/ComponentArchitecture

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜