Using import to create a "plugin" architecture

Hello all,
I have a hairy problem that I'm working on.  I'd appreciate any help that you can offer.

I'm currently trying to build a "plugin" system using Python.  It would operate something like this.
-------------
A directory would hold a number of python modules.

Each module would define one function and a string giving an english language description of the function's purpose.

A function in the main program would loop over the files in the directory, importing each module.

The description of the function would be a dictionary key, with a reference to the function as it's value.
--------------

In my head it looks something like this:

def load_plugin_funcs(pluginDir):
    plugins = dict()
    sys.path.append(pluginDir)
    plugins = [x.strip(".py") for x in os.listdir(pluginDir) if x.endswith(".py")]
    for x in plugins:
        exec("import %s" % (x))
        plugins[MODULE_NAME_FROM_IMPORT.description] = MODULE_NAME_FROM_IMPORT.function
    return plugins

I really have no idea how to make something like this actually work, though.  Any suggestions?

Thanks
subject42Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RichieHindleCommented:
The key pieces you're misssing are new.module() to create a new module, and execfile() to execute the plugin code in the context of such a newly-created module.

Here's a full working example.  It has this structure:

ee-plugins          - top-level directory
   loadplugins.py - the main script that loads and runs the plugins
   plugins           - the directory in which the plugins live
      one.py        - an example plugin
      two.py        - another example plugin

*** loadplugins.py ****

import os, new

def load_plugin_funcs(pluginDir):
    plugins = dict()
    pluginNames = [x.strip(".py") for x in os.listdir(pluginDir) if x.endswith(".py")]
    for x in pluginNames:
        pluginModule = new.module(x)
        execfile(os.path.join(pluginDir, x+'.py'), pluginModule.__dict__)
        plugins[pluginModule.__doc__] = pluginModule.function
    return plugins

if __name__ == '__main__':
    plugins = load_plugin_funcs('plugins')
    for description, function in plugins.items():
        print description, function()

*** one.py ***

"""This is plugin one."""

def function():
    return "One"

*** two.py ***

"""This is plugin two."""

def function():
    return "Two"

*** end ***

Run loadplugins.py and you should see the following output:

This is plugin one. One
This is plugin two. Two
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
subject42Author Commented:
Sir,
You rock my world.
0
RichieHindleCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Components

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.