473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

Jul 18 '05 #1
9 2432

Tian wrote:
Is there any good method to
read in python code and test availability and invoke the functions
inside?


You mean like 'import'? :)

- alex23

Jul 18 '05 #2
> 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.
Jul 18 '05 #3

On Mar 3, 2005, at 9:33 PM, Simon Wittber wrote:
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.


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

Jul 18 '05 #4
On Wednesday 02 March 2005 11:28 pm, Tian wrote:
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.p....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

--
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 18 '05 #5
Simon Wittber <si**********@gmail.com> writes:
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.


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

Use __import__ instead.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #6
Mark Rowe wrote:
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
da***@dalkescientific.com

Jul 18 '05 #7
Mark Rowe <ma*************@bdash.net.nz> wrote in message news:<ma***************************************@py thon.org>...
On Mar 3, 2005, at 9:33 PM, Simon Wittber wrote:
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.


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
Jul 18 '05 #8
Andre wrote:
Mark Rowe <ma*************@bdash.net.nz> wrote in message

news:<ma***************************************@py thon.org>...
On Mar 3, 2005, at 9:33 PM, Simon Wittber wrote:
> 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.


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

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.

Jul 18 '05 #9
David M. Cooke wrote:
Simon Wittber <si**********@gmail.com> writes:
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.


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
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Patrick | last post by:
I'm trying to figure out the best way to trap for specific errors when making calls to a web service. For instance, if the web service returns an "invalid login" error, I want to be able to trap...
2
by: Andy Fish | last post by:
Hi, I am wanting to put some simple logging into my .net app. The basic features will be that errors get logged to a file and that there will be a configuration flag to enable more detailed...
3
by: dufffman | last post by:
Hi, We essentially have (about 1000/sec) messages coming through a channel and I have to store them in a UDB database. Will UDB be able to write it that fast? If not what are a few strategies...
6
by: Graham Ashton | last post by:
Hi. I'm trying to edit C# code from within eclipse 2.1.1 but am getting nowhere. The Improve plug-in doesn't seem to install on such recent versions of eclipse; is there anything else out there...
2
by: Jon Davis | last post by:
I have a full-blown application that consists of several (fifteen or so) assembly DLLs, each being a separate VS.NET project that outputs to the main DLL's bin directory. They are all strongly...
0
by: siedem | last post by:
Hi I have to write a plugin to MS Outlook Express and MSN Messenger. Does anybody have any examples or documentation related with this topic? thanks in advance P.
3
by: Sinex | last post by:
Hi, I want to build an application that triggers different algorithms. The algorithms will be developed as class libraries over a period of time. I want to just plug-in these libraries as and when...
1
by: DeveloperX | last post by:
I've knocked up a quick plug in project (actually it's 6 projects, but more on that in a moment). This project is referenced by a shared add in (used for Excel). The issue I have is that when...
4
by: -Lost | last post by:
How should one write a plug-in interface? I've tossed around several ideas but rudimentary ones at best. For example: Plug-In-A -Plug-In-Proxy -Application The plug-in simply hands off its...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.