Proposal: Plugins should be modules

Suggestions and discussion of future versions
tarka
Member
Member
Posts: 22
Joined: Sat Jun 09, 2007 6:49 am

Proposal: Plugins should be modules

Post by tarka »

Currently plugins are implemented as directory which contains a 'plugin.py' file, which is executed when the plugin loads. This registers the plugin class and defines some parameters. However this tends impose artificial limitations on the plugin (e.g. normal imports don't work as expected), and makes breaking the module up into smaller pieces more difficult (because any other *.py files in the module are not on the python path and therefore can't be imported).

I'd like to propose that the plugin structure be changed such that each plugin is a standalone python module. It would work like this:
  • The plugin manager adds the plugin directories to the python path
  • The plugin manager then scans the root of each directory and imports each module
  • The module __init__.py contains some standard-named variables that plugin manager can register the plugin with (description, author, etc).
  • The __init__.py would also contain 'enable', 'disable' and 'configure' functions that would invoked at the appropriate points.
I'd be happy to do this, including porting the existing included plugins to the system. I've already implemented something similar for an experimental Jabber client I wrote a while ago. The code to do this is actually pretty simple (the following is ripped straight from that code and doesn't completely match the above, but you get the idea):

Code: Select all

    _plugins = {}
    def loadPlugins(self, pdir='plugins'):
        sys.path.append(pdir)
        for path, dirs, files in os.walk(pdir):
            if '__init__.py' in files:
                elems = path.split('/')
                modname = '.'.join(elems)

                # Import the found module. Note that the last
                # parameter is important otherwise only the base
                # modules (ie. 'plugins') is imported.  This appears
                # to be by design.
                mod = __import__(modname, globals(), locals(), [''])
                if 'init' in dir(mod):
                    plugin = mod.init(self)
                    if plugin:
                        self._plugins[modname] = plugin
andar
Top Bloke
Top Bloke
Posts: 1050
Joined: Fri Jun 08, 2007 8:38 pm
Location: Victoria, BC
Contact:

Re: Proposal: Plugins should be modules

Post by andar »

tarka wrote:Currently plugins are implemented as directory which contains a 'plugin.py' file, which is executed when the plugin loads. This registers the plugin class and defines some parameters. However this tends impose artificial limitations on the plugin (e.g. normal imports don't work as expected), and makes breaking the module up into smaller pieces more difficult (because any other *.py files in the module are not on the python path and therefore can't be imported).

I'd like to propose that the plugin structure be changed such that each plugin is a standalone python module. It would work like this:
  • The plugin manager adds the plugin directories to the python path
  • The plugin manager then scans the root of each directory and imports each module
  • The module __init__.py contains some standard-named variables that plugin manager can register the plugin with (description, author, etc).
  • The __init__.py would also contain 'enable', 'disable' and 'configure' functions that would invoked at the appropriate points.
I'd be happy to do this, including porting the existing included plugins to the system. I've already implemented something similar for an experimental Jabber client I wrote a while ago. The code to do this is actually pretty simple (the following is ripped straight from that code and doesn't completely match the above, but you get the idea):

Code: Select all

    _plugins = {}
    def loadPlugins(self, pdir='plugins'):
        sys.path.append(pdir)
        for path, dirs, files in os.walk(pdir):
            if '__init__.py' in files:
                elems = path.split('/')
                modname = '.'.join(elems)

                # Import the found module. Note that the last
                # parameter is important otherwise only the base
                # modules (ie. 'plugins') is imported.  This appears
                # to be by design.
                mod = __import__(modname, globals(), locals(), [''])
                if 'init' in dir(mod):
                    plugin = mod.init(self)
                    if plugin:
                        self._plugins[modname] = plugin
This sounds cool.. I was thinking of redoing the plugin system myself, but if you have already started then please feel free to send us a patch.
markybob
Compulsive Poster
Compulsive Poster
Posts: 1230
Joined: Thu May 24, 2007 11:27 pm
Location: Chicago, IL, USA
Contact:

Re: Proposal: Plugins should be modules

Post by markybob »

andar wrote: This sounds cool.. I was thinking of redoing the plugin system myself, but if you have already started then please feel free to send us a patch.
i'm with andar. feel free to shoot me an email markybob@gmail.com
User avatar
zachtib
Leecher
Leecher
Posts: 97
Joined: Thu May 24, 2007 3:26 pm
Location: Louisville, KY
Contact:

Re: Proposal: Plugins should be modules

Post by zachtib »

markybob wrote:
andar wrote: This sounds cool.. I was thinking of redoing the plugin system myself, but if you have already started then please feel free to send us a patch.
i'm with andar. feel free to shoot me an email markybob@gmail.com
it's a good idea, but won't be implemented till we start work on 0.5.2, which should be pretty soon
Former Deluge Developer
tarka
Member
Member
Posts: 22
Joined: Sat Jun 09, 2007 6:49 am

Re: Proposal: Plugins should be modules

Post by tarka »

Cool. I'll make the changes as part of the filtering plugin I'm working on and post it.
tarka
Member
Member
Posts: 22
Joined: Sat Jun 09, 2007 6:49 am

Re: Proposal: Plugins should be modules

Post by tarka »

Patch attached to this ticket: http://dev.deluge-torrent.org/ticket/315
markybob
Compulsive Poster
Compulsive Poster
Posts: 1230
Joined: Thu May 24, 2007 11:27 pm
Location: Chicago, IL, USA
Contact:

Re: Proposal: Plugins should be modules

Post by markybob »

tarka wrote:Patch attached to this ticket: http://dev.deluge-torrent.org/ticket/315
i tried to merge all of the patches in 315 with svn and things didnt look very great.
when trying to load a .gz list:

Code: Select all

reader = PGReader(self.blockfile)
   File "/usr/share/deluge/plugins/BlocklistImport/peerguardian.py", line 21, in __init__
     hdr = unpack("l", buf)[0]
struct.error: unpack str size does not match format
terminate called after throwing an instance of 'asio::system_error'
  what():  Transport endpoint is not connected
Aborted
most plugins also broke, since they call common. ie:

Code: Select all

  File "/usr/lib/python2.4/site-packages/deluge/plugins.py", line 88, in update_active_plugins
    plugin.update()
  File "/usr/share/deluge/plugins/NetworkGraph/plugin.py", line 114, in update
    import common
ImportError: No module named common
i need to revert this in svn for now. please see if you can re-sync to the current trunk and testing the plugins. thanks
shirish
Seeder
Seeder
Posts: 163
Joined: Fri May 25, 2007 4:01 am
Location: South Asia, India

Re: Proposal: Plugins should be modules

Post by shirish »

No developer here, but please see http://dev.deluge-torrent.org/changeset/567 that was when markybob tried to do the new plugin system & it didn't work.
Debian Sid, Intel Dual-Core, 512 kbps DL , 64 Kbps UL, deluge-1.3.5, pieces plugin, GNOME 3.4.x

All posts under creative commons http://creativecommons.org/licenses/by-nc/3.0/
markybob
Compulsive Poster
Compulsive Poster
Posts: 1230
Joined: Thu May 24, 2007 11:27 pm
Location: Chicago, IL, USA
Contact:

Re: Proposal: Plugins should be modules

Post by markybob »

shirish wrote:No developer here, but please see http://dev.deluge-torrent.org/changeset/567 that was when markybob tried to do the new plugin system & it didn't work.
568 if you also want the ip block plugin. just fyi
markybob
Compulsive Poster
Compulsive Poster
Posts: 1230
Joined: Thu May 24, 2007 11:27 pm
Location: Chicago, IL, USA
Contact:

Re: Proposal: Plugins should be modules

Post by markybob »

this is now all doing ok in svn, btw. as of rev 573
Post Reply