From ca6e36e1647970f58ddd34af12d9b6ccc5ac9613 Mon Sep 17 00:00:00 2001 From: Jade Date: Sun, 20 Oct 2024 00:27:51 +0900 Subject: [PATCH 1/2] fix: remove debug print statements from main function in sub_adjust.py --- sub_adjust.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sub_adjust.py b/sub_adjust.py index 0147823..65e2b38 100644 --- a/sub_adjust.py +++ b/sub_adjust.py @@ -324,7 +324,6 @@ def check_queue(): def main(): # 使用 options_first=True 确保没有参数时不会直接触发 Usage 输出 args = docopt(USAGE, version=__version__, options_first=True) - print(args) input_files = args["INPUTS"] @@ -340,7 +339,6 @@ def main(): logger.info("进入命令行模式...") shift_value = float(args["--offset"]) * 1000 layer_numbers = parse_layers(args["--layers"]) - print(layer_numbers) if input_files: for filepath in input_files: if filepath.endswith(".ass") or filepath.endswith(".ssa"): From e7cbbb9950aa177de3e36a1905b669cb115a5e45 Mon Sep 17 00:00:00 2001 From: Jade Date: Sun, 20 Oct 2024 11:21:14 +0900 Subject: [PATCH 2/2] feat: add file processing function to handle multiple file types and log results is command line mode --- sub_adjust.py | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/sub_adjust.py b/sub_adjust.py index 65e2b38..39d0678 100644 --- a/sub_adjust.py +++ b/sub_adjust.py @@ -3,6 +3,7 @@ import threading import tkinter as tk from tkinter import (messagebox, scrolledtext) +from typing import List, Optional import webbrowser from docopt import docopt from loguru import logger @@ -174,7 +175,43 @@ def process_ass_ssa_file(filepath, adjusted_shift_value, layers): if is_cmd_mode: logger.error(f"错误处理文件 {filepath}: {str(e)}") return False, str(e) + +def shift_times_in_filelist(filelist: Optional[List[str]], shift_value: float, layers, queue): + total_files = len(filelist) + success_count = 0 + failure_count = 0 + failure_reasons = [] + for filepath in filelist: + if filepath.endswith(".ass") or filepath.endswith(".ssa"): + success, reason = process_ass_ssa_file(filepath, shift_value, layers) + elif filepath.endswith(".srt"): + success, reason = process_srt_file(filepath, shift_value) + + if success: + success_count += 1 + else: + failure_count += 1 + failure_reasons.append(f"{filepath}: {reason}") + + result_message = ( + f"运行完毕。\n" + f"共处理 {total_files} 个文件。\n" + f"成功处理 {success_count} 个文件。\n" + f"失败处理 {failure_count} 个文件。\n" + ) + + if failure_count > 0: + result_message += "\n失败原因:\n" + "\n".join(failure_reasons) + queue.put(result_message) + else: + queue.put(result_message) + + if is_cmd_mode: + log_result_fn = logger.info if success_count > 0 else logger.error + log_result_fn(f"\n\n{result_message}") + + def shift_times_in_directory(directory, shift_value, shift_direction, layer_numbers, queue): if shift_direction is None: shift_direction = "delay" @@ -205,6 +242,7 @@ def shift_times_in_directory(directory, shift_value, shift_direction, layer_numb failure_reasons.append(f"{file}: {reason}") result_message = ( + f"运行完毕。\n" f"共处理 {total_files} 个文件。\n" f"成功处理 {success_count} 个文件。\n" f"失败处理 {failure_count} 个文件。\n" @@ -215,6 +253,10 @@ def shift_times_in_directory(directory, shift_value, shift_direction, layer_numb queue.put(result_message) + if is_cmd_mode: + log_result_fn = logger.info if success_count > 0 else logger.error + log_result_fn(f"\n\n{result_message}") + def open_mail(event=None): webbrowser.open("mailto:i@kayanoai.net") @@ -325,7 +367,7 @@ def main(): # 使用 options_first=True 确保没有参数时不会直接触发 Usage 输出 args = docopt(USAGE, version=__version__, options_first=True) - input_files = args["INPUTS"] + input_files: Optional[List[str]] = args["INPUTS"] # 如果用户请求版本信息,显示版本信息 if args["--version"]: @@ -340,11 +382,7 @@ def main(): shift_value = float(args["--offset"]) * 1000 layer_numbers = parse_layers(args["--layers"]) if input_files: - for filepath in input_files: - if filepath.endswith(".ass") or filepath.endswith(".ssa"): - process_ass_ssa_file(filepath, shift_value, layer_numbers) - elif filepath.endswith(".srt"): - process_srt_file(filepath, shift_value) + shift_times_in_filelist(input_files, shift_value, layer_numbers, queue.Queue()) else: shift_times_in_directory(os.getcwd(), shift_value, None, layer_numbers, queue.Queue())