473,573 Members | 3,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2445

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.someMeth od()

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.someMeth od()

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__(plug inName)
plugin.someMeth od()

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 anansispacework s.com )
Anansi Spaceworks http://www.anansispaceworks.com

Jul 18 '05 #5
Simon Wittber <si**********@g mail.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.someMeth od()

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)phy sics(dot)mcmast er(dot)ca
Jul 18 '05 #6
Mark Rowe wrote:
A better method would be something along the lines of:

plugin = __import__(plug inName)
plugin.someMeth od()


In the one time I did a plugin architecture I found that
state = ... set up intial state for my program ...
...
plugin = __import__(plug inName)
plugin.someMeth od(state)
was useful because it gave the plugin a way to modify
the program state, rather than changing global variables.

Andrew
da***@dalkescie ntific.com

Jul 18 '05 #7
Mark Rowe <ma************ *@bdash.net.nz> wrote in message news:<ma******* *************** *************** **@python.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.someMeth od()

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__(plug inName)
plugin.someMeth od()

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******* *************** *************** **@python.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.someMeth od()

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__(plug inName)
plugin.someMeth od()

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**********@g mail.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.someMeth od()

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
1740
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 the error and take the user to a login page. All of the errors return from my web services come back as RemoteException objects. Is there a way to...
2
4333
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 logging. It must be simple to enable the detailed output in the live application. I have used Debug.Write() but this seems to come out only on the...
3
1590
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 that I could implement to get over this problem? I am relatively new to databases, but have programming in java for a while.
6
3665
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 that I could try? Thanks, Graham
2
1560
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 named. I have registered the main DLL, which references the other DLLs, to the GAC cache. I have built a plug-in for a third party application with...
0
1073
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
1349
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 they get developed. The 'interface' for all the algorithms will be same...only the implementation keeps changing. The main application will make a...
1
1343
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 I iterate through the DLL's in the plug in's directory, when it finds my plug in dll it claims there are no types in the assembly. Assembly...
4
1426
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 information such as author, description, functions, parameters, et cetera to the proxy -- the application then polls the proxy at runtime.
0
7777
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7686
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8016
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8061
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5584
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3731
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2197
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1038
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.