Plugin system

Robert Brewer fumanchu at amor.org
Sat Oct 30 18:55:20 CEST 2004


Josiah Carlson wrote:
> Reinhold Birkenfeld <reinhold-birkenfeld-nospam at wolke7.net> wrote:
> > 
> > Irmen de Jong wrote:
> > > Reinhold Birkenfeld wrote:
> > > 
> > >> How would you implement a plugin system (for a web-based 
> application)?
> > > 
> > > This is so general a question that I cannot think of 
> anything concrete to answer.
> > > Please ask again, with more detail about what you 
> want/need/have in mind.
> > 
> > Okay, the question is too open: My focus is on how you 
> would structure
> > the plugin code (directories, files) and how you would write code to
> > load these plugins.
> 
> ...Have a place for plugins
> app/plugins
> 
> ...Load the plugins on startup
> import traceback
> import os
> for fn in os.listdir('plugins'):
>     if fn[:1] != '_' and fn.split('.')[-1] in ('py', 'pyw'):
>         try:
>             globals()[fn] = \
>               __import__('plugins.%s'%fn, globals(), locals(), [fn])
>         except:
>             traceback.print_exc()
> 
> Etcetera.  May not be the best, but it will get the job done.

Right. Working example pulled from a wiki app (junct/__init__.py):

def startup(application):
    application._builtin_rules = [parsers.ParseBoldAndItalic,
                                  parsers.ParseHorizRule,
                                  parsers.TableRule(),
                                  parsers.ListRule(),
                                  parsers.ParseLinks,
##                                  parsers.ParseHighASCII,
                                  parsers.CleanBRs,
                                  parsers.UNCRule,
                                  parsers.FreezeTagRule(),
                                  ]
    application.pluginRules = []
    application.pluginCommands = []
    application.pluginTopicCommands = []
    
    application._post_plugin_rules = [parsers.LineBreakRule(),
                                      parsers.FreezeTagRule(),
                                      parsers.TopicNameRule(),
                                      ]
    
    # Run plugin startups
    plugins = application.config(u'Plugins', default={})
    for plugin, args in plugins.items():
        pluginModule = xray.modules(plugin)
        args = tuple([x.strip() for x in args.split(u',') if x.strip()])
        try:
            startFunc = pluginModule.startup
        except AttributeError:
            pass
        else:
            startFunc(application, *args)


Etcetera. 'xray' uses __import__ like Peter and Josiah mentioned.

Here's the startup function from one of the plugins:


def startup(application):
    application.pluginRules.append(DateRule())
    p = (u'    <a href="%(AbsoluteURLRoot)s/calendar.htm" title='
         u'"Show My Calendar (current week)">Calendar</a>')
    application.pluginCommands.append(p)
    disp = application.config(u'Dispatches')
    disp.update({u'calendar': u'junct.calendar.week',
                 u'month': u'junct.calendar.month',
                 u'newdate': u'junct.calendar.newdate',
                 u'makerecur': u'junct.calendar.makerecur',
                 u'recur': u'junct.calendar.showrecur',
                 u'week': u'junct.calendar.week',
                 })


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list