hook.web

class immp.hook.web.WebContext(hook, prefix, module, path=None, env=None)

Bases: object

Abstraction from aiohttp to provide routing and templating to other hooks.

hook

.WebHook – Parent hook instance providing the webserver.

prefix

str – URL prefix acting as the base path.

module

str – Dotted module name of the Python module using this context (__name__).

path

str – Base path of the module (os.path.dirname(__file__)), needed for static routes.

env

dict – Additional variables to make available in the Jinja context. The default context is:

route(method, route, fn, template=None, name=None)

Add a new route to the webserver.

Parameters
  • method (str) – HTTP verb for the route (GET, POST etc.).
  • route (str) – URL pattern to match.
  • fn (function) – Callable to render the response, accepting a aiohttp.web.Request argument.
  • template (str) – Optional template path, relative to the module path. If specified, the view callable should return a context dict which is passed to the template.
  • name (str) – Custom name for the route, defaulting to the function name if not specified.
static(route, path, name=None)

Add a new route to the webserver.

Parameters
  • route (str) – URL pattern to match.
  • path (str) – Filesystem location relative to the base path.
  • name (str) – Custom name for the route, defaulting to the function name if not specified.
url_for(name_, **kwargs)

Generate an absolute URL for the named route.

Parameters
name (str) – Route name, either the function name or the custom name given during registration.
Returns
Relative URL to the corresponding page.
Return type
str
class immp.hook.web.WebHook(name, config, host)

Bases: immp.core.hook.ResourceHook

Hook that provides a generic webserver, which other hooks can bind routes to.

app

aiohttp.web.Application – Web application instance, used to add new routes.

context(prefix, module, path=None, env=None)

Retrieve a context for the current module.

Parameters
  • prefix (str) – URL prefix acting as the base path.
  • module (str) – Dotted module name of the Python module using this context. Callers should use __name__ from the root of their module.
  • path (str) – Base path of the module, needed for static routes. Callers should use os.path.dirname(__file__) from the root of their module.
  • env (dict) – Additional variables to make available in the Jinja context. See WebContext.env for details.
Returns

Linked context instance for that module.

Return type

WebContext

add_loader(module)

Register a Jinja2 package loader for the given module.

Parameters
module (str) – Module name to register.
add_route(*args, **kwargs)

Equivalent to aiohttp.web.UrlDispatcher.add_route().

add_static(*args, **kwargs)

Equivalent to aiohttp.web.UrlDispatcher.add_static().