|
| 1 | +import os |
| 2 | +from io import BytesIO |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | +import notion_client as n_client |
| 6 | +import frontmatter |
| 7 | +from ruamel.yaml import YAML |
| 8 | +from frontmatter.default_handlers import YAMLHandler, DEFAULT_POST_TEMPLATE |
| 9 | +from notion.catalog import PersistenceCatalog |
| 10 | + |
| 11 | +token = os.getenv("NOTION_TOKEN") |
| 12 | +markdown_path = "../../src/content/docs/aws/services" |
| 13 | +persistence_path = "../../src/data/persistence" |
| 14 | +persistence_data = os.path.join(persistence_path, "coverage.json") |
| 15 | + |
| 16 | + |
| 17 | +class CustomYAMLHandler(YAMLHandler): |
| 18 | + |
| 19 | + def load(self, fm: str, **kwargs: object): |
| 20 | + yaml = YAML() |
| 21 | + yaml.default_flow_style = False |
| 22 | + yaml.preserve_quotes = True |
| 23 | + return yaml.load(fm, **kwargs) # type: ignore[arg-type] |
| 24 | + |
| 25 | + def export(self, metadata: dict[str, object], **kwargs: object) -> str: |
| 26 | + yaml = YAML() |
| 27 | + yaml.default_flow_style = False |
| 28 | + from io import StringIO |
| 29 | + stream = StringIO() |
| 30 | + yaml.dump(metadata, stream) |
| 31 | + return stream.getvalue().rstrip() |
| 32 | + |
| 33 | + def format(self, post, **kwargs): |
| 34 | + """ |
| 35 | + Simple customization to avoid removing the last line. |
| 36 | + """ |
| 37 | + start_delimiter = kwargs.pop("start_delimiter", self.START_DELIMITER) |
| 38 | + end_delimiter = kwargs.pop("end_delimiter", self.END_DELIMITER) |
| 39 | + |
| 40 | + metadata = self.export(post.metadata, **kwargs) |
| 41 | + |
| 42 | + return DEFAULT_POST_TEMPLATE.format( |
| 43 | + metadata=metadata, |
| 44 | + content=post.content, |
| 45 | + start_delimiter=start_delimiter, |
| 46 | + end_delimiter=end_delimiter, |
| 47 | + ).lstrip() |
| 48 | + |
| 49 | + |
| 50 | +def lookup_full_name(shortname: str) -> str: |
| 51 | + """Given the short default name of a service, looks up for the full name""" |
| 52 | + service_lookup = Path("../../src/data/coverage/service_display_name.json") |
| 53 | + service_info = {} |
| 54 | + if service_lookup.exists() and service_lookup.is_file(): |
| 55 | + with open(service_lookup, "r") as f: |
| 56 | + service_info = json.load(f) |
| 57 | + |
| 58 | + service_name_title = shortname |
| 59 | + |
| 60 | + if service_name_details := service_info.get(shortname, {}): |
| 61 | + service_name_title = service_name_details.get("long_name", shortname) |
| 62 | + if service_name_title and (short_name := service_name_details.get("short_name")): |
| 63 | + service_name_title = f"{short_name} ({service_name_title})" |
| 64 | + return service_name_title |
| 65 | + |
| 66 | + |
| 67 | +def collect_status() -> dict: |
| 68 | + """Reads the catalog on Notion and returns the status of persistence for each service""" |
| 69 | + if not token: |
| 70 | + print("Aborting, please provide a NOTION_TOKEN in the env") |
| 71 | + notion_client = n_client.Client(auth=token) |
| 72 | + |
| 73 | + catalog_db = PersistenceCatalog(notion_client=notion_client) |
| 74 | + statuses = {} |
| 75 | + for item in catalog_db: |
| 76 | + # we do not want some services to be mentioned in the docs (for instance, not yet released) |
| 77 | + if item.exclude: |
| 78 | + continue |
| 79 | + |
| 80 | + # Skip entries with empty or placeholder names |
| 81 | + if not item.name or not item.name.strip(): |
| 82 | + continue |
| 83 | + |
| 84 | + # Skip template/placeholder entries |
| 85 | + if item.name.strip().lower() in ['new service page', 'template', 'placeholder']: |
| 86 | + continue |
| 87 | + |
| 88 | + service = item.name.replace('_', '-') |
| 89 | + status = item.status.lower() |
| 90 | + statuses[service] = { |
| 91 | + "service": service, |
| 92 | + "full_name": lookup_full_name(service), |
| 93 | + "support": status, |
| 94 | + "test_suite": item.has_test or False, |
| 95 | + # we collect limitations notes only for the services explicitly marked with limitations |
| 96 | + "limitations": item.limitations if "limit" in status else "" |
| 97 | + } |
| 98 | + statuses = dict(sorted(statuses.items())) |
| 99 | + |
| 100 | + # save the data |
| 101 | + if not os.path.exists(persistence_path): |
| 102 | + os.mkdir(persistence_path) |
| 103 | + with open(persistence_data, 'w') as f: |
| 104 | + json.dump(statuses, f, indent=2) |
| 105 | + return statuses |
| 106 | + |
| 107 | + |
| 108 | +def update_frontmatter(statuses: dict): |
| 109 | + """Updates the frontmatter of the service page in the user guide Markdown file""" |
| 110 | + for service, values in statuses.items(): |
| 111 | + |
| 112 | + # a bunch of special cases |
| 113 | + if "cognito" in service: |
| 114 | + service = "cognito" |
| 115 | + if service == "kafka": |
| 116 | + service = "msk" |
| 117 | + |
| 118 | + _path = os.path.join(markdown_path, f"{service}.mdx") |
| 119 | + if not os.path.exists(_path): |
| 120 | + continue |
| 121 | + |
| 122 | + support_value = values.get("support") |
| 123 | + is_supported = support_value == "supported" or support_value == "supported with limitations" |
| 124 | + if not is_supported: |
| 125 | + # we don't want to modify the frontmatter for the services not supporting persistence |
| 126 | + continue |
| 127 | + |
| 128 | + # open the markdown file and read the content |
| 129 | + content = frontmatter.load(_path, handler=CustomYAMLHandler()) |
| 130 | + desc = content.metadata["description"] |
| 131 | + content.metadata["description"] = desc.strip() |
| 132 | + content.metadata["persistence"] = values.get("support", "unknown") |
| 133 | + frontmatter.dump(content, _path) |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + data = collect_status() |
| 138 | + update_frontmatter(statuses=data) |
0 commit comments