Contexts

Contexts are a feature of the hook-based extension system in Tutor, which allows us to keep track of which components of the code created which callbacks. Contexts are very much an internal concept that most plugin developers should not have to worry about.

tutor.hooks.contexts.enter(name)

Identify created hooks with one or multiple context strings.

Parameters

name (str) – name of the context that will be attached to hooks.

Rtype t.ContextManager[None]

Usage:

from tutor import hooks

with hooks.contexts.enter("my-context"):
    # declare new actions and filters
    ...

# Later on, actions and filters can be disabled with:
hooks.actions.clear_all(context="my-context")
hooks.filters.clear_all(context="my-context")

This is a context manager that will attach a context name to all hook callbacks created within its scope. The purpose of contexts is to solve an issue that is inherent to pluggable hooks: it is difficult to track in which part of the code each hook callback was created. This makes things hard to debug when a single hook callback goes wrong. It also makes it impossible to disable some hook callbacks after they have been created.

We resolve this issue by storing the current contexts in a static list. Whenever a hook is created, the list of current contexts is copied as a contexts attribute. This attribute can be later examined, either for removal or for limiting the set of hook callbacks that should be applied.

class tutor.hooks.contexts.Context(name)
Parameters

name (str) –