Refactor: Format Code with Ruff and Update Deprecated G2PW Link (#2255)
* ruff check --fix * ruff format --line-length 120 --target-version py39 * Change the link for G2PW Model * update pytorch version and colab
This commit is contained in:
@@ -2,23 +2,27 @@ import json
|
||||
import locale
|
||||
import os
|
||||
|
||||
I18N_JSON_DIR : os.PathLike = os.path.join(os.path.dirname(os.path.relpath(__file__)), 'locale')
|
||||
I18N_JSON_DIR: os.PathLike = os.path.join(os.path.dirname(os.path.relpath(__file__)), "locale")
|
||||
|
||||
|
||||
def load_language_list(language):
|
||||
with open(os.path.join(I18N_JSON_DIR, f"{language}.json"), "r", encoding="utf-8") as f:
|
||||
language_list = json.load(f)
|
||||
return language_list
|
||||
|
||||
|
||||
def scan_language_list():
|
||||
language_list = []
|
||||
for name in os.listdir(I18N_JSON_DIR):
|
||||
if name.endswith(".json"):language_list.append(name.split('.')[0])
|
||||
if name.endswith(".json"):
|
||||
language_list.append(name.split(".")[0])
|
||||
return language_list
|
||||
|
||||
|
||||
class I18nAuto:
|
||||
def __init__(self, language=None):
|
||||
if language in ["Auto", None]:
|
||||
language = locale.getdefaultlocale()[0]
|
||||
language = locale.getdefaultlocale()[0]
|
||||
# getlocale can't identify the system's language ((None, None))
|
||||
if not os.path.exists(os.path.join(I18N_JSON_DIR, f"{language}.json")):
|
||||
language = "en_US"
|
||||
@@ -31,6 +35,7 @@ class I18nAuto:
|
||||
def __repr__(self):
|
||||
return "Use Language: " + self.language
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
i18n = I18nAuto(language='en_US')
|
||||
print(i18n)
|
||||
i18n = I18nAuto(language="en_US")
|
||||
print(i18n)
|
||||
|
||||
@@ -4,21 +4,18 @@ import json
|
||||
import os
|
||||
from collections import OrderedDict
|
||||
|
||||
I18N_JSON_DIR : os.PathLike = os.path.join(os.path.dirname(os.path.relpath(__file__)), 'locale')
|
||||
DEFAULT_LANGUAGE: str = "zh_CN" # 默认语言
|
||||
TITLE_LEN : int = 60 # 标题显示长度
|
||||
KEY_LEN : int = 30 # 键名显示长度
|
||||
SHOW_KEYS : bool = False # 是否显示键信息
|
||||
SORT_KEYS : bool = False # 是否按全局键名写入文件
|
||||
I18N_JSON_DIR: os.PathLike = os.path.join(os.path.dirname(os.path.relpath(__file__)), "locale")
|
||||
DEFAULT_LANGUAGE: str = "zh_CN" # 默认语言
|
||||
TITLE_LEN: int = 60 # 标题显示长度
|
||||
KEY_LEN: int = 30 # 键名显示长度
|
||||
SHOW_KEYS: bool = False # 是否显示键信息
|
||||
SORT_KEYS: bool = False # 是否按全局键名写入文件
|
||||
|
||||
|
||||
def extract_i18n_strings(node):
|
||||
i18n_strings = []
|
||||
|
||||
if (
|
||||
isinstance(node, ast.Call)
|
||||
and isinstance(node.func, ast.Name)
|
||||
and node.func.id == "i18n"
|
||||
):
|
||||
if isinstance(node, ast.Call) and isinstance(node.func, ast.Name) and node.func.id == "i18n":
|
||||
for arg in node.args:
|
||||
if isinstance(arg, ast.Str):
|
||||
i18n_strings.append(arg.s)
|
||||
@@ -28,6 +25,7 @@ def extract_i18n_strings(node):
|
||||
|
||||
return i18n_strings
|
||||
|
||||
|
||||
def scan_i18n_strings():
|
||||
"""
|
||||
scan the directory for all .py files (recursively)
|
||||
@@ -43,7 +41,7 @@ def scan_i18n_strings():
|
||||
if "I18nAuto" in code:
|
||||
tree = ast.parse(code)
|
||||
i18n_strings = extract_i18n_strings(tree)
|
||||
print(f"{filename.ljust(KEY_LEN*3//2)}: {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)
|
||||
@@ -51,9 +49,10 @@ def scan_i18n_strings():
|
||||
print(f"\033[31m[Failed] Error occur at {filename}: {e}\033[0m")
|
||||
|
||||
code_keys = set(strings)
|
||||
print(f"{'Total Unique'.ljust(KEY_LEN*3//2)}: {len(code_keys)}")
|
||||
print(f"{'Total Unique'.ljust(KEY_LEN * 3 // 2)}: {len(code_keys)}")
|
||||
return code_keys
|
||||
|
||||
|
||||
def update_i18n_json(json_file, standard_keys):
|
||||
standard_keys = sorted(standard_keys)
|
||||
print(f" Process {json_file} ".center(TITLE_LEN, "="))
|
||||
@@ -89,8 +88,10 @@ def update_i18n_json(json_file, standard_keys):
|
||||
sorted(
|
||||
json_data.items(),
|
||||
key=lambda x: (
|
||||
list(standard_keys).index(x[0]) if x[0] in standard_keys and not x[1].startswith('#!') else len(json_data),
|
||||
)
|
||||
list(standard_keys).index(x[0])
|
||||
if x[0] in standard_keys and not x[1].startswith("#!")
|
||||
else len(json_data),
|
||||
),
|
||||
)
|
||||
)
|
||||
# 打印处理后的 JSON 条目数
|
||||
@@ -111,21 +112,26 @@ def update_i18n_json(json_file, standard_keys):
|
||||
# 打印是否有重复的值
|
||||
for value, keys in duplicate_items.items():
|
||||
if len(keys) > 1:
|
||||
print("\n".join([f"\033[31m{'[Failed] Duplicate Value'.ljust(KEY_LEN)}: {key} -> {value}\033[0m" for key in keys]))
|
||||
print(
|
||||
"\n".join(
|
||||
[f"\033[31m{'[Failed] Duplicate Value'.ljust(KEY_LEN)}: {key} -> {value}\033[0m" for key in keys]
|
||||
)
|
||||
)
|
||||
|
||||
if num_miss_translation > 0:
|
||||
print(f"\033[31m{'[Failed] Missing Translation'.ljust(KEY_LEN)}: {num_miss_translation}\033[0m")
|
||||
else:
|
||||
print(f"\033[32m[Passed] All Keys Translated\033[0m")
|
||||
print("\033[32m[Passed] All Keys Translated\033[0m")
|
||||
# 将处理后的结果写入 JSON 文件
|
||||
with open(json_file, "w", encoding="utf-8") as f:
|
||||
json.dump(json_data, f, ensure_ascii=False, indent=4, sort_keys=SORT_KEYS)
|
||||
f.write("\n")
|
||||
print(f" Updated {json_file} ".center(TITLE_LEN, "=") + '\n')
|
||||
print(f" Updated {json_file} ".center(TITLE_LEN, "=") + "\n")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
code_keys = scan_i18n_strings()
|
||||
for json_file in os.listdir(I18N_JSON_DIR):
|
||||
if json_file.endswith(r".json"):
|
||||
json_file = os.path.join(I18N_JSON_DIR, json_file)
|
||||
update_i18n_json(json_file, code_keys)
|
||||
update_i18n_json(json_file, code_keys)
|
||||
|
||||
Reference in New Issue
Block a user