mirror of
https://github.com/xzb23/check_duplicate_files.git
synced 2026-11-24 18:45:31 +08:00
添加I18n支持
This commit is contained in:
7 files changed
+98
-13
No files matched your search
@@ -5,3 +5,5 @@ build
|
|||||||
*.spec
|
*.spec
|
||||||
*.backup
|
*.backup
|
||||||
/.venv
|
/.venv
|
||||||
|
/.vscode
|
||||||
|
/conf.d
|
||||||
@@ -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
|
||||||
@@ -5,3 +5,4 @@ import hashlib
|
|||||||
import typing
|
import typing
|
||||||
import collections
|
import collections
|
||||||
import json
|
import json
|
||||||
|
import platform
|
||||||
@@ -2,19 +2,20 @@ from _in_package import *
|
|||||||
import config
|
import config
|
||||||
import common
|
import common
|
||||||
import runing_data
|
import runing_data
|
||||||
|
import I18n
|
||||||
|
|
||||||
|
|
||||||
def traverse_dir(root_dir):
|
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):
|
for path, dirs, files in os.walk(root_dir):
|
||||||
path = common.formatting_path(path)
|
path = common.formatting_path(path)
|
||||||
for filename in files:
|
for filename in files:
|
||||||
runing_data.file_path_list.append(path + filename)
|
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):
|
def hash_files_from_glist(algorithm: function):
|
||||||
print("正在计算文件散列ing...")
|
print(config.l10n_data.get_text("tip_hash_files_from_glist"))
|
||||||
i = 0
|
i = 0
|
||||||
p_old_i = -1
|
p_old_i = -1
|
||||||
for f_path in runing_data.file_path_list:
|
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]:
|
for fp in runing_data.hash_to_files[t]:
|
||||||
print("\t", fp)
|
print("\t", fp)
|
||||||
print()
|
print()
|
||||||
print(f"重复部分占用/总占用:\t{(size_total-size_if_one)//1024}KiB / {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(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_if_size-flag_duplicate_msg"), config.flag_size, (print_size_total-print_size_if_one)//1024, print_size_total//1024))
|
||||||
|
|
||||||
|
|
||||||
def reg_opts():
|
def reg_opts():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("-P", "--path", default=None, required=False)
|
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()
|
args = parser.parse_args()
|
||||||
if args.path != None:
|
if args.path != None:
|
||||||
config.work_dir = args.path
|
config.work_dir = args.path
|
||||||
if args.fsize != None:
|
if args.fsize != None:
|
||||||
config.flag_size = int(args.fsize)
|
config.flag_size = int(args.fsize)
|
||||||
|
|
||||||
reg_opts()
|
if __name__ == "__main__":
|
||||||
|
if not os.path.exists(config.work_dir):
|
||||||
if not os.path.exists(config.work_dir):
|
|
||||||
exit(-1)
|
exit(-1)
|
||||||
|
config.exe_dir = common.get_exe_dir()
|
||||||
|
|
||||||
traverse_dir(config.work_dir)
|
lang = common.get_config("lang")
|
||||||
hash_files_from_glist(hashlib.sha256)
|
if lang != None:
|
||||||
|
config.lang = lang
|
||||||
|
else: lang = config.lang
|
||||||
|
config.l10n_data = I18n.get_lang_file(lang)
|
||||||
|
reg_opts()
|
||||||
|
|
||||||
print_info()
|
traverse_dir(config.work_dir)
|
||||||
|
hash_files_from_glist(hashlib.sha256)
|
||||||
|
|
||||||
|
print_info()
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from _in_package import *
|
from _in_package import *
|
||||||
from config import *
|
import config
|
||||||
|
|
||||||
|
|
||||||
def formatting_path(path: str) -> str:
|
def formatting_path(path: str) -> str:
|
||||||
@@ -8,3 +8,35 @@ def formatting_path(path: str) -> str:
|
|||||||
if path[-1] != "/":
|
if path[-1] != "/":
|
||||||
path += "/"
|
path += "/"
|
||||||
return path
|
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__"
|
||||||
@@ -2,5 +2,12 @@ from _in_package import *
|
|||||||
|
|
||||||
flag_size: int = 0
|
flag_size: int = 0
|
||||||
work_dir: str = "."
|
work_dir: str = "."
|
||||||
|
exe_dir: str
|
||||||
|
|
||||||
wait_c: int = 100
|
wait_c: int = 100
|
||||||
|
|
||||||
|
lang: str = "zh_cn"
|
||||||
|
l10n_data: I18n.I18nText
|
||||||
|
|
||||||
|
configdir_path: str = None
|
||||||
|
import I18n
|
||||||
@@ -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
|
||||||
Reference in new issue
Block a user