From ce6f25f656d31cbcbf78c013186cd1ad19f7540d Mon Sep 17 00:00:00 2001 From: Kenichiro NOGI Date: Sun, 2 Aug 2026 10:24:40 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=E3=83=AD=E3=82=B0=E6=94=B9=E8=A1=8C?= =?UTF-8?q?=E6=B7=B7=E5=85=A5=E5=AF=BE=E7=AD=96=E3=81=A8run=5Fqueue?= =?UTF-8?q?=E5=86=85=E6=9C=AA=E5=87=A6=E7=90=86=E4=BE=8B=E5=A4=96=E3=81=AE?= =?UTF-8?q?=E5=AE=89=E5=85=A8=E7=B5=82=E4=BA=86=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit コードレビュー指摘対応。 - ログ備考欄に改行(スタックトレース等)が混入すると1行フォーマットが崩れるため、改行を空白に平坦化 - run_queue内でファイルコピー・移動が例外を投げた場合に未処理例外でクラッシュしていた問題を修正。main()側でrun_queue呼び出しをtry/exceptし、ログ記録の上でロックを解放して安全終了する Co-Authored-By: Claude Sonnet 5 --- .../ScanOCR/スクリプト/extract_and_rename.py | 29 ++++++++++++------- PythonProj/ScanOCR/スクリプト/logger.py | 3 +- .../ScanOCR/スクリプト/tests/test_logger.py | 20 +++++++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/PythonProj/ScanOCR/スクリプト/extract_and_rename.py b/PythonProj/ScanOCR/スクリプト/extract_and_rename.py index 5a8bcec6..46a35a63 100644 --- a/PythonProj/ScanOCR/スクリプト/extract_and_rename.py +++ b/PythonProj/ScanOCR/スクリプト/extract_and_rename.py @@ -283,17 +283,24 @@ def main() -> None: stable_wait_sec = float(config["ファイル安定待ち秒"]) stable_retries = int(config["ファイル安定待ちリトライ回数"]) - run_queue( - folders["スキャンフォルダ"], - folders["アウトプットフォルダ"], - folders["成功フォルダ"], - folders["失敗フォルダ"], - folders["ログフォルダ"], - boxes, - margin, - stable_wait_sec, - stable_retries, - ) + try: + run_queue( + folders["スキャンフォルダ"], + folders["アウトプットフォルダ"], + folders["成功フォルダ"], + folders["失敗フォルダ"], + folders["ログフォルダ"], + boxes, + margin, + stable_wait_sec, + stable_retries, + ) + except Exception as e: + append_log( + folders["ログフォルダ"], "エラー", "-", None, None, + f"予期しないエラーで処理を中断しました: {type(e).__name__}: {e}", + ) + print(f"予期しないエラーが発生しました: {e}") finally: release_lock(lock_path) diff --git a/PythonProj/ScanOCR/スクリプト/logger.py b/PythonProj/ScanOCR/スクリプト/logger.py index 839a46ea..d7bf48aa 100644 --- a/PythonProj/ScanOCR/スクリプト/logger.py +++ b/PythonProj/ScanOCR/スクリプト/logger.py @@ -26,10 +26,11 @@ def append_log( ) -> None: log_dir.mkdir(parents=True, exist_ok=True) now = now_func() + flat_note = note.replace("\r\n", " ").replace("\n", " ").replace("\r", " ") line = ( f"[{now.strftime('%Y-%m-%d %H:%M:%S')}] " f"結果={result} | 元ファイル={original_filename} | " - f"氏名={name or ''} | 日付={date or ''} | 備考={note}\n" + f"氏名={name or ''} | 日付={date or ''} | 備考={flat_note}\n" ) path = log_path_for_today(log_dir, now_func) with open(path, "a", encoding=LOG_ENCODING) as f: diff --git a/PythonProj/ScanOCR/スクリプト/tests/test_logger.py b/PythonProj/ScanOCR/スクリプト/tests/test_logger.py index eabfb4f1..43fc4eb5 100644 --- a/PythonProj/ScanOCR/スクリプト/tests/test_logger.py +++ b/PythonProj/ScanOCR/スクリプト/tests/test_logger.py @@ -55,6 +55,26 @@ def test_append_log_handles_none_name_and_date(tmp_path): assert "備考=抽出不十分" in content +def test_append_log_flattens_multiline_note(tmp_path): + log_dir = tmp_path / "ログ" + + append_log( + log_dir, + result="エラー", + original_filename="scan003.pdf", + name=None, + date=None, + note="RuntimeError: OCRエラー\nTraceback (most recent call last):\n line 1", + now_func=fixed_now, + ) + + content = (log_dir / "2026-08-02.log").read_text(encoding=LOG_ENCODING) + lines = content.splitlines() + assert len(lines) == 1 + assert "\n" not in lines[0] + assert "RuntimeError: OCRエラー" in lines[0] + + def test_append_log_appends_multiple_lines(tmp_path): log_dir = tmp_path / "ログ"