84 lines
3.0 KiB
Python
84 lines
3.0 KiB
Python
"""Tätigkeitsnachweis-Generator – Konfigurations-Loader."""
|
||
|
||
import configparser
|
||
import json
|
||
import fnmatch
|
||
from pathlib import Path
|
||
from typing import Optional
|
||
|
||
from .models import AppConfig, FieldSchema, OutputMapping
|
||
|
||
|
||
def load_settings(config_dir: Path, project_root: Path) -> AppConfig:
|
||
"""Lädt settings.ini und gibt ein AppConfig-Objekt zurück."""
|
||
ini_path = config_dir / "settings.ini"
|
||
if not ini_path.exists():
|
||
raise FileNotFoundError(f"settings.ini nicht gefunden: {ini_path}")
|
||
|
||
parser = configparser.ConfigParser()
|
||
parser.read(ini_path, encoding="utf-8")
|
||
|
||
paths = parser["paths"] if "paths" in parser else {}
|
||
output = parser["output"] if "output" in parser else {}
|
||
parsing = parser["parsing"] if "parsing" in parser else {}
|
||
|
||
return AppConfig(
|
||
input_dir=str(project_root / paths.get("input_dir", "input")),
|
||
template_dir=str(project_root / paths.get("template_dir", "template")),
|
||
output_dir=str(project_root / paths.get("output_dir", "output")),
|
||
filename_pattern=output.get("filename_pattern", "{last_name}_{first_name}_Tätigkeitsnachweis.xlsx"),
|
||
month_sheet_pattern=parsing.get("month_sheet_pattern", "* 20??"),
|
||
skip_empty_name=parsing.getboolean("skip_empty_name", True),
|
||
skip_empty_antragsnummer=parsing.getboolean("skip_empty_antragsnummer", True),
|
||
)
|
||
|
||
|
||
def load_input_schema(config_dir: Path) -> list[FieldSchema]:
|
||
"""Lädt input-fields.json."""
|
||
schema_path = config_dir / "input-fields.json"
|
||
if not schema_path.exists():
|
||
raise FileNotFoundError(f"input-fields.json nicht gefunden: {schema_path}")
|
||
|
||
with open(schema_path, "r", encoding="utf-8") as f:
|
||
data = json.load(f)
|
||
|
||
return [FieldSchema(**item) for item in data]
|
||
|
||
|
||
def load_output_mapping(config_dir: Path) -> list[OutputMapping]:
|
||
"""Lädt output-fields.json."""
|
||
mapping_path = config_dir / "output-fields.json"
|
||
if not mapping_path.exists():
|
||
raise FileNotFoundError(f"output-fields.json nicht gefunden: {mapping_path}")
|
||
|
||
with open(mapping_path, "r", encoding="utf-8") as f:
|
||
data = json.load(f)
|
||
|
||
return [OutputMapping(**item) for item in data]
|
||
|
||
|
||
def get_available_files(directory: Path, extension: str = ".xlsx") -> list[str]:
|
||
"""Listet alle Dateien mit der angegebenen Erweiterung im Verzeichnis auf."""
|
||
if not directory.exists():
|
||
return []
|
||
files = sorted(
|
||
f.name for f in directory.iterdir()
|
||
if f.is_file() and f.suffix.lower() == extension and not f.name.startswith("~")
|
||
)
|
||
return files
|
||
|
||
|
||
def get_available_input_files(config: AppConfig) -> list[str]:
|
||
"""Listet alle .xlsx-Dateien im input-Ordner."""
|
||
return get_available_files(Path(config.input_dir))
|
||
|
||
|
||
def get_available_templates(config: AppConfig) -> list[str]:
|
||
"""Listet alle .xlsx-Dateien im template-Ordner."""
|
||
return get_available_files(Path(config.template_dir))
|
||
|
||
|
||
def is_month_sheet(sheet_name: str, pattern: str) -> bool:
|
||
"""Prüft, ob ein Sheet-Name dem Monatsmuster entspricht."""
|
||
return fnmatch.fnmatch(sheet_name.strip(), pattern)
|