Make Donations

Recent site activity

Tutorials‎ > ‎

Developing and Installing Plugins


This page shows how to develop and install GAEO plugins.

The GAEO Plugin Loader

The newer GAEO project's main.py file should be:

import os
import re
import sys
import wsgiref.handlers

from google.appengine.ext import webapp

import gaeo
from gaeo.dispatch import router

def initRoutes():
    r = router.Router()
    
    #TODO: add routes here

    r.connect('/:controller/:action/:id')

def initPlugins():
    """
    Initialize the installed plugins
    """
    plugins_root = os.path.join(os.path.dirname(__file__), 'plugins')
    if os.path.exists(plugins_root):
        plugins = os.listdir(plugins_root)
        for plugin in plugins:
            if not re.match('^__', plugin):
                exec('from plugins import %s' % plugin)

def main():
    # add the project's directory to the import path list.
    sys.path.append(os.path.dirname(__file__))
    sys.path.append(os.path.join(os.path.dirname(__file__), 'application'))

    # get the gaeo's config (singleton)
    config = gaeo.Config()
    # setup the templates' location
    config.template_dir = os.path.join(
        os.path.dirname(__file__), 'application', 'templates')

    initRoutes()
    # initialize the installed plugins
    initPlugins()

    app = webapp.WSGIApplication([
                (r'.*', gaeo.MainHandler),
            ], debug=True)
    wsgiref.handlers.CGIHandler().run(app)

if __name__ == '__main__':
    main()

The initPlugins() method is added in this version and it will load all plugins from the plugins/ directory. GAEO takes each plugin as a python module so the initialization process should be put in the __init__.py file. For example, if your plugin's name is 'hello', you have create a module under the plugins directory like this:

plugins/
    __init__.py
    hello/
        __init__.py
        ....other files...

While the main.py imports your plugin (module), the __init__.py will automatically be executed.

Patching BaseController

You can create a class like this:

# hello/__init__.py

class HelloPlugin:
    def hello(self):
        self.render('<h1>Hello</h1>')

    def world(self):
        self.render('<h1>World</h1>')

from gaeo.controller import BaseController
BaseController.__bases__ += (HelloPlugin,)

Thus, you can use hello method in your controller. In this example, you can call self.hello() in your controller and it renders '<h1>Hello</h1>' to the client and self.world() renders '<h1>World</h1>'.