Skip to content

Commit 03737f3

Browse files
committed
🐛 Fix handling optional stable catalog
Problem: - When there are no stable strings, one cannot get them from a dictionary. Solution: - Use `dict.get(key, default)` instead.
1 parent eb8b788 commit 03737f3

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

tools/gen_str_catalog.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/usr/bin/env python3
22

33
import argparse
4-
from functools import partial
54
import itertools
65
import json
76
import re
87
import xml.etree.ElementTree as et
8+
from functools import partial
99

1010

1111
def find_arg_split_pos(s: str, start: int) -> int:
@@ -215,7 +215,7 @@ def write_json(messages, modules, extra_inputs: list[str], filename: str, stable
215215

216216

217217
def read_stable(stable_filenames: list[str]):
218-
stable_catalog = dict()
218+
stable_catalog: dict = dict(messages=[], modules=[])
219219
for filename in stable_filenames:
220220
with open(filename, "r") as f:
221221
stable_catalog.update(json.load(f))
@@ -397,10 +397,12 @@ def main():
397397
args = parse_cmdline()
398398

399399
stable_catalog = read_stable(args.stable_json)
400+
stable_messages = stable_catalog.get("messages", [])
401+
stable_modules = stable_catalog.get("modules", [])
400402
try:
401403
stable_ids = (
402-
{stable_msg_key(msg): msg["id"] for msg in stable_catalog["messages"]},
403-
{m["string"]: m["id"] for m in stable_catalog["modules"]},
404+
{stable_msg_key(msg): msg["id"] for msg in stable_messages},
405+
{m["string"]: m["id"] for m in stable_modules},
404406
)
405407
modules, messages = read_input(args.input, stable_ids)
406408
except Exception as e:
@@ -411,9 +413,7 @@ def main():
411413

412414
stable_output = dict(messages=[], modules=[])
413415
if not args.forget_old_ids:
414-
stable_output = dict(
415-
messages=stable_catalog["messages"], modules=stable_catalog["modules"]
416-
)
416+
stable_output = dict(messages=stable_messages, modules=stable_modules)
417417

418418
if args.json_output is not None:
419419
write_json(messages, modules, args.json_input, args.json_output, stable_output)

0 commit comments

Comments
 (0)