"""Files written from the declarative specs, so each rule has one source of truth: .github/ISSUE_TEMPLATE/report-a-run.yml from collection/specs/report_form.yaml (report_form.render) the CROSSWALK block of core/graph.js from collection/specs/bot.yaml (Ken's bot uses it at run time) """ import json from .common import path, read_yaml GRAPH_JS = "core/graph.js" BEGIN = " // BEGIN CROSSWALK" END = " // END CROSSWALK\n" def graph_js(): """core/graph.js with its CROSSWALK block rewritten from the bot spec.""" text = path(GRAPH_JS).read_text(encoding="utf-8") a = text.index(BEGIN) a = text.index("\n", a) + 1 b = text.index(END, a) rules = read_yaml("collection/specs/bot.yaml")["crosswalk"] lines = [" const CROSSWALK = ["] for i, (rx, col, val) in enumerate(rules): sep = "," if i < len(rules) - 1 else "" lines.append(f" [/{rx.replace('/', chr(92) + '/')}/, {json.dumps(col)}, {json.dumps(val, ensure_ascii=False)}]{sep}") lines.append(" ];") return text[:a] + "\n".join(lines) + "\n" + text[b:]