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:
PLUGINS=list-of-plugin-names This one is required. You must give a list of the names of the plugin classes that are present in the plugin module. The names are the class names and will also be the name under which the plugin is known in the server, if not specified otherwise (see below). ENABLED=True or ENABLED=False to enable/disable the plugin. You may omit this (default=Enabled).PLUGINS list) that inherit from one of the plugin base classes defined in snakeserver.plugin, for example:
from snakeserver.plugin import ServerPlugin class MyServerPlugin(ServerPlugin):
...
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.
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:
plug_init(server) method when the plugin is loaded during startup (the server param is the HTTP server object that loads the plugin). No webapps have been loaded yet.The following events are used in the ServerPlugin:
plug_serverStart(self, server) method (with the http server as argument) when the server is about to start accepting requests. All plugins and webapps have been loaded.plug_serverStop(self, server) method (with the http server as argument) when the server is about to stop. All webapps have already been removed.plug_sessionCreated(self, session, webapp, request) method, when a new http session has just been created.plug_sessionDestroyed(self, session, webapp, request) method, when a http session is going to be destroyed (timeout or explicit).You don't have to implement all of them, you can only provide the one(s) that are of interest for your plugin.
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.
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.
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