add exception handling

This commit is contained in:
starylan 2025-02-12 01:45:03 +08:00
parent f29921d85d
commit ff89cda9b2

View File

@ -37,16 +37,21 @@ def scan_i18n_strings():
strings = [] strings = []
print(" Scanning Files and Extracting i18n Strings ".center(TITLE_LEN, "=")) print(" Scanning Files and Extracting i18n Strings ".center(TITLE_LEN, "="))
for filename in glob.iglob("**/*.py", recursive=True): for filename in glob.iglob("**/*.py", recursive=True):
try:
with open(filename, "r", encoding="utf-8") as f: with open(filename, "r", encoding="utf-8") as f:
code = f.read() code = f.read()
if "I18nAuto" in code: if "I18nAuto" in code:
tree = ast.parse(code) tree = ast.parse(code)
i18n_strings = extract_i18n_strings(tree) i18n_strings = extract_i18n_strings(tree)
print(f"{filename.ljust(30)}: {len(i18n_strings)}") print(f"{filename.ljust(KEY_LEN*3//2)}: {len(i18n_strings)}")
if SHOW_KEYS:
print("\n".join([s for s in i18n_strings]))
strings.extend(i18n_strings) strings.extend(i18n_strings)
except Exception as e:
print(f"\033[31m[Failed] Error occur at {filename}: {e}\033[0m")
code_keys = set(strings) code_keys = set(strings)
print(f"{'Total Unique'.ljust(30)}: {len(code_keys)}") print(f"{'Total Unique'.ljust(KEY_LEN*3//2)}: {len(code_keys)}")
return code_keys return code_keys
def update_i18n_json(json_file, standard_keys): def update_i18n_json(json_file, standard_keys):