111 lines
4.1 KiB
Python
111 lines
4.1 KiB
Python
import dataclasses
|
|
import os
|
|
import re
|
|
|
|
from markdown_sections import sections
|
|
|
|
TITLE = "# Comprehension history\n"
|
|
HEADING = re.compile(r"^([^,]+), (.+) at ([0-9a-f]{7,40})(?:, against ([0-9a-f]{7,40}))?$")
|
|
RULING = re.compile(r"^Ruling: (better|same|worse)$", re.M)
|
|
SEAT_PLACES = re.compile(r"^.+, (decided by|hardest first|would restructure first):$")
|
|
# `path:line` or `:line`, then the unit's name where one follows.
|
|
PLACE = re.compile(r"`([^`\s]*):\d+`(?: `(\w+)`)?")
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Place:
|
|
path: str
|
|
name: str | None
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class Entry:
|
|
"""One paired ruling, which names the commit it was against and its ruling, or one scoring run, which names neither."""
|
|
heading: str
|
|
time: str
|
|
commit: str
|
|
against: str | None
|
|
ruling: str | None
|
|
places: list[Place]
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
class ScoringRun:
|
|
heading: str
|
|
commit: str
|
|
places: list[Place]
|
|
|
|
|
|
def entries(history: str) -> list[Entry]:
|
|
"""Each paired ruling and scoring run in a comprehension history, in the order the file holds them; a section whose heading names no commit is left out."""
|
|
found = []
|
|
for heading, bodies in sections(history).items():
|
|
if named := HEADING.match(heading):
|
|
body = "\n".join(bodies)
|
|
ruling = RULING.search(body)
|
|
found.append(Entry(heading, named.group(1), named.group(3), named.group(4), ruling.group(1) if ruling else None, seat_places(body)))
|
|
return found
|
|
|
|
|
|
def scoring_runs(history: str) -> list[ScoringRun]:
|
|
"""Each scoring run, with the places its seats named, as they wrote them; paired rulings name what changed, not what is hard, and are left out."""
|
|
return [ScoringRun(entry.heading, entry.commit, entry.places) for entry in entries(history) if entry.against is None]
|
|
|
|
|
|
def seat_places(body: str) -> list[Place]:
|
|
places = []
|
|
listing = False
|
|
for line in body.splitlines():
|
|
if SEAT_PLACES.match(line):
|
|
listing = True
|
|
elif listing and line.startswith("- "):
|
|
places += bullet_places(line)
|
|
elif not line.startswith(" "):
|
|
listing = False
|
|
return places
|
|
|
|
|
|
def bullet_places(bullet: str) -> list[Place]:
|
|
"""A `:line` with no path is in the file named before it in the bullet."""
|
|
places = []
|
|
path = ""
|
|
for match in PLACE.finditer(bullet):
|
|
path = match.group(1) or path
|
|
if path:
|
|
places.append(Place(path, match.group(2)))
|
|
return places
|
|
|
|
|
|
def resolved(places: list[Place], tracked: list[str]) -> tuple[list[Place], list[Place]]:
|
|
"""Entries written before paths were written from the repository's root shorten a path once they have named its folder: `cycle/run.py` after `bin/loop_claude/cycle/states.py`. Each place resolves to the one tracked file it names, or, where several match, the one in the folder of the place resolved before it; a place naming no file, or several, is dropped. Returns the resolved places and the dropped ones."""
|
|
found = []
|
|
dropped = []
|
|
before = ""
|
|
for place in places:
|
|
candidates = [place.path] if place.path in tracked else [path for path in tracked if path.endswith("/" + place.path)]
|
|
nearest = [path for path in candidates if path.startswith(os.path.dirname(before) + "/")] if len(candidates) > 1 else candidates
|
|
if len(nearest) == 1:
|
|
before = nearest[0]
|
|
found.append(Place(before, place.name))
|
|
else:
|
|
dropped.append(place)
|
|
return found, dropped
|
|
|
|
|
|
def appended(history: str, entry: str) -> str:
|
|
"""The history with entry added last; an empty history gets the file's title first."""
|
|
text = history.rstrip("\n") + "\n" if history.strip() else TITLE
|
|
return f"{text}\n{entry}"
|
|
|
|
|
|
def entry_text(heading: str, lines: list[str], table: list[list[str]], seats: list[tuple[str, list[str]]]) -> str:
|
|
"""An entry in the history's format: the heading, any lines such as `Ruling: same`, a table whose first row is its header, and under each seat's label its places, one bullet each."""
|
|
header, *rows = table
|
|
parts = [f"## {heading}", *lines, "\n".join([row_text(header), "|" + "---|" * len(header), *map(row_text, rows)])]
|
|
parts += [f"{label}\n\n" + "\n".join(f"- {place}" for place in places) for label, places in seats if places]
|
|
return "\n\n".join(parts) + "\n"
|
|
|
|
|
|
def row_text(cells: list[str]) -> str:
|
|
return "| " + " | ".join(cells) + " |"
|