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.
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
Flows
_front_matter
dict[str, str] clew.wiki.adr._front_matter(str text)
MADR-style YAML front matter, as a lower-cased mapping.
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
Flows
_section
str clew.wiki.adr._section(str body, str name)
The first non-empty line under a ## heading.
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
Flows
decisions_page
str clew.wiki.adr.decisions_page(list[Decision] decisions, Path source)
Write the Decisions page.
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
Requirements
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 "".
Called by _docs_page
Tested by
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
Requirements
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.
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
Requirements
Flows
href
str clew.wiki.adr.Decision.href(self)
The decision's page, relative to the site root.
Source · 5 lines
def href(self) -> str:
"""@brief Href. @return Path."""
return f"prose/{Path(self.file).with_suffix('')}.html"
Activity · 1 steps
Requirements
parse_adr
Decision clew.wiki.adr.parse_adr(str rel, str text, tuple[str,...] req_ids=())
Parse one ADR.
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)),
)