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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tian

    #1

    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

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


    Tian wrote:[color=blue]
    > Is there any good method to
    > read in python code and test availability and invoke the functions
    > inside?[/color]

    You mean like 'import'? :)

    - alex23

    Comment

    • Simon Wittber

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

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

      Sw.

      Comment

      • Mark Rowe

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


        On Mar 3, 2005, at 9:33 PM, Simon Wittber wrote:
        [color=blue][color=green]
        >> You mean like 'import'? :)[/color]
        >
        > 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.[/color]

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

        Comment

        • Terry Hancock

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

          On Wednesday 02 March 2005 11:28 pm, Tian wrote:[color=blue]
          > 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?[/color]

          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):

          Download Narya Project Incubator and Forum for free. Narya is a forum/incubator software, based on a Python/Zope/MySQL platform. Emphasis on graphics support and collaboration for space and technology development.


          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

          Comment

          • David M. Cooke

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

            Simon Wittber <simonwittber@g mail.com> writes:
            [color=blue][color=green]
            >> You mean like 'import'? :)[/color]
            >
            > 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.[/color]

            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

            Comment

            • Andrew Dalke

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

              Mark Rowe wrote:[color=blue]
              > A better method would be something along the lines of:
              >
              > plugin = __import__(plug inName)
              > plugin.someMeth od()[/color]

              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
              dalke@dalkescie ntific.com

              Comment

              • Andre

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

                Mark Rowe <mail.python.or g@bdash.net.nz> wrote in message news:<mailman.3 293.1109840097. 22381.python-list@python.org >...[color=blue]
                > On Mar 3, 2005, at 9:33 PM, Simon Wittber wrote:
                >[color=green][color=darkred]
                > >> You mean like 'import'? :)[/color]
                > >
                > > 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.[/color]
                >
                > 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.
                >[/color]
                What happens if you have:
                ..def someMethod():
                .. import os
                .. rm * # or whatever other evil thing you might thing of

                Andre

                Comment

                • hemanth

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

                  Andre wrote:[color=blue]
                  > Mark Rowe <mail.python.or g@bdash.net.nz> wrote in message[/color]
                  news:<mailman.3 293.1109840097. 22381.python-list@python.org >...[color=blue][color=green]
                  > > On Mar 3, 2005, at 9:33 PM, Simon Wittber wrote:
                  > >[color=darkred]
                  > > >> 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.[/color]
                  > >
                  > > 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[/color][/color]
                  well as[color=blue][color=green]
                  > > the need to parse + interpret the string.
                  > >[/color]
                  > What happens if you have:
                  > .def someMethod():
                  > . import os
                  > . rm * # or whatever other evil thing you might thing of
                  >
                  > Andre[/color]


                  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.

                  Comment

                  • Reinhold Birkenfeld

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

                    David M. Cooke wrote:[color=blue]
                    > Simon Wittber <simonwittber@g mail.com> writes:
                    >[color=green][color=darkred]
                    >>> You mean like 'import'? :)[/color]
                    >>
                    >> 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.[/color]
                    >
                    > You'd better hope someone doesn't name their plugin
                    > 'os; os.system("rm -rf /"); import sys'[/color]
                    ^
                    Well, that would be difficult, but "rm -rf ~" would work rather nicely...

                    Of course, one could test pluginName that it contains only
                    alphanumerics, but
                    [color=blue]
                    > Use __import__ instead.[/color]

                    is surely the better solution.

                    Reinhold

                    Comment

                    Working...