-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_multilingual_docs.py
More file actions
208 lines (182 loc) · 7.79 KB
/
Copy pathvalidate_multilingual_docs.py
File metadata and controls
208 lines (182 loc) · 7.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/usr/bin/env python
"""Validate EN/FR multilingual docs metadata and assets."""
from __future__ import annotations
import argparse
import re
import sys
from dataclasses import dataclass
from pathlib import Path
import yaml
REQUIRED_KEYS = {"page_id", "locale", "title", "path_segments", "source_hash", "status"}
SUPPORTED_LOCALES = ("en", "fr")
SNIPPET_TOKEN_RE = re.compile(r"\{\{\s*snippet:([a-zA-Z0-9_-]+)\s*\}\}")
PY_BLOCK_RE = re.compile(r"```python\s*\n(.*?)\n```", re.DOTALL)
@dataclass
class DocPage:
file_path: Path
page_id: str
locale: str
title: str
path_segments: list[str]
source_hash: str
status: str
body: str
@property
def route_path(self) -> str:
return f"/{self.locale}/docs/" + "/".join(self.path_segments)
def parse_front_matter(path: Path) -> tuple[dict, str]:
text = path.read_text(encoding="utf-8")
if not text.startswith("---\n"):
raise ValueError(f"{path}: missing YAML front matter start delimiter")
parts = text.split("---\n", 2)
if len(parts) < 3:
raise ValueError(f"{path}: invalid YAML front matter format")
raw_yaml = parts[1]
body = parts[2]
data = yaml.safe_load(raw_yaml) or {}
if not isinstance(data, dict):
raise ValueError(f"{path}: front matter must be a YAML mapping")
return data, body
def load_routes(repo_root: Path, locale: str) -> dict[str, str]:
route_file = repo_root / "docs" / "routing" / f"{locale}.routes.yml"
if not route_file.exists():
raise ValueError(f"missing route manifest: {route_file}")
raw = yaml.safe_load(route_file.read_text(encoding="utf-8")) or {}
if not isinstance(raw, dict):
raise ValueError(f"{route_file}: manifest must be a mapping")
routes: dict[str, str] = {}
for page_id, payload in raw.items():
if isinstance(payload, dict):
path_value = payload.get("path")
else:
path_value = None
if not isinstance(page_id, str) or not isinstance(path_value, str):
raise ValueError(f"{route_file}: entry for {page_id!r} must include string path")
routes[page_id] = path_value
return routes
def load_docs(repo_root: Path, locale: str) -> dict[str, DocPage]:
root = repo_root / "docs" / "content" / locale
if not root.exists():
raise ValueError(f"missing locale folder: {root}")
pages: dict[str, DocPage] = {}
for path in root.rglob("*.md"):
metadata, body = parse_front_matter(path)
missing = REQUIRED_KEYS - metadata.keys()
if missing:
raise ValueError(f"{path}: missing required keys: {sorted(missing)}")
page_id = metadata["page_id"]
locale_value = metadata["locale"]
title = metadata["title"]
path_segments = metadata["path_segments"]
source_hash = metadata["source_hash"]
status = metadata["status"]
if not isinstance(page_id, str) or not page_id:
raise ValueError(f"{path}: page_id must be a non-empty string")
if locale_value != locale:
raise ValueError(f"{path}: locale must be {locale!r}, got {locale_value!r}")
if not isinstance(title, str) or not title:
raise ValueError(f"{path}: title must be a non-empty string")
if not isinstance(path_segments, list) or not path_segments:
raise ValueError(f"{path}: path_segments must be a non-empty list")
if not all(isinstance(segment, str) and segment for segment in path_segments):
raise ValueError(f"{path}: every path_segments entry must be a non-empty string")
if not isinstance(source_hash, str) or not source_hash:
raise ValueError(f"{path}: source_hash must be a non-empty string")
if not isinstance(status, str) or not status:
raise ValueError(f"{path}: status must be a non-empty string")
if page_id in pages:
raise ValueError(f"{path}: duplicate page_id {page_id!r} for locale {locale}")
pages[page_id] = DocPage(
file_path=path,
page_id=page_id,
locale=locale,
title=title,
path_segments=path_segments,
source_hash=source_hash,
status=status,
body=body,
)
return pages
def validate_snippets(repo_root: Path, page: DocPage, errors: list[str]) -> None:
for snippet_id in SNIPPET_TOKEN_RE.findall(page.body):
snippet_file = repo_root / "docs" / "snippets" / snippet_id / f"{page.locale}.code"
if not snippet_file.exists():
errors.append(
f"{page.file_path}: missing snippet variant for {snippet_id!r} locale {page.locale}"
)
if PY_BLOCK_RE.search(page.body):
errors.append(
f"{page.file_path}: inline runnable Python block found; use {{snippet:<id>}} instead"
)
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--strict-freshness", action="store_true")
args = parser.parse_args()
repo_root = Path(__file__).resolve().parents[1]
errors: list[str] = []
warnings: list[str] = []
pages_by_locale: dict[str, dict[str, DocPage]] = {}
routes_by_locale: dict[str, dict[str, str]] = {}
try:
for locale in SUPPORTED_LOCALES:
pages_by_locale[locale] = load_docs(repo_root, locale)
routes_by_locale[locale] = load_routes(repo_root, locale)
except ValueError as exc:
print(f"ERROR: {exc}")
return 1
en_ids = set(pages_by_locale["en"])
fr_ids = set(pages_by_locale["fr"])
missing_in_fr = sorted(en_ids - fr_ids)
missing_in_en = sorted(fr_ids - en_ids)
if missing_in_fr:
errors.append(f"missing French pages for page_id(s): {', '.join(missing_in_fr)}")
if missing_in_en:
errors.append(f"missing English pages for page_id(s): {', '.join(missing_in_en)}")
for locale in SUPPORTED_LOCALES:
locale_pages = pages_by_locale[locale]
locale_routes = routes_by_locale[locale]
route_seen: dict[str, str] = {}
for page_id, page in locale_pages.items():
expected_route = page.route_path
actual_route = locale_routes.get(page_id)
if actual_route is None:
errors.append(f"{locale} route manifest missing page_id {page_id!r}")
elif actual_route != expected_route:
errors.append(
f"{locale} route mismatch for {page_id!r}: expected {expected_route}, got {actual_route}"
)
previous = route_seen.get(expected_route)
if previous:
errors.append(
f"{locale} route collision: {expected_route} used by {previous!r} and {page_id!r}"
)
else:
route_seen[expected_route] = page_id
validate_snippets(repo_root, page, errors)
extra_routes = sorted(set(locale_routes) - set(locale_pages))
for extra in extra_routes:
errors.append(f"{locale} route manifest has unknown page_id {extra!r}")
common_ids = sorted(en_ids & fr_ids)
stale_ids: list[str] = []
for page_id in common_ids:
en_hash = pages_by_locale["en"][page_id].source_hash
fr_hash = pages_by_locale["fr"][page_id].source_hash
if en_hash != fr_hash:
stale_ids.append(page_id)
if stale_ids:
message = "stale translation hash mismatch for page_id(s): " + ", ".join(stale_ids)
if args.strict_freshness:
errors.append(message)
else:
warnings.append(message)
if warnings:
for warning in warnings:
print(f"WARNING: {warning}")
if errors:
for error in errors:
print(f"ERROR: {error}")
return 1
print("Multilingual docs validation passed for locales: en, fr")
return 0
if __name__ == "__main__":
sys.exit(main())