diff --git a/.tools/readmes/render.py b/.tools/readmes/render.py index 898c5342cb1..3f48735d93e 100644 --- a/.tools/readmes/render.py +++ b/.tools/readmes/render.py @@ -1,15 +1,16 @@ # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 -from collections import defaultdict import datetime import logging import os import re -from jinja2 import Environment, FileSystemLoader, select_autoescape +from collections import defaultdict from operator import itemgetter from pathlib import Path +from jinja2 import Environment, FileSystemLoader, select_autoescape + import config logger = logging.getLogger(__name__) @@ -47,8 +48,8 @@ def __init__(self, scanner, sdk_ver, safe, svc_folder=None): self.lang_config["service_folder"] = svc_folder else: if ( - "service_folder_overrides" in self.lang_config - and scanner.svc_name in self.lang_config["service_folder_overrides"] + "service_folder_overrides" in self.lang_config + and scanner.svc_name in self.lang_config["service_folder_overrides"] ): overrides = self.lang_config["service_folder_overrides"] self.lang_config["service_folder"] = overrides[scanner.svc_name] @@ -90,9 +91,9 @@ def _transform_sdk(self): def _transform_service(self): pre_svc = self.scanner.service() if ( - "expanded" not in pre_svc - or "blurb" not in pre_svc - or "guide" not in pre_svc + "expanded" not in pre_svc + or "blurb" not in pre_svc + or "guide" not in pre_svc ): logger.error( "%s entry in services.yaml does not have required entity expansions, blurb, or guide " @@ -129,14 +130,14 @@ def _transform_hello(self, pre_hello): post_hello.append(action) return sorted(post_hello, key=itemgetter("title_abbrev")) - def _transform_actions(self, pre_actions): post_actions = [] for pre_id, pre in pre_actions.items(): try: api = next(iter(pre["services"][self.scanner.svc_name])) except: - raise MissingMetadataError(f"Action not found for example {pre_id} and service {self.scanner.svc_name}.") + raise MissingMetadataError( + f"Action not found for example {pre_id} and service {self.scanner.svc_name}.") action = { "title_abbrev": api, "file": self.scanner.snippet( @@ -218,7 +219,7 @@ def _transform_crosses(self): base_folder = f"{config.language[self.scanner.lang_name][self.sdk_ver]['base_folder']}/" if base_folder in github: github = ( - self._lang_level_double_dots() + github.split(base_folder, 1)[1] + self._lang_level_double_dots() + github.split(base_folder, 1)[1] ) cross = { "title_abbrev": pre["title_abbrev"], @@ -247,7 +248,7 @@ def _scrape_customs(self, readme_filename, sdk_short): with open(readme_filename, "r", encoding="utf-8") as readme: for line in readme.readlines(): if line.lstrip().startswith("" + "start-->" ): tag_parts = line.split(".") section = tag_parts[1] @@ -260,7 +261,7 @@ def _scrape_customs(self, readme_filename, sdk_short): else: customs[section] = "" elif line.lstrip().startswith("" + "end-->" ): end_section = line.split(".")[1] if end_section != section: @@ -331,11 +332,13 @@ def write(self): self.readme_filename, f'{self.lang_config["service_folder"]}/{config.saved_readme}', ) - os.remove(self.readme_filename) # Do this so that new files are always updated to the correct case (README.md). + if Path(self.readme_filename).exists(): + os.remove(self.readme_filename) # Do this so that new files are always updated to the correct case (README.md). with open(self.readme_filename, "w", encoding="utf-8") as f: f.write(self.readme_text) print(f"Updated {self.readme_filename}.") + def check(self): with open(self.readme_filename, "r", encoding="utf-8") as f: readme_current = f.read()