web.py

clew/wiki/web.py

The repository web: every function in one zoomable, walkable view (round 4, R3).

_index_requirements

list[str] clew.wiki.web._index_requirements(list[dict] nodes, dict facts)

Give each implementing node indices into one sorted table of requirement ids.

clew/wiki/web.py:431

Show on the map →

Called by web_data

Calls none

Source · 16 lines
def _index_requirements(nodes: list[dict], facts: dict) -> list[str]:
    """COMPACT: one id table and small integers per node, rather than the id strings repeated on
    every function that implements them (P5.1's weight budget; clew's own map has ~3,000 nodes).

    @brief Requirement overlay data.
    @return Ids.
    """
    table = sorted({r for p in facts.values() for r in p.get("reqs") or ()})
    at = {r: i for i, r in enumerate(table)}
    for node in nodes:
        reqs = (facts.get((node["name"], node["file"])) or {}).get("reqs")
        if reqs:
            node["r"] = [at[r] for r in reqs]
    return table

Activity · 7 steps
lines 438–438table = sorted({r for p in facts.values() for r in p.get("reqs") or ()})lines 439–439at = {r: i for i, r in enumerate(table)}lines 440–440◇ for node in nodeslines 441–441reqs = (facts.get((node["name"], node["file"])) or {}).get("reqs")lines 442–442◇ if reqslines 443–443node["r"] = [at[r] for r in reqs]lines 444–444return table

Requirements

REQ-DDB-WIKI-022

minimap_html

str clew.wiki.web.minimap_html(dict[str, Any] graph, list[str] order)

The sidebar's labelled block list: the web's blocks, in its order, each a link to it.

clew/wiki/web.py:695

Show on the map →

Called by _build_site

Calls _ordered

Source · 21 lines
def minimap_html(graph: dict[str, Any], order: list[str]) -> str:
    """ROUND 4, R2 (owner: the unlabelled sidebar boxes were "easier to simply click into map"):
    one labelled row per block with its size, ordered as the web draws them, so a block's link
    (`#b=<index>`) lands on that block. ONE STRING FOR EVERY PAGE: a page lights its own blocks
    with a CSS rule on `[data-comp]`, so the markup never varies.

    @brief Labelled minimap.
    @return HTML.
    """
    blocks = _ordered(graph["components"], order)
    if not blocks:
        return ""
    top = max(b.get("functions", 0) for b in blocks) or 1
    rows = "".join(
        f'<a href="@MAP@#b={i}" data-comp="{escape(b["id"])}" title="{escape(b["id"])}">'
        f'<span class="mm-name">{escape(b["id"])}</span>'
        f'<span class="mm-size" style="--w:{max(4, round(100 * b.get("functions", 0) / top))}%">'
        f'</span><span class="mm-n">{b.get("functions", 0)}</span></a>'
        for i, b in enumerate(blocks)
    )
    return f'<nav class="minimap" aria-label="System blocks">{rows}</nav>'

1 more lines, not shown

Activity · 6 steps
lines 704–704blocks = _ordered(graph["components"], order)lines 705–705◇ if not blockslines 706–706return ""lines 707–707top = max(b.get("functions", 0) for b in blocks) or 1lines 708–714rows = "".join( f'<a href="@MAP@#b={i}" data-comp="{escape(b["id"])}" title="{escape(b["id"])}">' f'<span class="mm-name">{escape(b["id"])}</span>' f'<span class="mm-size" style="--w:{max(4, round(100 * b.get("functions", 0) / top))}%">' f'</span><span class="mm-n">{b.get("functions", 0)}</span></a>' for i, b in enumerate(blocks) )lines 715–715return f'<nav class="minimap" aria-label="System blocks">{rows}</nav>'

Requirements

REQ-DDB-WIKI-021

web_data

dict[str, Any] clew.wiki.web.web_data(Path db, dict[str, Any] graph, list[str]|None order=None, int max_ring=MAX_RING, bool owners=True)

The web's data: blocks, functions with positions, edges, block links.

clew/wiki/web.py:65

Show on the map →

Called by _data, web_page

Tested by

2 teststest_each_function_carries_the_requirements_it_implements, test_the_change_lens_exists_only_with_an_impact_report

Calls _block_row, _boundary, _edges, _file_links, _first_owners, _index_requirements, _ordered, _place, _rings, _wedges, connect

Source · 53 lines
def web_data(
    db: Path,
    graph: dict[str, Any],
    order: list[str] | None = None,
    max_ring: int = MAX_RING,
    owners: bool = True,
) -> dict[str, Any]:
    """@brief Compute the web. @return Data."""
    ## THE OWNER'S RULING (round 5): vendor sits OUTSIDE the boundary. First-party blocks share the
    ## whole circle; each vendor area is one node beyond the outer ring, and calls into it end there.
    every = _ordered(graph["components"], order or [])
    blocks = [b for b in every if not b.get("vendor")]
    vendors = [b for b in every if b.get("vendor")]
    index = {b["id"]: i for i, b in enumerate(blocks)}
    owner = graph.get("file_block") or {}
    facts = {(p["name"], p["file"]): p for p in graph.get("symbols") or []}
    with connect(db) as conn:
        all_funcs = _functions(conn, owner)
        all_edges = _edges(conn, all_funcs)
        first_owner = _first_owners(conn) if owners else {}
    funcs = {r: f for r, f in all_funcs.items() if owner[f[1]] in index}
    edges = {(a, b) for a, b in all_edges if a in funcs and b in funcs}
    ring = _rings(funcs, edges, max_ring)
    _wedges(blocks, funcs, owner)
    nodes, slot, files = _place(blocks, index, funcs, owner, ring, facts)
    boundary, crossing = _boundary(vendors, all_funcs, all_edges, owner, nodes, slot, ring)
    ## P5 (2.0.0): THE OWNERS OVERLAY: each node carries its file's first owner, unless the
    ## audience withholds them (`exclude_owners`), in which case no node carries one.
    for node in nodes:
        if node["file"] in first_owner:
            node["owner"] = first_owner[node["file"]]
    req_ids = _index_requirements(nodes, facts)
    node_edges = sorted({(slot[a], slot[b]) for a, b in edges if a in slot and b in slot})
    return {
        "req_ids": req_ids,
        "blocks": [_block_row(b) for b in blocks],
        "nodes": nodes,
        "edges": node_edges,
        ## Owner (2026-11-22): files as slices, and the calls between them, for the zoomed-in view.
        "files": files,
        "file_links": _file_links(nodes, node_edges, files),
        "links": [
            {"from": link["from"], "to": link["to"], "calls": link["calls"]}
            for link in graph["links"]
            if link.get("calls")
        ],
        "max_ring": max(ring.values(), default=0),
        "geometry": {"r0": _R0, "band": _BAND},
        "boundary": boundary,
        "boundary_edges": crossing,
    }

Activity · 22 steps
lines 75–75every = _ordered(graph["components"], order or [])lines 76–76blocks = [b for b in every if not b.get("vendor")]lines 77–77vendors = [b for b in every if b.get("vendor")]lines 78–78index = {b["id"]: i for i, b in enumerate(blocks)}lines 79–79owner = graph.get("file_block") or {}lines 80–80facts = {(p["name"], p["file"]): p for p in graph.get("symbols") or []}lines 81–81with connect(db) as connlines 82–82all_funcs = _functions(conn, owner)lines 83–83all_edges = _edges(conn, all_funcs)lines 84–84first_owner = _first_owners(conn) if owners else {}lines 85–85funcs = {r: f for r, f in all_funcs.items() if owner[f[1]] in index}lines 86–86edges = {(a, b) for a, b in all_edges if a in funcs and b in funcs}lines 87–87ring = _rings(funcs, edges, max_ring)lines 88–88_wedges(blocks, funcs, owner)lines 89–89nodes, slot, files = _place(blocks, index, funcs, owner, ring, facts)lines 90–90boundary, crossing = _boundary(vendors, all_funcs, all_edges, owner, nodes, slot, ring)lines 93–93◇ for node in nodeslines 94–94◇ if node["file"] in first_ownerlines 95–95node["owner"] = first_owner[node["file"]]lines 96–96req_ids = _index_requirements(nodes, facts)lines 97–97node_edges = sorted({(slot[a], slot[b]) for a, b in edges if a in slot and b in slot})lines 98–115return { "req_ids": req_ids, "blocks": [_block_row(b) for b in blocks], "nodes": nodes, "edges": node_edges, ## Owner (2026-11-22): files as slices, and the calls between them, for the zoomed-in view. "files": files, "file_links": _file_links(nodes, node_edges, files), "links": [ {"from": link["from"], "to": link["to"], "calls": link["calls"]} for link in graph["links"] if link.get("calls") ], "max_ring": max(ring.values(), default=0), "geometry": {"r0": _R0, "band": _BAND}, "boundary": boundary, "boundary_edges": crossing, }

Requirements

REQ-DDB-WIKI-019

web_html

str clew.wiki.web.web_html(dict[str, Any] data)

The web's HTML: toolbar, canvas, side panel, inline data, and the block table.

clew/wiki/web.py:497

Source · 43 lines
def web_html(data: dict[str, Any]) -> str:
    """THE DATA IS INLINE (`application/json`, never executed) and the script is local: the page
    works from file://, a Pages site or an ADO wiki with nothing fetched.

    @brief Render.
    @return HTML.
    """
    payload = json.dumps(data, separators=(",", ":")).replace("</", "<\\/")
    flows = "".join(
        f'<option value="{i}">{escape(f["title"])}</option>'
        for i, f in enumerate(data.get("flows") or [])
    )
    picker = (
        f'<label>Play a flow <select data-web-flow><option value="">none</option>{flows}</select>'
        "</label>"
        if flows
        else ""
    )
    return (
        '<div class="web" data-web>'
        '<div class="web-bar"><input type="search" data-web-search placeholder="Find a function" '
        ## C-14 (UX review, owner Q/A 2026-11-22): zoom is one grouped control.
        'aria-label="Find a function"><span class="seg"><button type="button" data-web-zoom="1.6" '
        'aria-label="Zoom in">+</button><button type="button" data-web-zoom="0.625" aria-label="Zoom out">'
        '&minus;</button><button type="button" data-web-fit>Whole system</button></span>'
        '<label>Lens <select data-web-lens><option value="tier">Requirements</option>'
        '<option value="block">Blocks</option>'
        + ('<option value="changed">Change</option>' if _has(data, "changed") else "")
        + ('<option value="owner">Owners</option>' if _has(data, "owner") else "")
        + f"</select></label>{picker}</div>"
        ## Owner map notes (2026-11-22): what each colour means, with its count, for the lens.
        '<div class="web-legend" data-web-legend aria-live="polite"></div>'
        '<div class="web-stage"><canvas class="web-canvas" aria-label="Repository web: blocks '
        'as wedges, functions on rings by call depth"></canvas>'
        '<aside class="web-panel" data-web-panel hidden></aside>'
        '<div class="web-tip" data-web-tip hidden></div></div>'
        f'<script type="application/json" id="web-data">{payload}</script>'
        ## web-files.js (the zoomed-in file layer) BEFORE web.js, which calls it.
        "</div>" + _table(data) + '<script src="_static/web-files.js"></script>'
        '<script src="_static/web.js"></script>'
    )

Activity · 4 steps
lines 504–504payload = json.dumps(data, separators=(",", ":")).replace("</", "<\\/")lines 505–508flows = "".join( f'<option value="{i}">{escape(f["title"])}</option>' for i, f in enumerate(data.get("flows") or []) )lines 509–514picker = ( f'<label>Play a flow <select data-web-flow><option value="">none</option>{flows}</select>' "</label>" if flows else "" )lines 515–537return ( '<div class="web" data-web>' '<div class="web-bar"><input type="search" data-web-search placeholder="Find a function" ' ## C-14 (UX review, owner Q/A 2026-11-22): zoom is one grouped control. 'aria-label="Find a function"><span class="seg"><button type="button" data-web-zoom="1.6" ' 'aria-label="Zoom in">+</button><button type="button" data-web-zoom="0.625" aria-label="Zoom out">' '&minus;</button><button type="button" data-web-fit>Whole system</button></span>' '<label>Lens <select data-web-lens><option value="tier">Requirements</option>' '<option value="block">Blocks</option>' + ('<option value="changed">Change</option>' if _has(data, "changed") else "") + ('<option value="owner">Owners</option>' if _has(data, "owner") else "") + f"</select></label>{picker}</div>" ## Owner map notes (2026-11-22): what each colour means, with its count, for the lens. '<div class="web-legend" data-web-legend aria-live="polite"></div>' '<div class="web-stage"><canvas class="web-canvas" aria-label="Repository web: blocks ' 'as wedges, functions on rings by call depth"></canvas>' '<aside class="web-panel" data-web-panel hidden></aside>' '<div class="web-tip" data-web-tip hidden></div></div>' f'<script type="application/json" id="web-data">{payload}</script>' ## web-files.js (the zoomed-in file layer) BEFORE web.js, which calls it. "</div>" + _table(data) + '<script src="_static/web-files.js"></script>' '<script src="_static/web.js"></script>' )

Requirements

REQ-DDB-WIKI-021

web_page

str clew.wiki.web.web_page(Path db, dict[str, Any] graph, list[str] order, list pages, dict[tuple[str, str], str] hrefs, Path source, int max_ring=MAX_RING, bool owners=True)

Write the map page: the web, with source links and playable flows.

clew/wiki/web.py:623

Show on the map →

Called by _build_site

Calls _briefs, _flow, _with_briefs, connect, web_data, web_html

Source · 45 lines
def web_page(
    db: Path,
    graph: dict[str, Any],
    order: list[str],
    pages: list,
    hrefs: dict[tuple[str, str], str],
    source: Path,
    max_ring: int = MAX_RING,
    owners: bool = True,
) -> str:
    """A FLOW IS PLAYED BY ITS FUNCTIONS IN CALL ORDER: the camera visits each step; a step whose
    function is not drawn (test code, or outside the index) is skipped, never invented.

    @brief Write map.md.
    @return Page name.
    """
    if not graph["components"]:
        return ""
    data = web_data(db, graph, order, max_ring, owners)
    ## X-4 (owner review 2, 2026-11-22): what each function does, at a glance in the pop-out.
    from ..query._common import connect
    from .entities import _briefs

    with connect(db) as conn:
        _with_briefs(data["nodes"], _briefs(conn))
    slot = {(n["name"], n["file"]): i for i, n in enumerate(data["nodes"])}
    for n in data["nodes"]:
        href = hrefs.get((n["name"], n["file"]))
        if href:
            n["href"] = href
    data["flows"] = [f for f in (_flow(p, slot) for p in pages) if len(f["steps"]) > 1]
    lead = (
        "Every function in the repository, in one view. Each wedge is a block of the system; rings "
        "run outward by call depth from the entry points at the centre. Zoom in to see functions, "
        "click one to see what it calls and what calls it, and walk from there; zoom out and the "
        "functions fold back into their blocks. Pick a flow to watch it travel."
    )
    ## THE MAP IS HOME (owner Q/A 2026-11-22): its block is written ONCE and included by both this
    ## page and the home page, so the two can never differ.
    (Path(source) / MAP_BLOCK).write_text(web_html(data), encoding="utf-8")
    text = ["# Map", "", lead, "", "```{raw} html", f":file: {MAP_BLOCK}", "```", ""]
    (Path(source) / "map.md").write_text("\n".join(text), encoding="utf-8")
    return "map"

Activity · 18 steps
lines 639–639◇ if not graph["components"]lines 640–640return ""lines 641–641data = web_data(db, graph, order, max_ring, owners)lines 643–643from ..query._common import connectlines 644–644from .entities import _briefslines 646–646with connect(db) as connlines 647–647_with_briefs(data["nodes"], _briefs(conn))lines 648–648slot = {(n["name"], n["file"]): i for i, n in enumerate(data["nodes"])}lines 649–649◇ for n in data["nodes"]lines 650–650href = hrefs.get((n["name"], n["file"]))lines 651–651◇ if hreflines 652–652n["href"] = hreflines 653–653data["flows"] = [f for f in (_flow(p, slot) for p in pages) if len(f["steps"]) > 1]lines 654–659lead = ( "Every function in the repository, in one view. Each wedge is a block of the system; rings " "run outward by call depth from the entry points at the centre. Zoom in to see functions, " "click one to see what it calls and what calls it, and walk from there; zoom out and the " "functions fold back into their blocks. Pick a flow to watch it travel." )lines 662–662(Path(source) / MAP_BLOCK).write_text(web_html(data), encoding="utf-8")lines 663–663text = ["# Map", "", lead, "", "```{raw} html", f":file: {MAP_BLOCK}", "```", ""]lines 664–664(Path(source) / "map.md").write_text("\n".join(text), encoding="utf-8")lines 665–665return "map"

Requirements

REQ-DDB-WIKI-021