ken_nogi/PythonProj/ScanOCR/スクリプト/config_loader.py
Kenichiro NOGI 4ae53c2725 feat: config.txt読込モジュールを追加
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-02 10:08:02 +09:00

46 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""config.txtkey=value形式読込モジュール。"""
from __future__ import annotations
from pathlib import Path
class ConfigError(Exception):
pass
REQUIRED_KEYS = (
"スクリプトフォルダ",
"テンプレートフォルダ",
"スキャンフォルダ",
"アウトプットフォルダ",
"成功フォルダ",
"失敗フォルダ",
"ログフォルダ",
"margin",
"DPI",
"ファイル安定待ち秒",
"ファイル安定待ちリトライ回数",
)
def load_config(config_path: Path) -> dict[str, str]:
if not config_path.exists():
raise ConfigError(f"config.txt が見つかりません: {config_path}")
config: dict[str, str] = {}
for raw_line in config_path.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, _, value = line.partition("=")
config[key.strip()] = value.strip()
missing = [key for key in REQUIRED_KEYS if key not in config]
if missing:
raise ConfigError(
f"config.txt に必須項目が不足しています: {', '.join(missing)}"
)
return config