469,916 Members | 2,523 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

Plug-Ins In A Python Application

Its the Java developer again...

I'm working on an application framework that I would like to implement
in Python. Part of the application framework is a plug-in model that is
similar to the one used in Eclipse.

Is it possible to load and use "modules" containing plug-in code
written by third party developers into a running instance of the
framework? How would I do this? Do I need to "dynamically load" the
module at runtime? (I will scan a folder in the application direcotry
for XML files containing information about the plug-ins, including the
modules that implement them.)

Thanks,

Scott Huey

Apr 18 '06 #1
3 1099
re****************@gmail.com wrote:
Its the Java developer again...

I'm working on an application framework that I would like to implement
in Python. Part of the application framework is a plug-in model that is
similar to the one used in Eclipse.

Is it possible to load and use "modules" containing plug-in code
written by third party developers into a running instance of the
framework? How would I do this? Do I need to "dynamically load" the
module at runtime? (I will scan a folder in the application direcotry
for XML files containing information about the plug-ins, including the
modules that implement them.)

Thanks,

Scott Huey

Look at the "imp" module.

Any module can be imported at any time in your code. The import
statement is usually used to the top of a file, making it *look* like a
declaration, but in fact it may be executed anywhere. The import
statements does, however, hard code its module's name. For a plugin
system, you'll probably want to import a module given a string
containing its name. The "imp" module provides this as well as access
to many of the features of the import mechanism.

Gary Herron

Apr 18 '06 #2

On Apr 18, 2006, at 9:58 AM, re****************@gmail.com wrote:
Its the Java developer again...

I'm working on an application framework that I would like to implement
in Python. Part of the application framework is a plug-in model that is
similar to the one used in Eclipse.

Is it possible to load and use "modules" containing plug-in code
written by third party developers into a running instance of the
framework? How would I do this? Do I need to "dynamically load" the
module at runtime? (I will scan a folder in the application direcotry
for XML files containing information about the plug-ins, including the
modules that implement them.)


Well, the "state of the art" in Python plugins is moving towards Eggs:
http://peak.telecommunity.com/DevCenter/PythonEggs

Though that might be a bit much for you to jump into considering you're
just starting out in Python. However, if you do learn it now, you won't
have to learn it again later when everyone's using it :)

Jay P.

Apr 18 '06 #3
re****************@gmail.com wrote:
Is it possible to load and use "modules" containing plug-in code
written by third party developers into a running instance of the
framework? How would I do this? Do I need to "dynamically load" the
module at runtime? (I will scan a folder in the application direcotry
for XML files containing information about the plug-ins, including the
modules that implement them.)


a simple approach is to do something like

for file in list_of_plugins:
ns = {}
execfile(file, ns)
# pick up interesting objects from the namespace dictionary
# e.g.
callback = ns["callback"]

where individual plugins might look something like

# my plugin

def callback(event):
pass # do something here

You can prepopulate the namespace to make application-specific
objects available for the plugins:

context = MyContext()

for file in list_of_plugins:
ns = dict(context=context)
execfile(file, ns)

which allows the plugins to do e.g.

# my plugin

def callback(event):
pass

context.register(callback)

or you can use a plugin initialization function:

context = MyContext()

for file in list_of_plugins:
ns = {}
execfile(file, ns)
setup = ns["setup"]
setup(context)

where the plugins look like:

def callback(event):
pass

def setup(context):
context.register(callback)

and so on. To learn more about this, look up exec/execfile and
__import__ in the library reference.

</F>

Apr 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Patrick | last post: by
13 posts views Thread by Mike | last post: by
6 posts views Thread by Graham Ashton | last post: by
3 posts views Thread by Muhammad Aftab Alam | last post: by
reply views Thread by hespamme | last post: by
1 post views Thread by DeveloperX | last post: by
2 posts views Thread by iftikar | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.