feat: config.txt読込モジュールを追加

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Kenichiro NOGI 2026-08-02 10:08:02 +09:00
parent b8b8f4d93c
commit 4ae53c2725
2 changed files with 100 additions and 0 deletions

View File

@ -0,0 +1,45 @@
# -*- 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

View File

@ -0,0 +1,55 @@
import pytest
from pathlib import Path
from config_loader import ConfigError, load_config
VALID_CONTENT = """\
スクリプトフォルダ=スクリプト
テンプレートフォルダ=テンプレート
スキャンフォルダ=スキャン
アウトプットフォルダ=アウトプット
成功フォルダ=成功
失敗フォルダ=失敗
ログフォルダ=ログ
margin=0.10
DPI=300
ファイル安定待ち秒=1
ファイル安定待ちリトライ回数=5
"""
def test_load_config_missing_file_raises(tmp_path):
missing_path = tmp_path / "config.txt"
with pytest.raises(ConfigError):
load_config(missing_path)
def test_load_config_parses_all_keys(tmp_path):
config_path = tmp_path / "config.txt"
config_path.write_text(VALID_CONTENT, encoding="utf-8")
config = load_config(config_path)
assert config["スクリプトフォルダ"] == "スクリプト"
assert config["テンプレートフォルダ"] == "テンプレート"
assert config["margin"] == "0.10"
assert config["ファイル安定待ちリトライ回数"] == "5"
def test_load_config_missing_required_key_raises(tmp_path):
config_path = tmp_path / "config.txt"
config_path.write_text("スクリプトフォルダ=スクリプト\n", encoding="utf-8")
with pytest.raises(ConfigError):
load_config(config_path)
def test_load_config_ignores_blank_lines_and_comments(tmp_path):
config_path = tmp_path / "config.txt"
content = "# comment\n\n" + VALID_CONTENT
config_path.write_text(content, encoding="utf-8")
config = load_config(config_path)
assert config["スクリプトフォルダ"] == "スクリプト"