How to write python plug-ins for your own python program?

T

Tian

I am writing an audio game using Python. in this game you can apply
some sound effects for the clips you have recorded. I want to make this
function extensible. I want user to be able to add new sound effect
plug-ins in the future.

I want the plug-in to be a simple python code (text file) and a
description file. I will set some rules for plug-in writing (like you
must inherit some class and implement some method). I hope plugin can
be added while original program is running. Is there any good method to
read in python code and test availability and invoke the functions
inside?

Thanks


Tian
 
S

Simon Wittber

You mean like 'import'? :)

That's how I would do it. It's the simplest thing, that works.

exec("import %s as plugin" % pluginName)
plugin.someMethod()

where pluginName is the name of the python file, minus the ".py" extension.

Sw.
 
M

Mark Rowe

That's how I would do it. It's the simplest thing, that works.

exec("import %s as plugin" % pluginName)
plugin.someMethod()

where pluginName is the name of the python file, minus the ".py"
extension.

A better method would be something along the lines of:

plugin = __import__(pluginName)
plugin.someMethod()

This avoids the potential security problem that `exec' poses as well as
the need to parse + interpret the string.

Regards,

Mark Rowe
<http://bdash.net.nz/>
 
T

Terry Hancock

I am writing an audio game using Python. in this game you can apply
some sound effects for the clips you have recorded. I want to make this
function extensible. I want user to be able to add new sound effect
plug-ins in the future.

I want the plug-in to be a simple python code (text file) and a
description file. I will set some rules for plug-in writing (like you
must inherit some class and implement some method). I hope plugin can
be added while original program is running. Is there any good method to
read in python code and test availability and invoke the functions
inside?

It's not hard to scrape the contents of a directory and import any modules
you find (i.e. to implement a "plugin directory"). I do it here (beware line-wrap):

http://cvs.sourceforge.net/viewcvs....age/Operators/__init__.py?rev=1.1&view=markup

You might not want to have the reading code in the same directory as the
plugins -- you don't have to do it that way. Take a look at the __import__()
built-in in the Python Library Reference for more information.

I'm sure it's possible to run this code periodically at runtime, although I
prefer to do it only at startup (what if one of the plugins is faulty and
crashes the program?).

Cheers,
Terry
 
D

David M. Cooke

Simon Wittber said:
That's how I would do it. It's the simplest thing, that works.

exec("import %s as plugin" % pluginName)
plugin.someMethod()

where pluginName is the name of the python file, minus the ".py" extension.

You'd better hope someone doesn't name their plugin
'os; os.system("rm -rf /"); import sys'

Use __import__ instead.
 
A

Andrew Dalke

Mark said:
A better method would be something along the lines of:

plugin = __import__(pluginName)
plugin.someMethod()

In the one time I did a plugin architecture I found that


state = ... set up intial state for my program ...
...
plugin = __import__(pluginName)
plugin.someMethod(state)


was useful because it gave the plugin a way to modify
the program state, rather than changing global variables.

Andrew
(e-mail address removed)
 
A

Andre

Mark Rowe said:
A better method would be something along the lines of:

plugin = __import__(pluginName)
plugin.someMethod()

This avoids the potential security problem that `exec' poses as well as
the need to parse + interpret the string.
What happens if you have:
..def someMethod():
.. import os
.. rm * # or whatever other evil thing you might thing of

Andre
 
H

hemanth

Andre said:
Mark Rowe <[email protected]> wrote in message
What happens if you have:
.def someMethod():
. import os
. rm * # or whatever other evil thing you might thing of

Andre


Some time back I remember discussions on plugin risks in
Leo (leo.sf.net). The conclusion was someone can always harm
your system by writing a nasty plugin. Hence you should always
use plugins from sources you can trust. I don't know if there
is any alternative way in Python to have safe third party
plugins.
 
R

Reinhold Birkenfeld

David said:
You'd better hope someone doesn't name their plugin
'os; os.system("rm -rf /"); import sys'
^
Well, that would be difficult, but "rm -rf ~" would work rather nicely...

Of course, one could test pluginName that it contains only
alphanumerics, but
Use __import__ instead.

is surely the better solution.

Reinhold
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top