From 407ab98000df8b18a9d1bf2593a588e1c9210196 Mon Sep 17 00:00:00 2001 From: OleSTEEP Date: Sat, 19 Oct 2024 02:20:58 +0300 Subject: [PATCH] vnrecode: duplicates check for more than two files --- vnrecode/compress.py | 2 +- vnrecode/utils.py | 24 ++++++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/vnrecode/compress.py b/vnrecode/compress.py index 4050e4c..b1c2567 100644 --- a/vnrecode/compress.py +++ b/vnrecode/compress.py @@ -192,6 +192,6 @@ class Compress: case "unknown": out_file = self.unknown(source, output) - self.__utils.out_rename(out_file, source.name) + self.__utils.out_rename(out_file, source) self.__printer.bar.update() self.__printer.bar.next() diff --git a/vnrecode/utils.py b/vnrecode/utils.py index 7da06d3..c30abe3 100644 --- a/vnrecode/utils.py +++ b/vnrecode/utils.py @@ -17,7 +17,7 @@ class Utils: self.__errors = 0 self.__params = params_inst self.__printer = printer_inst - self.__duplicates = [] + self.__duplicates = {} @staticmethod def sys_pause(): @@ -102,9 +102,13 @@ class Utils: :return: Duplicate path name with (vncopy) on end """ if path.is_file() and path.exists(): - new_path = Path(path.stem + "(vncopy)" + path.suffix) - self.__duplicates.append(new_path) - return new_path + orig_name = path.name.replace("(vncopy)", "") + new_path = Path(path.parent, path.stem + "(vncopy)" + path.suffix) + try: self.__duplicates[orig_name] + except KeyError: self.__duplicates[orig_name] = [] + if not new_path.name in self.__duplicates[orig_name]: + self.__duplicates[orig_name].append(new_path.name) + return self.catch_duplicates(new_path) return path def print_duplicates(self): @@ -112,13 +116,13 @@ class Utils: Method prints message about all duplicates generated during recode process :return: None """ - for filename in self.__duplicates: + for filename in self.__duplicates.keys(): self.__printer.warning( - f'Duplicate file has been found! Check manually this files - "{filename.name}", ' - f'"{filename.stem + "(vncopy)" + filename.suffix}"' + f'Duplicate file has been found! Check manually this files - "{filename}", ' + + ', '.join(self.__duplicates[filename]) ) - def out_rename(self, out_path: Path, target: str): + def out_rename(self, out_path: Path, target: Path): """ Method removes md5 hash from file name and changes file extension in dependence of mimic mode :param out_path: Recoded file Path @@ -126,7 +130,7 @@ class Utils: :return: None """ if not self.__params.mimic_mode: - dest_name = self.catch_duplicates(Path(out_path.parent, target)) + dest_name = self.catch_duplicates(Path(out_path.parent, target.stem+out_path.suffix)) os.rename(out_path, dest_name) else: - os.rename(out_path, Path(out_path.parent, target)) + os.rename(out_path, Path(out_path.parent, target.name))