46 lines
1.2 KiB
Python
46 lines
1.2 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""config.txt(key=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
|