Server plugins

Snakelets can be extended with your own server-wide or webapp-specific plugins. They are loaded by the server automatically when it starts up, and are triggered by the server on certain events. Writing a plugin is simple: create a new directory in the "snakeserver/plugins/" directory, with the name of your plugin. Create a package init file (__init__.py) in it that contains the init code of your plugin. It must contain the following:

There is a separate "plugins" project available that contains various useful plugins such as HTTP-compression and a nicer directory lister. See the download page for more information.

Adding plugins from your webapp

You can also add specific plugins for your webapp (the plugins above are server-wide plugins). You can call the addPlugin on the webapp object that you get in the webapp's init method. Create a plugin class like above, and call the webapp.addPlugin(plugin) method with an instance of your plugin class as argument. The plugin you define like this will only be called for your webapp, it is not a server-wide plugin. Its name will be the name of the class prefixed by "webappname/", and it will have default plugin sequence priority.

You have to add several specific plugin-methods to the class, depending on the type of plugin that you are writing. They will be called by the server at specific events. The following events are recognised:

The following events are used in the ServerPlugin:

You don't have to implement all of them, you can only provide the one(s) that are of interest for your plugin.

Specifying the plugin Name and Sequence priority

If you want to give plugins another name or a different sequence priority, define the following class attributes: PLUGIN_NAME (with the desired plugin name, this will then be used instead of the class name) and PLUGIN_SEQ (with the desired sequence priority, see plugin.py for a few of the defined standard priorities). You can also override the __init__ method (don't forget to call the super class's __init__ !) where you then set the attributes self.name and self.sequence.

This works for server-installed plugins and for plugins defined in your webapp.

How to actually call the plugin functions?

You can add your own methods to your plugin class, and call them from the event callback methods described above. You can also give a name to your plugin, and when your snakelet or ypage code wants to use the plugin, it can look it up and then call your own custom methods. From within your snakelets or Ypage source you can use self.getPlugin(pluginname) and self.getPluginNames() to access the plugin(s). Those are Snakelet methods, see Snakelets for more info about them. You can also access the plugins from the web app's init and close functions (in the __init__.py of the webapp), they are called with the webapp argument, do this to get the plugins: plugin = webapp.server.getPlugin("pluginname").

The way a plugin can hook itself into the server is not yet standardized. The server calls various methods of the plugin when certain events occur (the Hollywood priciple; don't call us--we'll call you). If you wish you can access the server's internals trough the arguments that are passed. Use the source, Luke! :-) But when the API of the server changes you have to fix the plugins too. Perhaps later there are more options to hook into the server.

More information

Please read the "plugin.py" source module, and download the Plugins package and see how the plugins there are implemented.

Snakelets manual - Back to index