Module

pages.context

Context cascade for filesystem-based page routes.

Runs_context.pyproviders from root to deepest, merging results. Child context overrides parent — like Bengal's cascade_snapshot but for live server requests instead of static site builds.

Functions

build_cascade_context 3 dict[str, Any]
Run context providers from root to leaf, merging results. Each provider's outp…
async
async def build_cascade_context(providers: tuple[ContextProvider, ...], path_params: dict[str, str], service_providers: dict[type, Any] | None = None) -> dict[str, Any]

Run context providers from root to leaf, merging results.

Each provider's output is merged into the accumulated context. Later providers (deeper in the filesystem) override earlier ones.

Providers may raiseHTTPError subclasses (e.g. NotFound) to abort the cascade early. The exception propagates to the caller (page_wrapper), which is wrapped by handle_request — chirp's standard error pipeline renders the appropriate error page automatically. This eliminates the need for downstream handlers to guard against missing resources::

# In _context.py:
from chirp import NotFound

def context(doc_id: str) -> dict:
    doc = store.get(doc_id)
    if doc is None:
        raise NotFound(f"Document {doc_id} not found")
    return {"doc": doc}
Parameters
Name Type Description
providers tuple[ContextProvider, ...]

Context providers ordered from root (depth=0) to leaf.

path_params dict[str, str]

Extracted path parameters from the URL match.

service_providers dict[type, Any] | None

Type-keyed factories fromapp.provide(). Provider params with matching annotations are resolved from these.

Default:None
Returns
dict[str, Any]
_call_provider 4 Any
Call a context provider, injecting path params, parent context, and services. …
def _call_provider(func: Any, path_params: dict[str, str], accumulated_ctx: dict[str, Any], service_providers: dict[type, Any]) -> Any

Call a context provider, injecting path params, parent context, and services.

The provider function's signature determines which arguments it receives:

  • Path params (e.g.name from /skill/{name}) come from the URL.

  • Other params come from the accumulated context of parent providers.

  • Params with type annotations matchingapp.provide()are resolved from service providers::

    def context() -> dict:

    ...  # receives nothing
    

    def context(doc_id: str, store: DocumentStore) -> dict:

    ...  # doc_id from path, store from app.provide()
    
Parameters
Name Type Description
func Any
path_params dict[str, str]
accumulated_ctx dict[str, Any]
service_providers dict[type, Any]
Returns
Any