6
6
import logging
7
7
import os
8
8
import json
9
- from collections import defaultdict
10
- from difflib import unified_diff
9
+
11
10
from pathlib import Path
12
- from typing import Optional
11
+ from typing import Optional , Dict
12
+ from copy import deepcopy
13
13
14
14
from render import Renderer , MissingMetadataError , RenderStatus
15
15
from scanner import Scanner
16
16
17
+ from aws_doc_sdk_examples_tools .entities import expand_all_entities
17
18
from aws_doc_sdk_examples_tools .doc_gen import DocGen , DocGenEncoder
18
19
19
20
logging .basicConfig (level = os .environ .get ("LOGLEVEL" , "INFO" ).upper (), force = True )
@@ -72,7 +73,7 @@ def main():
72
73
action = "store_true" ,
73
74
dest = "dry_run" ,
74
75
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 ,
76
77
)
77
78
parser .add_argument ("--no-dry-run" , dest = "dry_run" , action = "store_false" )
78
79
parser .add_argument ("--check" , dest = "dry_run" , action = "store_true" )
@@ -98,7 +99,6 @@ def main():
98
99
written = []
99
100
unchanged = []
100
101
101
-
102
102
scanner = prepare_scanner (doc_gen )
103
103
if scanner is None :
104
104
return - 1
@@ -166,15 +166,29 @@ def write_catalog_json(doc_gen, service_name, language_name, folder_path, is_dry
166
166
language_examples = []
167
167
for example in doc_gen .examples .values ():
168
168
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 = []
172
179
173
180
new_catalog = json .dumps (
174
181
{"examples" : language_examples },
175
182
cls = DocGenEncoder , indent = "\t "
176
183
)
177
184
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
+
178
192
# If the file already exists, read it to compare contents.
179
193
try :
180
194
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
192
206
return RenderStatus .UPDATED
193
207
194
208
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>" , "" )\
200
221
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