|
| 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) |
0 commit comments