adr.py

clew/wiki/adr.py

Architecture decision records (round 3, P3c).

_file_number

str clew.wiki.adr._file_number(str rel)

The leading digits of a file name.

clew/wiki/adr.py:208

Show on the map →

Called by parse_adr

Calls none

Source · 4 lines
def _file_number(rel: str) -> str:
    """@brief Number. @return Digits."""
    found = _NUMBER.match(Path(rel).name)
    return found.group(1) if found else ""

1 more lines, not shown

Activity · 2 steps
lines 210–210found = _NUMBER.match(Path(rel).name)lines 211–211return found.group(1) if found else ""

Flows

_front_matter

dict[str, str] clew.wiki.adr._front_matter(str text)

MADR-style YAML front matter, as a lower-cased mapping.

clew/wiki/adr.py:177

Show on the map →

Called by parse_adr

Calls none

Source · 14 lines
def _front_matter(text: str) -> dict[str, str]:
    """@brief Front matter. @return Mapping."""
    import yaml

    found = _FRONT.match(text)
    if not found:
        return {}
    try:
        data = yaml.safe_load(found.group(1)) or {}
    except yaml.YAMLError:
        return {}
    return {str(k).lower(): str(v) for k, v in data.items()} if isinstance(data, dict) else {}

Activity · 9 steps
lines 179–179import yamllines 181–181found = _FRONT.match(text)lines 182–182◇ if not foundlines 183–183return {}lines 184–184trylines 185–185data = yaml.safe_load(found.group(1)) or {}lines 186–186except yaml.YAMLErrorlines 187–187return {}lines 188–188return {str(k).lower(): str(v) for k, v in data.items()} if isinstance(data, dict) else {}

Flows

_section

str clew.wiki.adr._section(str body, str name)

The first non-empty line under a ## heading.

clew/wiki/adr.py:197

Show on the map →

Called by parse_adr

Calls none

Source · 6 lines
def _section(body: str, name: str) -> str:
    """@brief Section line. @return Text."""
    found = re.search(rf"^#+\s*{name}\s*\n+\s*(\S.*?)\s*$", body, re.M | re.I)
    return found.group(1) if found else ""

Activity · 2 steps
lines 199–199found = re.search(rf"^#+\s*{name}\s*\n+\s*(\S.*?)\s*$", body, re.M | re.I)lines 200–200return found.group(1) if found else ""

Flows

decisions_page

str clew.wiki.adr.decisions_page(list[Decision] decisions, Path source)

Write the Decisions page.

clew/wiki/adr.py:107

Show on the map →

Called by _docs_page

Calls table

Source · 47 lines
def decisions_page(decisions: list[Decision], source: Path) -> str:
    """@brief Write decisions.md. @return Page name."""
    from .htmltable import table

    if not decisions:
        return ""
    rows = [
        [
            escape(d.number),
            f'<a href="{escape(d.href)}">{escape(d.title)}</a>',
            escape(d.status),
            escape(d.date),
            " ".join(
                f'<a class="req-badge" href="requirements.html#{escape(r)}">{escape(r)}</a>'
                for r in d.requirements
            ),
        ]
        for d in decisions
    ]
    body = table(
        ["#", "decision", "status", "date", "requirements"],
        rows,
        numeric=("#",),
        keep=("decision",),
    )
    pages = [str(Path("prose") / Path(d.file).with_suffix("")) for d in decisions]
    lines = [
        "# Decisions",
        "",
        "The repository's architecture decision records: what was decided, its status, and the "
        "requirements each names.",
        "",
        "```{raw} html",
        body,
        "```",
        "",
        "```{toctree}",
        ":hidden:",
        "",
        *pages,
        "```",
        "",
    ]
    (Path(source) / "decisions.md").write_text("\n".join(lines), encoding="utf-8")
    return "decisions"

Activity · 9 steps
lines 109–109from .htmltable import tablelines 111–111◇ if not decisionslines 112–112return ""lines 113–125rows = [ [ escape(d.number), f'<a href="{escape(d.href)}">{escape(d.title)}</a>', escape(d.status), escape(d.date), " ".join( f'<a class="req-badge" href="requirements.html#{escape(r)}">{escape(r)}</a>' for r in d.requirements ), ] for d in decisions ]lines 126–131body = table( ["#", "decision", "status", "date", "requirements"], rows, numeric=("#",), keep=("decision",), )lines 132–132pages = [str(Path("prose") / Path(d.file).with_suffix("")) for d in decisions]lines 133–149lines = [ "# Decisions", "", "The repository's architecture decision records: what was decided, its status, and the " "requirements each names.", "", "```{raw} html", body, "```", "", "```{toctree}", ":hidden:", "", *pages, "```", "", ]lines 150–150(Path(source) / "decisions.md").write_text("\n".join(lines), encoding="utf-8")lines 151–151return "decisions"

Requirements

REQ-DDB-WIKI-031

doc_kind

str clew.wiki.adr.doc_kind(str rel, tuple[tuple[str, str],...] declared)

A document's kind: declared, else agent-facing by name, else "".

clew/wiki/adr.py:160

Show on the map →

Called by _docs_page

Tested by

1 teststest_clew_documents_itself_with_its_own_wiki

Calls none

Source · 8 lines
def doc_kind(rel: str, declared: tuple[tuple[str, str], ...]) -> str:
    """@brief Kind. @return Kind or ""."""
    for glob, kind in declared:
        if fnmatch.fnmatch(rel, glob):
            return kind
    return "agents" if Path(rel).name in _AGENT_DOCS else ""

Activity · 4 steps
lines 162–162◇ for glob, kind in declaredlines 163–163◇ if fnmatch.fnmatch(rel, glob)lines 164–164return kindlines 165–165return "agents" if Path(rel).name in _AGENT_DOCS else ""

Requirements

REQ-DDB-WIKI-031

find_decisions

list[Decision] clew.wiki.adr.find_decisions(Path repo_root, tuple[str,...] globs, tuple[str,...] req_ids)

Every ADR under the configured globs.

clew/wiki/adr.py:87

Source · 14 lines
def find_decisions(
    repo_root: Path, globs: tuple[str, ...], req_ids: tuple[str, ...]
) -> list[Decision]:
    """@brief Find ADRs. @return Decisions."""
    root = Path(repo_root)
    files = sorted({p for g in globs for p in root.glob(g) if p.is_file()})
    return [
        parse_adr(
            p.relative_to(root).as_posix(), p.read_text(encoding="utf-8", errors="replace"), req_ids
        )
        for p in files
    ]

Activity · 3 steps
lines 91–91root = Path(repo_root)lines 92–92files = sorted({p for g in globs for p in root.glob(g) if p.is_file()})lines 93–98return [ parse_adr( p.relative_to(root).as_posix(), p.read_text(encoding="utf-8", errors="replace"), req_ids ) for p in files ]

Requirements

REQ-DDB-WIKI-031

Flows

href

str clew.wiki.adr.Decision.href(self)

The decision's page, relative to the site root.

clew/wiki/adr.py:46

Show on the map →

Called by none

Calls none

Source · 5 lines
    def href(self) -> str:
        """@brief Href. @return Path."""
        return f"prose/{Path(self.file).with_suffix('')}.html"

Activity · 1 steps
lines 48–48return f"prose/{Path(self.file).with_suffix('')}.html"

Requirements

REQ-DDB-WIKI-031

parse_adr

Decision clew.wiki.adr.parse_adr(str rel, str text, tuple[str,...] req_ids=())

Parse one ADR.

clew/wiki/adr.py:58

Source · 22 lines
def parse_adr(rel: str, text: str, req_ids: tuple[str, ...] = ()) -> Decision:
    """FRONT MATTER, THEN AN INLINE LINE, THEN A `## Status` SECTION: the first that says it wins.
    The number is the heading's (`# 3. Title`) or the file name's leading digits.

    @brief Parse.
    @return Decision.
    """
    front = _front_matter(text)
    body = _FRONT.sub("", text, count=1)
    heading = _TITLE.search(body)
    inline = {k.lower(): v for k, v in _INLINE.findall(body)}
    number = (heading.group(1) if heading and heading.group(1) else "") or _file_number(rel)
    return Decision(
        rel,
        number.lstrip("0") or number,
        heading.group(2) if heading else Path(rel).stem,
        front.get("status") or inline.get("status") or _section(body, "Status"),
        front.get("date") or inline.get("date") or "",
        tuple(r for r in req_ids if re.search(rf"(?<![\w-]){re.escape(r)}(?![\w])", text)),
    )

Activity · 6 steps
lines 65–65front = _front_matter(text)lines 66–66body = _FRONT.sub("", text, count=1)lines 67–67heading = _TITLE.search(body)lines 68–68inline = {k.lower(): v for k, v in _INLINE.findall(body)}lines 69–69number = (heading.group(1) if heading and heading.group(1) else "") or _file_number(rel)lines 70–77return Decision( rel, number.lstrip("0") or number, heading.group(2) if heading else Path(rel).stem, front.get("status") or inline.get("status") or _section(body, "Status"), front.get("date") or inline.get("date") or "", tuple(r for r in req_ids if re.search(rf"(?<![\w-]){re.escape(r)}(?![\w])", text)), )

Requirements

REQ-DDB-WIKI-031

Flows