Snakelets run inside a WebApp context (snakeserver.webapp.WebApp). You can get the current WebApp object by calling getWebApp() on the request or the snakelet object itself. You can then use the following methods from the webapp:
| Method | description |
|---|---|
| getName() | a tuple: (short name, full name). The short name is taken from the webapp directory it is in. Full name is specified in the webapp descriptor __init__.py file. |
| getVirtualHost() | a tuple (hostname, portnumber) of the 'virtual host' this webapp is running on. Does not take vhost aliasing in account; if you need the 'real' vhost and port that the request arrived on, use request.getServerName(). |
| isEnabled() | is the webapp enabled? (True or False) |
| setEnabled(enabled) | enable or disable this webapp |
| getFileSystemPath() | the filesystem path where the webapp's files are located |
| getDocRootPath() | the web document root, where the pages, images, etc are located |
| getContext() | webapp context (ContextContainer object). Scope: web app. shared for all users/requests |
| getURLprefix() | the URL prefix that is used for urls inside this webapp (example: "/shop/") This prefix includes the (optional) server-wide prefix. |
| getAssetprefix() | the asset base location as specified in the WebApp's config file. |
| mkUrl(path) mkUrl(path, arg="", params=[], htmlescape=True) |
create a correct URL based on the webapp's url prefix, and automatically html-escapes the resulting string so you can directly use it in your html output. arg (optional) can be given a string that should become the url 'arg'. params (optional) can be given a dict or a sequence that contains the name/values that should become the query params. By setting the optional htmlescape param to False, no escaping will be done (and the resulting string can no longer be directly used in html output because there is a risk of generating invalid html) |
| mkAssetUrl(path) mkAssetUrl(path, htmlescape=True) |
create a correct URL for the static asset (images, files etc) based on the asset base location. It automatically html-escapes the resulting string so you can directly use it in your html output. By setting the optional htmlescape param to False, no escaping will be done (and the resulting string can no longer be directly used in html output because there is a risk of generating invalid html) |
| getSnakelet(urlpattern) | returns the snakelet instance belonging to the given urlpattern (from the webapp init file) |
| getConfigItems() | the dictionary of all user config items |
| getConfigItem(item) | the value of a single user config item |
| addPlugin(plugin) | add a webapp-specific plugin (for instance to do things at session creation time). plugin is your plugin instance.
If a plugin with the same name already exists, a ValueError will be raised.
If your webapp is deployed on multiple vhosts, you should be prepared for this (the webapp's init code will be called multiple times).
For more info, see the section on plugins. |
| serveStaticFile(filename, response, useResponseHeaders=False) | Use the server's internal code to serve a static file to the client.
This is useful in snakelets that return files (such as download controllers).
You can fool around with setting the correct headers, and reading/writing the file
efficiently to the response stream yourself, but it's usually much better to use this function instead.
The response object must not be used prior to this call. You cannot set custom HTTP headers,
but the required ones will be generated for you automatically (such as content-lenth).
The filename must be the absolute path to to file that must be served.
This must be the last action in your snakelet that generates output (the response's output must not be used at all)
The file serve loop is quite efficient and will use the sendfile system call if available.
NOTE: the server's RequestPlugins are not invoked on static files! (partly
due to the efficient stripped down processing code for static files to obtain higher performance)
The useResponseHeaders parameter tells the server if it should add the HTTP headers from the
request object (such as Cache-Control, Content-Disposition and other headers). Defaults to False.
|