Let the OS name a README.md read failure and reject a repeated premises heading

This commit is contained in:
2026-09-25 08:26:17 +02:00
parent 17dd447056
commit e74b52a640
3 changed files with 42 additions and 35 deletions
+13 -10
View File
@@ -87,26 +87,29 @@ def sections(text):
for line in text.splitlines():
fenced ^= line.startswith("```")
if not fenced and line.startswith("## "):
current = line.strip()[3:]
found[current] = []
current = []
found.setdefault(line.strip()[3:], []).append(current)
elif not fenced and line.startswith("# "):
current = None
elif current and line.strip():
found[current].append(line)
return {name: "\n".join(body) for name, body in found.items()}
elif current is not None and line.strip():
current.append(line)
return {name: ["\n".join(body) for body in bodies] for name, bodies in found.items()}
def read_premises():
try:
found = sections(open("README.md", encoding="utf-8").read())
except OSError:
sys.exit(f"no README.md in {os.getcwd()} - no premises.")
except OSError as failure:
sys.exit(f"README.md in {os.getcwd()}: {failure.strerror} - no premises.")
for name in ("Goals", "Audience"):
if name not in found:
bodies = found.get(name, [])
if not bodies:
sys.exit(f"README.md has no ## {name} heading - no premises.")
if not found[name]:
if len(bodies) > 1:
sys.exit(f"README.md has ## {name} twice - no premises.")
if not bodies[0]:
sys.exit(f"README.md has nothing under ## {name} - no premises.")
return f"## Goals\n{found['Goals']}\n\n## Audience\n{found['Audience']}"
return f"## Goals\n{found['Goals'][0]}\n\n## Audience\n{found['Audience'][0]}"
def project():