Files
computer-playbook/cli/build/docs/readme/schema.py
T
kevinveenbirkenbachandClaude Opus 5 3d6b3900e0 refactor(cli): gather the documentation generators under cli/build/docs
Two of them lived inside docs/ next to the pages they render, which is why both had grown a __file__ upward walk to find the repository root, a sys.path insert to import from utils, and a nocheck for each. A generator parked among documents reads as documentation and drops out of the checks that apply to code.

Moving them surfaced six real violations that the old location had hidden: yaml.safe_load instead of the shared cache, two uncached read_text calls, five hard-coded role paths instead of ROLE_FILE_META_SERVICES and ROLE_FILE_META_MCP, two make targets without an alias, and a requirement reference in code. All six are fixed at the root, none with a suppression, and both generators lose their project-root exemptions because a package supplies PROJECT_ROOT.

cli/build/readme joins them so the three generators that write into docs/ sit together. A new lint keeps docs/ to .md, .html, .rst and .nocheck; it enumerates through git ls-files so untracked __pycache__ is not its business. Checked against HEAD, it names exactly the three .py files that moved.

026-unified-addon-syntax stated that the matrix generator stays under docs/requirements/. The lint makes that impossible, so the requirement now records where the generator actually lives.

Verified: every one of the twelve modules under the new tree imports, no old module path survives anywhere in the repository, 482 lint tests and 1246 cli unit tests pass, and both generators run without drift.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-08 23:17:55 +02:00

81 lines
2.7 KiB
Python

"""Derive the role-README section schema from the Jinja template.
``templates/roles/README.md.j2.tmpl`` is the single source of truth. The
required sections are the H2s the template emits with no optional context;
the optional sections are the extra H2s that appear once every
``{% if ... %}``-guarded variable is supplied. Rendering the template both
ways and diffing the H2 sets keeps the lint and the generator in lock-step
with the template.
"""
from __future__ import annotations
import functools
import jinja2
from cli.build.docs.readme.sections import h2_titles
from utils.cache.files import PROJECT_ROOT
TEMPLATE_DIR = PROJECT_ROOT / "templates" / "roles"
TEMPLATE_NAME = "README.md.j2.tmpl"
_REQUIRED_CTX: dict = {
"application_id": "web-app-example",
"application_name": "Example",
"application_url": "https://example.com/",
"application_description": "is an example application.",
"application_overview": "This role deploys the example application.",
"application_features": [{"name": "Example", "description": "An example feature."}],
"application_author": "Kevin Veen-Birkenbach",
"cosmos_mermaid": 'flowchart LR\n a["a"] --> b["b"]',
}
_OPTIONAL_CTX: dict = {
"application_invokable": True,
"cosmos_intro": "Example cosmos intro.",
"application_author_url": "https://www.veen.world",
"application_use_cases": "Example use case.",
"application_developer_notes": [{"file": "Administration.md", "description": "notes."}],
"application_further_resources": [{"label": "Example", "url": "https://example.com/"}],
}
def _render(ctx: dict) -> str:
env = jinja2.Environment( # noqa: S701 — renders Markdown, not HTML; escaping would corrupt it
loader=jinja2.FileSystemLoader(str(TEMPLATE_DIR)),
keep_trailing_newline=True,
)
return env.get_template(TEMPLATE_NAME).render(**ctx)
def render_required() -> str:
return _render(dict(_REQUIRED_CTX))
def render_full() -> str:
return _render({**_REQUIRED_CTX, **_OPTIONAL_CTX})
@functools.lru_cache(maxsize=1)
def required_sections() -> tuple[str, ...]:
"""H2 titles the template emits without any optional context (mandatory)."""
return tuple(h2_titles(render_required()))
@functools.lru_cache(maxsize=1)
def canonical_order() -> tuple[str, ...]:
"""Full H2 order (required + optional) as the template lays them out."""
return tuple(h2_titles(render_full()))
@functools.lru_cache(maxsize=1)
def optional_sections() -> tuple[str, ...]:
required = set(required_sections())
return tuple(title for title in canonical_order() if title not in required)
def credits_heading() -> str:
"""The last required section (Credits by convention)."""
return required_sections()[-1]