|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# A script to generate markdown documentation from table schemas. |
| 4 | + |
| 5 | +from table2md import MarkdownTable |
| 6 | +import yaml |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +_DOCS_DIR = Path(__file__).parent |
| 10 | +_SCHEMA_DIR = _DOCS_DIR.parent / "schemas" / "input" |
| 11 | +_FILE_ORDER = { |
| 12 | + "Time slices": ["time_slices"], |
| 13 | + "Regions": ["regions"], |
| 14 | + "Agents": ["agents", "agent_*"], |
| 15 | + "Assets": ["assets"], |
| 16 | + "Commodities": ["commodities", "commodity_costs", "demand", "demand_slicing"], |
| 17 | + "Processes": ["processes", "process_*"], |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +def generate_markdown() -> str: |
| 22 | + out = ( |
| 23 | + "# Input file format\n" |
| 24 | + f"<!-- Automatically generated by {Path(__file__).name}. Do not edit manually. -->\n" |
| 25 | + "<!-- markdownlint-disable MD013 -->\n" |
| 26 | + "<!-- markdownlint-disable MD033 -->\n" |
| 27 | + ) |
| 28 | + |
| 29 | + for title, patterns in _FILE_ORDER.items(): |
| 30 | + out += f"\n## {title}\n" |
| 31 | + |
| 32 | + for pattern in patterns: |
| 33 | + paths = map(str, _SCHEMA_DIR.glob(f"{pattern}.yaml")) |
| 34 | + for path in map(Path, sorted(paths)): |
| 35 | + out += process_file(path) |
| 36 | + |
| 37 | + return out |
| 38 | + |
| 39 | + |
| 40 | +def process_file(path: Path) -> str: |
| 41 | + out = f"\n### `{path.stem}.csv`\n\n" |
| 42 | + with path.open() as f: |
| 43 | + data = yaml.safe_load(f) |
| 44 | + |
| 45 | + out += f"{add_full_stop(data['title'])}\n\n" |
| 46 | + |
| 47 | + try: |
| 48 | + table_str, notes_str = fields2table(data["fields"]) |
| 49 | + out += table_str |
| 50 | + except KeyError: |
| 51 | + print(f"MISSING VALUE IN {path}") |
| 52 | + raise |
| 53 | + |
| 54 | + desc = data.get("description", "") |
| 55 | + if not desc and not notes_str: |
| 56 | + return out |
| 57 | + |
| 58 | + out += "\n#### Notes\n\n" |
| 59 | + |
| 60 | + if desc: |
| 61 | + out += f"{add_full_stop(desc)}\n\n" |
| 62 | + |
| 63 | + if notes_str: |
| 64 | + out += notes_str |
| 65 | + |
| 66 | + return out |
| 67 | + |
| 68 | + |
| 69 | +def add_full_stop(s: str) -> str: |
| 70 | + s = s.rstrip() |
| 71 | + if s == "" or s.endswith("."): |
| 72 | + return s |
| 73 | + else: |
| 74 | + return f"{s}." |
| 75 | + |
| 76 | + |
| 77 | +def fields2table(fields: list[dict[str, str]]) -> tuple[str, str]: |
| 78 | + data = [] |
| 79 | + notes = [] |
| 80 | + for f in fields: |
| 81 | + row = {"Field": f"`{f['name']}`", "Description": f["title"]} |
| 82 | + data.append(row) |
| 83 | + |
| 84 | + if desc := f.get("description", ""): |
| 85 | + # MarkdownTable can't handle newlines, so replace with HTML equivalent |
| 86 | + desc = desc.replace("\n\n", "<br /><br />").replace("\n", " ") |
| 87 | + row = {"Field": f"`{f['name']}`", "Notes": desc} |
| 88 | + notes.append(row) |
| 89 | + |
| 90 | + data = [ |
| 91 | + { |
| 92 | + "Field": f"`{f['name']}`", |
| 93 | + "Description": f["title"], |
| 94 | + } |
| 95 | + for f in fields |
| 96 | + ] |
| 97 | + |
| 98 | + table_str = str(MarkdownTable.from_dicts(data)) |
| 99 | + notes_str = str(MarkdownTable.from_dicts(notes)) if notes else "" |
| 100 | + return table_str, notes_str |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + output_path = _DOCS_DIR / "input_format.md" |
| 105 | + output_path.write_text(generate_markdown(), encoding="utf-8") |
0 commit comments