Plug-Ins In A Python Application

R

redefined.horizons

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
 
G

Gary Herron

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
 
F

Fredrik Lundh

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>
 
J

Jay Parlar

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top