From ff1a34aa6648d5d71a064c7209d9fd18c3e1083e Mon Sep 17 00:00:00 2001 From: David Souther Date: Mon, 30 Sep 2024 21:45:56 +0200 Subject: [PATCH] Attempt to load HEAD static assets. When for_root is called with `none`, attempt to read from `main` in the repo before loading from disk. This will hopefully help streamline rollouts and releases. --- aws_doc_sdk_examples_tools/doc_gen.py | 54 ++++++++++++++++----------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/aws_doc_sdk_examples_tools/doc_gen.py b/aws_doc_sdk_examples_tools/doc_gen.py index 8721ae8..13c1b53 100644 --- a/aws_doc_sdk_examples_tools/doc_gen.py +++ b/aws_doc_sdk_examples_tools/doc_gen.py @@ -3,6 +3,8 @@ import yaml import json +import urllib.request + from collections import defaultdict from dataclasses import dataclass, field, is_dataclass, asdict @@ -36,6 +38,21 @@ class DocGenMergeWarning(MetadataError): pass +def load_meta(path: Optional[Path], name: str): + try: + if path is None: + url = f"https://github.com/awsdocs/aws-doc-sdk-examples-tools/tree/main/aws_doc_sdk_examples_tools/config/{name}" + path = Path(url) + with urllib.request.urlopen(url) as file: + data = file + except Exception: + path = path or Path(__file__).parent / "config" + with path.open(encoding="utf-8") as file: + data = file + meta = yaml.safe_load(data) + return path, meta + + @dataclass class DocGen: root: Path @@ -150,12 +167,10 @@ def clone(self) -> "DocGen": ) def for_root( - self, root: Path, config: Optional[Path] = None, incremental=False + self, root: Path, config_path: Optional[Path] = None, incremental=False ) -> "DocGen": self.root = root - config = config or Path(__file__).parent / "config" - try: with open(root / ".doc_gen" / "validation.yaml", encoding="utf-8") as file: validation = yaml.safe_load(file) @@ -166,33 +181,28 @@ def for_root( pass try: - sdk_path = config / "sdks.yaml" - with sdk_path.open(encoding="utf-8") as file: - meta = yaml.safe_load(file) - sdks, errs = parse_sdks(sdk_path, meta) - self.sdks = sdks - self.errors.extend(errs) + sdk_path, meta = load_meta(config_path, "sdks.yaml") + sdks, errs = parse_sdks(sdk_path, meta) + self.sdks = sdks + self.errors.extend(errs) except Exception: pass try: - services_path = config / "services.yaml" - with services_path.open(encoding="utf-8") as file: - meta = yaml.safe_load(file) - services, service_errors = parse_services(services_path, meta) - self.services = services - for service in self.services.values(): - if service.expanded: - self.entities[service.long] = service.expanded.long - self.entities[service.short] = service.expanded.short - self.errors.extend(service_errors) + services_path, meta = load_meta(config_path, "services.yaml") + services, service_errors = parse_services(services_path, meta) + self.services = services + for service in self.services.values(): + if service.expanded: + self.entities[service.long] = service.expanded.long + self.entities[service.short] = service.expanded.short + self.errors.extend(service_errors) except Exception: pass try: - entities_config_path = config / "entities.yaml" - with entities_config_path.open(encoding="utf-8") as file: - entities_config = yaml.safe_load(file) + _, entities_config = load_meta(config_path, "entities.yaml") + _, meta = load_meta(config_path, "entities.yaml") for entity, expanded in entities_config["expanded_override"].items(): self.entities[entity] = expanded except Exception: