Skip to content

Commit 879eb5f

Browse files
authored
Add yaml writer to update doc_gen metadata. (#161)
1 parent 8e3321f commit 879eb5f

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
from collections import defaultdict
2+
from dataclasses import asdict
3+
from pathlib import Path
4+
from typing import Any, DefaultDict, Dict, List
5+
import yaml
6+
7+
from aws_doc_sdk_examples_tools.doc_gen import DocGen
8+
from aws_doc_sdk_examples_tools.metadata import Example
9+
10+
11+
def write_many(root: Path, to_write: Dict[str, str]):
12+
for path, examples in to_write.items():
13+
with open(root / path, "w") as file:
14+
file.write(examples)
15+
16+
17+
def dump_yaml(value: Any) -> str:
18+
repr: str = yaml.dump(value, sort_keys=False, width=float("inf"))
19+
repr = repr.replace(r"!!set {}", r"{}")
20+
return repr
21+
22+
23+
def prepare_write(examples: Dict[str, Example]) -> Dict[str, str]:
24+
reindexed: DefaultDict[Path, Dict[str, Any]] = defaultdict(dict)
25+
26+
for id, example in examples.items():
27+
if example.file:
28+
reindexed[example.file][id] = example_dict(asdict(example))
29+
30+
to_write = {str(path): dump_yaml(examples) for path, examples in reindexed.items()}
31+
32+
return to_write
33+
34+
35+
EXAMPLE_FIELD_ORDER = [
36+
# "id", # do not include ID, it goes in the key
37+
# "file", # similarly, do not include the file, it's the path to write to later
38+
"title",
39+
"title_abbrev",
40+
"synopsis",
41+
"synopsis_list",
42+
"guide_topic",
43+
# "doc_filenames", # These are currently generated, and don't need to be stored.
44+
"source_key",
45+
"category",
46+
"languages",
47+
"service_main",
48+
"services",
49+
]
50+
51+
VERSION_FIELD_ORDER = [
52+
"sdk_version",
53+
"block_content",
54+
"github",
55+
"sdkguide",
56+
"more_info",
57+
"owner",
58+
"authors",
59+
"source",
60+
"excerpts",
61+
]
62+
63+
EXCERPT_FIELD_ORDER = [
64+
"description",
65+
"genai",
66+
"snippet_tags",
67+
"snippet_files",
68+
]
69+
70+
71+
def reorder_dict(order: List[str], dict: Dict) -> Dict:
72+
replaced = {}
73+
74+
for field in order:
75+
if value := dict[field]:
76+
replaced[field] = value
77+
78+
return replaced
79+
80+
81+
def example_dict(example: Dict) -> Dict:
82+
replaced = reorder_dict(EXAMPLE_FIELD_ORDER, example)
83+
84+
replaced["languages"] = {
85+
k: dict(versions=[version_dict(version) for version in v["versions"]])
86+
for k, v in replaced["languages"].items()
87+
}
88+
89+
return replaced
90+
91+
92+
def version_dict(version: Dict) -> Dict:
93+
replaced = reorder_dict(VERSION_FIELD_ORDER, version)
94+
95+
replaced["excerpts"] = [excerpt_dict(excerpt) for excerpt in replaced["excerpts"]]
96+
97+
return replaced
98+
99+
100+
def excerpt_dict(excerpt: Dict) -> Dict:
101+
reordered = reorder_dict(EXCERPT_FIELD_ORDER, excerpt)
102+
if reordered.get("genai") == "none":
103+
del reordered["genai"]
104+
return reordered
105+
106+
107+
# For testing
108+
if __name__ == "__main__":
109+
doc_gen = DocGen.from_root(
110+
Path(__file__).parent / "test_resources" / "doc_gen_test"
111+
)
112+
writes = prepare_write(doc_gen.examples)
113+
write_many(Path("/"), writes)
114+
# print(writes)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from pathlib import Path
2+
import pytest
3+
4+
from aws_doc_sdk_examples_tools.doc_gen import DocGen
5+
from aws_doc_sdk_examples_tools.yaml_writer import prepare_write
6+
7+
8+
ROOT = Path(__file__).parent / "test_resources" / "doc_gen_test"
9+
10+
11+
@pytest.fixture
12+
def sample_doc_gen():
13+
return DocGen.from_root(ROOT)
14+
15+
16+
def test_doc_gen(sample_doc_gen: DocGen):
17+
del sample_doc_gen.examples["sns_EntityFailures"]
18+
writes = prepare_write(sample_doc_gen.examples)
19+
assert writes
20+
21+
writes = {k.replace(str(ROOT) + "/", ""): v for k, v in writes.items()}
22+
23+
expected_writes = {
24+
".doc_gen/metadata/aws_entity_metadata.yaml": """sns_EntitySuccesses:
25+
title: Title has &AWS; using an &AWS; SDK
26+
title_abbrev: Title Abbrev has &AWS; in it
27+
synopsis: this <programlisting>Synopsis programlisting has AWS in it.</programlisting>.
28+
synopsis_list:
29+
- Synopsis list code has <code>AWS</code> in it.
30+
category: Cat
31+
languages:
32+
Java:
33+
versions:
34+
- sdk_version: 1
35+
github: java/example_code/svc_EntityFailures
36+
excerpts:
37+
- description: This <emphasis><programlisting>Description programlisting has AWS in it</programlisting></emphasis> doesn't it.
38+
snippet_tags:
39+
- java.example_code.svc_EntityFailures.Test
40+
services:
41+
sns: {}
42+
"""
43+
}
44+
45+
assert writes == expected_writes

0 commit comments

Comments
 (0)