Skip to content

Commit fe9f3a6

Browse files
committed
Updates to writing the catalog.
1 parent dbf6222 commit fe9f3a6

File tree

56 files changed

+3109
-108491
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3109
-108491
lines changed

.tools/scanners/catalog_runner.py

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
import logging
77
import os
88
import json
9-
from collections import defaultdict
10-
from difflib import unified_diff
9+
1110
from pathlib import Path
12-
from typing import Optional
11+
from typing import Optional, Dict
12+
from copy import deepcopy
1313

1414
from render import Renderer, MissingMetadataError, RenderStatus
1515
from scanner import Scanner
1616

17+
from aws_doc_sdk_examples_tools.entities import expand_all_entities
1718
from aws_doc_sdk_examples_tools.doc_gen import DocGen, DocGenEncoder
1819

1920
logging.basicConfig(level=os.environ.get("LOGLEVEL", "INFO").upper(), force=True)
@@ -72,7 +73,7 @@ def main():
7273
action="store_true",
7374
dest="dry_run",
7475
help="In dry run, compare current vs generated and exit with failure if they do not match.",
75-
default=False, # Change this to default false when we're ready to use this generally.
76+
default=False,
7677
)
7778
parser.add_argument("--no-dry-run", dest="dry_run", action="store_false")
7879
parser.add_argument("--check", dest="dry_run", action="store_true")
@@ -98,7 +99,6 @@ def main():
9899
written = []
99100
unchanged = []
100101

101-
102102
scanner = prepare_scanner(doc_gen)
103103
if scanner is None:
104104
return -1
@@ -166,15 +166,29 @@ def write_catalog_json(doc_gen, service_name, language_name, folder_path, is_dry
166166
language_examples = []
167167
for example in doc_gen.examples.values():
168168
for lang_name, language in example.languages.items():
169-
for svc_name in example.services:
170-
if svc_name == service_name and lang_name == language_name:
171-
language_examples.append(example)
169+
if lang_name == language_name and service_name in example.services:
170+
example.title = sanitize_example_title(example, service_name)
171+
# Add to the catalog.
172+
language_examples.append(deepcopy(example))
173+
174+
for example in language_examples:
175+
# Remove the lists that aren't needed.
176+
example.languages = []
177+
example.doc_filenames.sdk_pages = []
178+
example.services = []
172179

173180
new_catalog = json.dumps(
174181
{"examples": language_examples},
175182
cls=DocGenEncoder, indent="\t"
176183
)
177184

185+
# Expand all of the entity text.
186+
[text, errors] = expand_all_entities(new_catalog, doc_gen.entities)
187+
if errors:
188+
print(errors)
189+
return RenderStatus.UNCHANGED
190+
new_catalog = text
191+
178192
# If the file already exists, read it to compare contents.
179193
try:
180194
with open(filepath, "r", encoding="utf-8") as example_meta:
@@ -192,22 +206,16 @@ def write_catalog_json(doc_gen, service_name, language_name, folder_path, is_dry
192206
return RenderStatus.UPDATED
193207

194208

195-
def write_language_json(doc_gen, language_name):
196-
# Test creating a file
197-
filepath = f"example_json/{language_name}_examples_list.json"
198-
filepath = filepath.lower()
199-
print("Writing serialized versions of DocGen to %s", filepath)
209+
def sanitize_example_title(example, service) -> [str, None]:
210+
"""Clean up the text in an example."""
211+
# API examples use the API name.
212+
if example.category == 'Api':
213+
return sorted(example.services[service])[0]
214+
# Basics use a standard title.
215+
if example.category == 'Basics':
216+
return 'Learn the basics'
217+
# Otherwise use the title with the code tags removed.
218+
s = example.title
219+
return s.replace("<code>", "")\
220+
.replace("</code>", "")\
200221

201-
language_examples = []
202-
for example in doc_gen.examples.values():
203-
for lang_name, language in example.languages.items():
204-
if lang_name == language_name:
205-
language_examples.append(example)
206-
207-
with open(filepath, "w") as example_meta:
208-
example_meta.write(
209-
json.dumps(
210-
{"examples": language_examples},
211-
cls=DocGenEncoder, indent="\t"
212-
)
213-
)

0 commit comments

Comments
 (0)