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

Discussion in 'Python' started by Tian, Mar 3, 2005.

  1. Tian

    Tian Guest

    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
    Tian, Mar 3, 2005
    #1
    1. Advertising

  2. Tian

    alex23 Guest

    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
    alex23, Mar 3, 2005
    #2
    1. Advertising

  3. > 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.
    Simon Wittber, Mar 3, 2005
    #3
  4. Tian

    Mark Rowe Guest

    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/>
    Mark Rowe, Mar 3, 2005
    #4
  5. 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....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

    --
    --
    Terry Hancock ( hancock at anansispaceworks.com )
    Anansi Spaceworks http://www.anansispaceworks.com
    Terry Hancock, Mar 3, 2005
    #5
  6. Simon Wittber <> 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
    David M. Cooke, Mar 3, 2005
    #6
  7. Tian

    Andrew Dalke Guest

    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
    Andrew Dalke, Mar 3, 2005
    #7
  8. Tian

    Andre Guest

    Mark Rowe <> wrote in message news:<>...
    > 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
    Andre, Mar 4, 2005
    #8
  9. Tian

    hemanth Guest

    Andre wrote:
    > Mark Rowe <> wrote in message

    news:<>...
    > > 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.
    hemanth, Mar 4, 2005
    #9
  10. David M. Cooke wrote:
    > Simon Wittber <> 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
    Reinhold Birkenfeld, Mar 5, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VGhlUGlua1BhbnRoZXI=?=

    Creating plug ins for an ASP.net app

    =?Utf-8?B?VGhlUGlua1BhbnRoZXI=?=, Feb 9, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    398
    Spam Catcher
    Feb 9, 2006
  2. Sameer
    Replies:
    0
    Views:
    347
    Sameer
    Mar 23, 2005
  3. Eric Nieuwland

    plug-ins

    Eric Nieuwland, May 7, 2005, in forum: Python
    Replies:
    0
    Views:
    388
    Eric Nieuwland
    May 7, 2005
  4. Replies:
    3
    Views:
    447
    Jay Parlar
    Apr 18, 2006
  5. George Maney
    Replies:
    1
    Views:
    274
    Lucien
    Jan 22, 2004
Loading...

Share This Page