?
Solved

Using import to create a "plugin" architecture

Posted on 2004-11-04
3
Medium Priority
?
242 Views
Last Modified: 2013-11-18
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
0
Comment
Question by:subject42
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
RichieHindle earned 1200 total points
ID: 12499235
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
 

Author Comment

by:subject42
ID: 12519860
Sir,
You rock my world.
0
 
LVL 14

Expert Comment

by:RichieHindle
ID: 12521419
8-)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question