flowchart.py

clew/wiki/flowchart.py

A test as a flowchart: each pinned assertion a diamond, each failure ending in a declared mode.

chart

list[Node] clew.wiki.flowchart.chart(list[Any] pins, list[str] lines, int first_line, list[str] modes)

A test's flowchart nodes, in pass-path order.

clew/wiki/flowchart.py:70

Source · 14 lines
def chart(pins: list[Any], lines: list[str], first_line: int, modes: list[str]) -> list[Node]:
    """@brief Derive the chart. @return Nodes."""
    only = modes[0] if len(modes) == 1 else None
    nodes = [Node("start", "start")]
    for pin in pins:
        if pin.target:
            nodes.append(Node("step", f"{pin.target}(…)", pin.bound_line or pin.line))
        index = (pin.line or 0) - first_line
        source = lines[index] if 0 <= index < len(lines) else ""
        nodes.append(Node("check", pin.statement, pin.line, marker(source) or only or UNASSIGNED))
    nodes.append(Node("passed", "passed"))
    return nodes

Activity · 10 steps
lines 72–72only = modes[0] if len(modes) == 1 else Nonelines 73–73nodes = [Node("start", "start")]lines 74–74◇ for pin in pinslines 75–75◇ if pin.targetlines 76–76nodes.append(Node("step", f"{pin.target}(…)", pin.bound_line or pin.line))lines 77–77index = (pin.line or 0) - first_linelines 78–78source = lines[index] if 0 <= index < len(lines) else ""lines 79–79nodes.append(Node("check", pin.statement, pin.line, marker(source) or only or UNASSIGNED))lines 80–80nodes.append(Node("passed", "passed"))lines 81–81return nodes

Requirements

REQ-DDB-WIKI-043

flowchart_html

str clew.wiki.flowchart.flowchart_html(list[Node] nodes, dict[str, str] meanings, int|None observed_line=None)

The flowchart as an inline SVG.

clew/wiki/flowchart.py:92

Source · 41 lines
def flowchart_html(
    nodes: list[Node], meanings: dict[str, str], observed_line: int | None = None
) -> str:
    """A MODE IS DRAWN ONCE, however many checks fail into it, so the right column reads as the
    test's distinct ways to fail. An undeclared id still draws, with its id and no meaning.

    @brief Render the chart.
    @return HTML.
    """
    if not any(n.kind == "check" for n in nodes):
        return ""
    ## EACH MODE BESIDE THE FIRST CHECK THAT FAILS INTO IT, so a fail edge runs across rather
    ## than climbing the chart (found looking at the first render).
    rows_of: dict[str, int] = {}
    for i, n in enumerate(nodes):
        if n.kind == "check":
            rows_of.setdefault(str(n.fails_into), i)
    ## F3: THE BRANCH A RECORDED RUN TOOK — the check on the failing line, and its mode.
    hit = next(
        (i for i, n in enumerate(nodes) if n.kind == "check" and n.line == observed_line), None
    )
    hit_mode = str(nodes[hit].fails_into) if hit is not None else None
    parts = [_edges(nodes, rows_of, hit)]
    parts += [_node(i, n, i == hit) for i, n in enumerate(nodes)]
    parts += [_mode(row, m, meanings, m == hit_mode) for m, row in rows_of.items()]
    height = len(nodes) * _ROW + 20
    observed = f' data-observed="{hit}"' if hit is not None else ""
    note = (
        f"<figcaption>The recorded run failed at line {observed_line}, into {escape(hit_mode)}."
        "</figcaption>"
        if hit is not None and hit_mode
        else ""
    )
    return (
        f'<figure class="flowchart"{observed}>'
        '<button type="button" class="fc-play">play</button>'
        f'<svg viewBox="0 0 {_MX + _W // 2 + 20} {height}" '
        f'role="img" aria-label="test flowchart">{"".join(parts)}</svg>{note}{_PLAYER}</figure>'
    )

Activity · 15 steps
lines 101–101◇ if not any(n.kind == "check" for n in nodes)lines 102–102return ""lines 105–105rows_of: dict[str, int] = {}lines 106–106◇ for i, n in enumerate(nodes)lines 107–107◇ if n.kind == "check"lines 108–108rows_of.setdefault(str(n.fails_into), i)lines 110–112hit = next( (i for i, n in enumerate(nodes) if n.kind == "check" and n.line == observed_line), None )lines 113–113hit_mode = str(nodes[hit].fails_into) if hit is not None else Nonelines 114–114parts = [_edges(nodes, rows_of, hit)]lines 115–115parts += [_node(i, n, i == hit) for i, n in enumerate(nodes)]lines 116–116parts += [_mode(row, m, meanings, m == hit_mode) for m, row in rows_of.items()]lines 117–117height = len(nodes) * _ROW + 20lines 118–118observed = f' data-observed="{hit}"' if hit is not None else ""lines 119–124note = ( f"<figcaption>The recorded run failed at line {observed_line}, into {escape(hit_mode)}." "</figcaption>" if hit is not None and hit_mode else "" )lines 125–130return ( f'<figure class="flowchart"{observed}>' '<button type="button" class="fc-play">play</button>' f'<svg viewBox="0 0 {_MX + _W // 2 + 20} {height}" ' f'role="img" aria-label="test flowchart">{"".join(parts)}</svg>{note}{_PLAYER}</figure>' )

Requirements

REQ-DDB-WIKI-043

flowchart_section

str clew.wiki.flowchart.flowchart_section(list[Any] pins, list[str] lines, int first_line, list[str] modes, dict[str, str] meanings, int|None failed_at=None)

A test's flowchart section, or "" when the test names no failure mode.

clew/wiki/flowchart.py:181

Show on the map →

Called by _definition

Calls all, chart, flowchart_html

Source · 21 lines
def flowchart_section(
    pins: list[Any],
    lines: list[str],
    first_line: int,
    modes: list[str],
    meanings: dict[str, str],
    failed_at: int | None = None,
) -> str:
    """DRAWN ONLY FOR A TEST THAT NAMES A MODE (in its doc or in a marker): on any other test
    every end would read "unassigned", which says nothing and costs a figure on every test page.
    A requirement's page lists its declared modes no test names.

    @brief Flowchart section.
    @return HTML.
    """
    nodes = chart(pins, lines, first_line, modes)
    if all(n.fails_into in (None, UNASSIGNED) for n in nodes):
        return ""
    return "<h2>How it can fail</h2>" + flowchart_html(nodes, meanings, failed_at)

Activity · 4 steps
lines 196–196nodes = chart(pins, lines, first_line, modes)lines 197–197◇ if all(n.fails_into in (None, UNASSIGNED) for n in nodes)lines 198–198return ""lines 199–199return "<h2>How it can fail</h2>" + flowchart_html(nodes, meanings, failed_at)

Requirements

REQ-DDB-WIKI-043

load_modes

tuple[dict[int, list[str]], dict[str, str]] clew.wiki.flowchart.load_modes(Any conn)

The failure modes the index holds: each test's, and every mode's meaning.

clew/wiki/flowchart.py:154

Show on the map →

Called by test_pages

Calls none

Source · 17 lines
def load_modes(conn: Any) -> tuple[dict[int, list[str]], dict[str, str]]:
    """@brief Load modes. @return Pair."""
    tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type = 'table'")}
    meanings = (
        dict(conn.execute("SELECT mode_id, meaning FROM failure_modes"))
        if "failure_modes" in tables
        else {}
    )
    by_test: dict[int, list[str]] = {}
    if "test_failure_modes" in tables:
        for mode, rowid in conn.execute(
            "SELECT mode_id, test_memberdef_rowid FROM test_failure_modes ORDER BY mode_id"
        ):
            by_test.setdefault(int(rowid), []).append(str(mode))
    return by_test, meanings

Activity · 7 steps
lines 156–156tables = {r[0] for r in conn.execute("SELECT name FROM sqlite_master WHERE type = 'table'")}lines 157–161meanings = ( dict(conn.execute("SELECT mode_id, meaning FROM failure_modes")) if "failure_modes" in tables else {} )lines 162–162by_test: dict[int, list[str]] = {}lines 163–163◇ if "test_failure_modes" in tableslines 164–164◇ for mode, rowid in conn.execute(lines 167–167by_test.setdefault(int(rowid), []).append(str(mode))lines 168–168return by_test, meanings

Flows

marker

str | None clew.wiki.flowchart.marker(str text)

The failure-mode id an inline marker on this source line names.

clew/wiki/flowchart.py:56

Show on the map →

Called by _apply_file, _unmarked, chart, check_fails_when_markers, test_a_marker_is_read_in_both_comment_styles

Calls none

Source · 6 lines
def marker(text: str) -> str | None:
    """@brief Marker id. @return Id or None."""
    found = _MARKER.search(text)
    return found.group(1) if found else None

Activity · 2 steps
lines 58–58found = _MARKER.search(text)lines 59–59return found.group(1) if found else None

Flows