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#

Parent hook instance providing the webserver.

Type:

.WebHook

prefix#

URL prefix acting as the base path.

Type:

str

module#

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

Type:

str

path#

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

Type:

str

env#

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

Type:

dict

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: ResourceHook

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

app#

Web application instance, used to add new routes.

Type:

aiohttp.web.Application

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().

async start()#

Perform any underlying operations needed to ready this resource for use, such as opening connections to an external network or API.

If using an event-driven framework that yields and runs in the background, you should use a signal of some form (e.g. asyncio.Condition) to block this method until the framework is ready for use.

async stop()#

Perform any underlying operations needed to stop using this resource and tidy up, such as terminating open network connections.

Like with start(), this should block as needed to wait for other frameworks – this method should return only when ready to be started again.