Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How to write python plug-ins for your own python program? (http://www.velocityreviews.com/forums/t342190-how-to-write-python-plug-ins-for-your-own-python-program.html)

Tian 03-03-2005 05:28 AM

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


alex23 03-03-2005 05:51 AM

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

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


Simon Wittber 03-03-2005 08:33 AM

Re: How to write python plug-ins for your own python program?
 
> 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.

Mark Rowe 03-03-2005 08:54 AM

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

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


Terry Hancock 03-03-2005 09:27 AM

Re: How to write python plug-ins for your own python program?
 
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


David M. Cooke 03-03-2005 05:48 PM

Re: How to write python plug-ins for your own python program?
 
Simon Wittber <simonwittber@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

Andrew Dalke 03-03-2005 08:19 PM

Re: How to write python plug-ins for your own python program?
 
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
dalke@dalkescientific.com


Andre 03-04-2005 02:12 AM

Re: How to write python plug-ins for your own python program?
 
Mark Rowe <mail.python.org@bdash.net.nz> wrote in message news:<mailman.3293.1109840097.22381.python-list@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.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

hemanth 03-04-2005 06:44 AM

Re: How to write python plug-ins for your own python program?
 
Andre wrote:
> Mark Rowe <mail.python.org@bdash.net.nz> wrote in message

news:<mailman.3293.1109840097.22381.python-list@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.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.


Reinhold Birkenfeld 03-05-2005 08:48 AM

Re: How to write python plug-ins for your own python program?
 
David M. Cooke wrote:
> Simon Wittber <simonwittber@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


All times are GMT. The time now is 09:14 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.