From df8c7bb50c01a5bf61aeb840cdf9b80b060b90e9 Mon Sep 17 00:00:00 2001 From: towzeur Date: Mon, 21 Aug 2023 10:28:41 +0200 Subject: [PATCH 1/2] add scan_i18n to detect unused and missing keys (#1058) --- scan_i18n.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 scan_i18n.py diff --git a/scan_i18n.py b/scan_i18n.py new file mode 100644 index 0000000..1f9247d --- /dev/null +++ b/scan_i18n.py @@ -0,0 +1,68 @@ +import ast +import glob +import json + +from collections import OrderedDict + +def extract_i18n_strings(node): + i18n_strings = [] + + 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) + + for child_node in ast.iter_child_nodes(node): + i18n_strings.extend(extract_i18n_strings(child_node)) + + return i18n_strings + +# scan the directory for all .py files (recursively) +# for each file, parse the code into an AST +# for each AST, extract the i18n strings + +strings = [] +for filename in glob.iglob('**/*.py', recursive=True): + with open(filename, 'r') as f: + code = f.read() + if "I18nAuto" in code: + tree = ast.parse(code) + i18n_strings = extract_i18n_strings(tree) + print(filename, len(i18n_strings)) + strings.extend(i18n_strings) +code_keys = set(strings) +''' +n_i18n.py +gui_v1.py 26 +app.py 16 +infer-web.py 147 +scan_i18n.py 0 +i18n.py 0 +lib/train/process_ckpt.py 1 +''' +print() +print('Total unique:', len(code_keys)) + + + +standard_file = "zh_CN.json" +with open(f"lib/i18n/{standard_file}", "r", encoding="utf-8") as f: + standard_data = json.load(f, object_pairs_hook=OrderedDict) +standard_keys = set(standard_data.keys()) + +# Define the standard file name +unused_keys = standard_keys - code_keys +print('Unused keys:', len(unused_keys)) +for unused_key in unused_keys: + print('\t', unused_key) + +missing_keys = code_keys - standard_keys +print('Missing keys:', len(missing_keys)) +for missing_key in missing_keys: + print('\t', missing_key) + + + + + + From 2789b8e8f405bf6d7563e6791fe95baba46c6600 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 21 Aug 2023 16:29:27 +0800 Subject: [PATCH 2/2] Format code (#1024) Co-authored-by: github-actions[bot] --- config.py | 35 +++++++++++++++++++++++++++++------ infer-web.py | 5 ++++- scan_i18n.py | 33 ++++++++++++++++----------------- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/config.py b/config.py index 0261b02..53f8135 100644 --- a/config.py +++ b/config.py @@ -148,28 +148,51 @@ class Config: x_max = 32 if self.dml: print("use DirectML instead") - if(os.path.exists("runtime\Lib\site-packages\onnxruntime\capi\DirectML.dll")==False): + if ( + os.path.exists( + "runtime\Lib\site-packages\onnxruntime\capi\DirectML.dll" + ) + == False + ): try: - os.rename("runtime\Lib\site-packages\onnxruntime", "runtime\Lib\site-packages\onnxruntime-cuda") + os.rename( + "runtime\Lib\site-packages\onnxruntime", + "runtime\Lib\site-packages\onnxruntime-cuda", + ) except: pass try: - os.rename("runtime\Lib\site-packages\onnxruntime-dml", "runtime\Lib\site-packages\onnxruntime") + os.rename( + "runtime\Lib\site-packages\onnxruntime-dml", + "runtime\Lib\site-packages\onnxruntime", + ) except: pass import torch_directml + self.device = torch_directml.device(torch_directml.default_device()) self.is_half = False else: if self.instead: print(f"use {self.instead} instead") - if(os.path.exists("runtime\Lib\site-packages\onnxruntime\capi\onnxruntime_providers_cuda.dll")==False): + if ( + os.path.exists( + "runtime\Lib\site-packages\onnxruntime\capi\onnxruntime_providers_cuda.dll" + ) + == False + ): try: - os.rename("runtime\Lib\site-packages\onnxruntime", "runtime\Lib\site-packages\onnxruntime-dml") + os.rename( + "runtime\Lib\site-packages\onnxruntime", + "runtime\Lib\site-packages\onnxruntime-dml", + ) except: pass try: - os.rename("runtime\Lib\site-packages\onnxruntime-cuda", "runtime\Lib\site-packages\onnxruntime") + os.rename( + "runtime\Lib\site-packages\onnxruntime-cuda", + "runtime\Lib\site-packages\onnxruntime", + ) except: pass return x_pad, x_query, x_center, x_max diff --git a/infer-web.py b/infer-web.py index 742299b..c5fd117 100644 --- a/infer-web.py +++ b/infer-web.py @@ -239,7 +239,10 @@ def vc_single( times[0], times[1], times[2], - ), (resample_sr if resample_sr >= 16000 and tgt_sr != resample_sr else tgt_sr, audio_opt) + ), ( + resample_sr if resample_sr >= 16000 and tgt_sr != resample_sr else tgt_sr, + audio_opt, + ) except: info = traceback.format_exc() print(info) diff --git a/scan_i18n.py b/scan_i18n.py index 1f9247d..7f4a3f7 100644 --- a/scan_i18n.py +++ b/scan_i18n.py @@ -4,10 +4,15 @@ import json from collections import OrderedDict + 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) @@ -17,13 +22,14 @@ def extract_i18n_strings(node): return i18n_strings + # scan the directory for all .py files (recursively) # for each file, parse the code into an AST # for each AST, extract the i18n strings strings = [] -for filename in glob.iglob('**/*.py', recursive=True): - with open(filename, 'r') as f: +for filename in glob.iglob("**/*.py", recursive=True): + with open(filename, "r") as f: code = f.read() if "I18nAuto" in code: tree = ast.parse(code) @@ -31,7 +37,7 @@ for filename in glob.iglob('**/*.py', recursive=True): print(filename, len(i18n_strings)) strings.extend(i18n_strings) code_keys = set(strings) -''' +""" n_i18n.py gui_v1.py 26 app.py 16 @@ -39,10 +45,9 @@ infer-web.py 147 scan_i18n.py 0 i18n.py 0 lib/train/process_ckpt.py 1 -''' +""" print() -print('Total unique:', len(code_keys)) - +print("Total unique:", len(code_keys)) standard_file = "zh_CN.json" @@ -52,17 +57,11 @@ standard_keys = set(standard_data.keys()) # Define the standard file name unused_keys = standard_keys - code_keys -print('Unused keys:', len(unused_keys)) +print("Unused keys:", len(unused_keys)) for unused_key in unused_keys: - print('\t', unused_key) + print("\t", unused_key) missing_keys = code_keys - standard_keys -print('Missing keys:', len(missing_keys)) +print("Missing keys:", len(missing_keys)) for missing_key in missing_keys: - print('\t', missing_key) - - - - - - + print("\t", missing_key)