forked from flypythoncom/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_manifest.py
More file actions
110 lines (94 loc) · 3.84 KB
/
Copy pathcontent_manifest.py
File metadata and controls
110 lines (94 loc) · 3.84 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
"""Load and validate bilingual first-party content for the public manifest."""
from __future__ import annotations
import hashlib
import json
from pathlib import Path
from typing import Any
import yaml
CONTENT_GLOBS = ("guides/**/*.md", "playbooks/**/*.md", "examples/**/*.md", "courses/**/*.md", "paths/**/*.md")
REQUIRED = {
"id",
"type",
"title",
"summary",
"lang",
"content_version",
"status",
"reviewed_on",
}
LANGUAGES = {"en-US", "zh-CN"}
TYPES = {"guide", "playbook", "example", "course", "path"}
class ContentManifestError(ValueError):
"""Raised when first-party content violates the manifest contract."""
def _metadata(path: Path) -> dict[str, Any] | None:
text = path.read_text(encoding="utf-8")
if not text.startswith("---\n"):
return None
parts = text.split("---\n", maxsplit=2)
if len(parts) != 3:
raise ContentManifestError(f"{path}: malformed YAML front matter")
data = yaml.safe_load(parts[1])
if not isinstance(data, dict):
raise ContentManifestError(f"{path}: front matter must be an object")
return data
def build_manifest(root: Path) -> dict[str, Any]:
records: dict[str, list[dict[str, Any]]] = {}
errors: list[str] = []
paths = sorted({path for pattern in CONTENT_GLOBS for path in root.glob(pattern)})
for path in paths:
metadata = _metadata(path)
if metadata is None:
continue
missing = sorted(REQUIRED - metadata.keys())
if missing:
errors.append(f"{path.relative_to(root)}: missing {', '.join(missing)}")
continue
if metadata["lang"] not in LANGUAGES:
errors.append(f"{path.relative_to(root)}: unsupported lang")
if metadata["type"] not in TYPES:
errors.append(f"{path.relative_to(root)}: unsupported type")
if metadata["status"] != "reviewed":
errors.append(f"{path.relative_to(root)}: public content must be reviewed")
relative = path.relative_to(root).as_posix()
records.setdefault(str(metadata["id"]), []).append(
{
"lang": metadata["lang"],
"path": relative,
"title": metadata["title"],
"summary": metadata["summary"],
"sha256": hashlib.sha256(path.read_bytes()).hexdigest(),
"metadata": metadata,
}
)
documents: list[dict[str, Any]] = []
for content_id, locales in sorted(records.items()):
languages = {locale["lang"] for locale in locales}
if languages != LANGUAGES or len(locales) != 2:
errors.append(f"{content_id}: expected exactly en-US and zh-CN")
continue
versions = {locale["metadata"]["content_version"] for locale in locales}
types = {locale["metadata"]["type"] for locale in locales}
reviewed_dates = {
str(locale["metadata"]["reviewed_on"]) for locale in locales
}
if len(versions) != 1 or len(types) != 1 or len(reviewed_dates) != 1:
errors.append(f"{content_id}: paired metadata does not match")
continue
documents.append(
{
"id": content_id,
"type": types.pop(),
"content_version": versions.pop(),
"status": "reviewed",
"reviewed_on": reviewed_dates.pop(),
"locales": [
{key: locale[key] for key in ("lang", "path", "title", "summary", "sha256")}
for locale in sorted(locales, key=lambda item: item["lang"])
],
}
)
if errors:
raise ContentManifestError("\n".join(errors))
return {"schema_version": 1, "documents": documents}
def dump_manifest(manifest: dict[str, Any]) -> str:
return json.dumps(manifest, ensure_ascii=False, indent=2, sort_keys=False) + "\n"