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
- env¶
Additional variables to make available in the Jinja context. The default context is:
immp
, the top-levelimmp
modulehost
, the runningHost
instancectx
, thisWebContext
instancerequest
, the currentaiohttp.web.Request
instance
- Type:
- 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.
- 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:
- 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.