diff --git a/.gitignore b/.gitignore index 846f1b6..146e3f0 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,6 @@ test build *.spec *.backup -/.venv \ No newline at end of file +/.venv +/.vscode +/conf.d \ No newline at end of file diff --git a/I18n.py b/I18n.py new file mode 100644 index 0000000..ac223c3 --- /dev/null +++ b/I18n.py @@ -0,0 +1,29 @@ +from _in_package import * +import config + +class I18nText: + langid: str + i18n_data: dict[str:str] + def __init__(self, langid: str, i18n_data: str): + self.langid = langid + self.i18n_data = i18n_data + # return self + def get_text(self, i18nid: str) -> str: + if not (i18nid in self.i18n_data.keys()): + return "__langerr__" + return self.i18n_data[i18nid] + + +def get_lang_file(lang: str = config.lang): + lang_file = config.exe_dir + f"lang/{lang}.lang" + if not (os.path.isfile(lang_file)): + return I18nText(lang, {}) + with open(lang_file, "r", encoding="utf-8") as lf: + l10n_lines = lf.read().strip().split("\n") + i18n_data = {} + for l10n_line in l10n_lines: + if len(l10n_line.split("=")) != 2: + continue + i18n_data[l10n_line.split("=")[0].strip()] = l10n_line.split("=")[1].strip() + l10n_data = I18nText(lang, i18n_data) + return l10n_data \ No newline at end of file diff --git a/_in_package.py b/_in_package.py index 224c83d..bdbcd39 100644 --- a/_in_package.py +++ b/_in_package.py @@ -4,4 +4,5 @@ import argparse import hashlib import typing import collections -import json \ No newline at end of file +import json +import platform \ No newline at end of file diff --git a/app.py b/app.py index 2861995..51f37c0 100644 --- a/app.py +++ b/app.py @@ -2,19 +2,20 @@ from _in_package import * import config import common import runing_data +import I18n def traverse_dir(root_dir): - print("正在遍历文件ing...") + print(config.l10n_data.get_text("tip_traverse_dir")) for path, dirs, files in os.walk(root_dir): path = common.formatting_path(path) for filename in files: runing_data.file_path_list.append(path + filename) - print(f"完成\t总计 {len(runing_data.file_path_list)} 个文件") + print(common.safe_format(config.l10n_data.get_text("info_traverse_dir"), len(runing_data.file_path_list))) def hash_files_from_glist(algorithm: function): - print("正在计算文件散列ing...") + print(config.l10n_data.get_text("tip_hash_files_from_glist")) i = 0 p_old_i = -1 for f_path in runing_data.file_path_list: @@ -50,26 +51,33 @@ def print_info(hash_to_files_list: list = runing_data.hash_to_files): for fp in runing_data.hash_to_files[t]: print("\t", fp) print() - print(f"重复部分占用/总占用:\t{(size_total-size_if_one)//1024}KiB / {size_total//1024}KiB") - print(f"占用大于 {config.flag_size} Byte 的文件中,重复部分占用/总占用:\t{(print_size_total-print_size_if_one)//1024}KiB / {print_size_total//1024}KiB") + print(common.safe_format(config.l10n_data.get_text("info_print_info_duplicate_msg"), (size_total-size_if_one)//1024, size_total//1024)) + print(common.safe_format(config.l10n_data.get_text("info_print_info_if_size-flag_duplicate_msg"), config.flag_size, (print_size_total-print_size_if_one)//1024, print_size_total//1024)) def reg_opts(): parser = argparse.ArgumentParser() parser.add_argument("-P", "--path", default=None, required=False) - parser.add_argument("--fsize", default=None, required=False, help="只打印文件大小大于fsize Byte的重复文件") + parser.add_argument("--fsize", default=None, required=False, help=config.l10n_data.get_text("help_fsize")) args = parser.parse_args() if args.path != None: config.work_dir = args.path if args.fsize != None: config.flag_size = int(args.fsize) -reg_opts() +if __name__ == "__main__": + if not os.path.exists(config.work_dir): + exit(-1) + config.exe_dir = common.get_exe_dir() -if not os.path.exists(config.work_dir): - exit(-1) + lang = common.get_config("lang") + if lang != None: + config.lang = lang + else: lang = config.lang + config.l10n_data = I18n.get_lang_file(lang) + reg_opts() -traverse_dir(config.work_dir) -hash_files_from_glist(hashlib.sha256) + traverse_dir(config.work_dir) + hash_files_from_glist(hashlib.sha256) -print_info() \ No newline at end of file + print_info() \ No newline at end of file diff --git a/common.py b/common.py index c9a6d71..1ddc28b 100644 --- a/common.py +++ b/common.py @@ -1,5 +1,5 @@ from _in_package import * -from config import * +import config def formatting_path(path: str) -> str: @@ -7,4 +7,36 @@ def formatting_path(path: str) -> str: path = path.replace("\\", "/") if path[-1] != "/": path += "/" - return path \ No newline at end of file + return path + +def get_exe_dir(): + if getattr(sys, "frozen", False): + exe_dir = os.path.dirname(os.path.abspath(sys.executable)) + else: + exe_dir = os.path.dirname(os.path.abspath(__file__)) + return formatting_path(exe_dir) + +def get_config(config_key: str): + if config.configdir_path == None: + match platform.system(): + case "Linux": + if os.path.isfile(config.exe_dir + "conf.dir"): + with open(config.exe_dir + "conf.dir") as cpf: + configdir_path = cpf.read().strip() + else: + configdir_path = "/etc/check_duplicate_files/" + case "Windows": + configdir_path = config.exe_dir + "conf.d/" + config.configdir_path = formatting_path(configdir_path) + config_path = config.configdir_path + config_key + if not (os.path.isfile(config_path)): + return None + with open(config_path, "r", encoding="utf-8") as conf: + conf_v = conf.read().strip() + return conf_v + + +def safe_format(fmt_str: str, *args) -> str: + try: + return fmt_str.format(*args) + except: return "__langerr__" \ No newline at end of file diff --git a/config.py b/config.py index 5d1d8fe..de3d810 100644 --- a/config.py +++ b/config.py @@ -2,5 +2,12 @@ from _in_package import * flag_size: int = 0 work_dir: str = "." +exe_dir: str -wait_c: int = 100 \ No newline at end of file +wait_c: int = 100 + +lang: str = "zh_cn" +l10n_data: I18n.I18nText + +configdir_path: str = None +import I18n \ No newline at end of file diff --git a/lang/zh_cn.lang b/lang/zh_cn.lang new file mode 100644 index 0000000..608e0c4 --- /dev/null +++ b/lang/zh_cn.lang @@ -0,0 +1,6 @@ +help_fsize = 只打印文件大小大于fsize Byte的重复文件 +tip_traverse_dir = 正在遍历文件ing... +info_traverse_dir = 完成 总计 {} 个文件 +tip_hash_files_from_glist = 正在计算文件散列ing... +info_print_info_duplicate_msg = 重复部分占用/总占用: {}KiB / {}KiB +info_print_info_if_size-flag_duplicate_msg = 占用大于 {} Byte 的文件中,重复部分占用/总占用: {}KiB / {}KiB \ No newline at end of file