Files
sufw-taetigkeitsnachweise/coldrun.py
T

125 lines
4.8 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.
#!/usr/bin/env python3
"""Cold-Run Tätigkeitsnachweis-Generator.
Nimmt automatisch die erste Input-Datei und das erste Template,
erzeugt einen Tätigkeitsnachweis für die erste gelistete Person
und öffnet das Ergebnis (macOS) – ohne GUI, ohne Fragen.
"""
import subprocess
import sys
from pathlib import Path
# Projekt-Root
PROJECT_ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(PROJECT_ROOT))
from src.config import (
load_settings,
load_input_schema,
load_output_mapping,
get_available_input_files,
get_available_templates,
)
from src.parser import parse_input_file, group_by_employee, get_unique_employees
from src.validator import validate_template
from src.writer import generate_activity_reports
def main():
print("=" * 50)
print(" Tätigkeitsnachweis-Generator (Cold Run)")
print("=" * 50)
print()
# ── Konfiguration laden ──────────────────────────────────────────
config_dir = PROJECT_ROOT / "config"
config = load_settings(config_dir, PROJECT_ROOT)
config.input_schema = load_input_schema(config_dir)
config.output_mapping = load_output_mapping(config_dir)
# ── Erste Input-Datei ermitteln ───────────────────────────────────
input_files = get_available_input_files(config)
if not input_files:
print("[FEHLER] Keine .xlsx-Dateien im input/-Ordner gefunden.")
return 1
input_file = input_files[0]
input_path = Path(config.input_dir) / input_file
print(f"[INFO] Input-Datei: {input_file}")
# ── Erstes Template ermitteln ─────────────────────────────────────
template_files = get_available_templates(config)
if not template_files:
print("[FEHLER] Keine .pdf-Dateien im template/-Ordner gefunden.")
return 1
template_file = template_files[0]
template_path = Path(config.template_dir) / template_file
print(f"[INFO] Template: {template_file}")
# ── Template validieren ───────────────────────────────────────────
print()
print("[INFO] Validiere PDF-Template…")
tpl_result = validate_template(template_path, config.output_mapping)
if not tpl_result.is_valid:
for err in tpl_result.errors:
print(f"[FEHLER] {err}")
return 1
found = tpl_result.info.get("found_fields", "?")
total = tpl_result.info.get("total_fields", "?")
print(f"[OK] {found}/{total} Platzhalter im Template gefunden.")
# ── Input parsen ──────────────────────────────────────────────────
print()
print("[INFO] Parse Input-Datei…")
records = parse_input_file(
filepath=input_path,
schema=config.input_schema,
month_sheet_pattern=config.month_sheet_pattern,
skip_empty_name=config.skip_empty_name,
skip_empty_antragsnummer=config.skip_empty_antragsnummer,
)
employees = get_unique_employees(records)
print(f"[INFO] {len(records)} Einträge für {len(employees)} Personen gefunden.")
# ── Erste Person ermitteln ────────────────────────────────────────
grouped = group_by_employee(records)
first_person = sorted(grouped.keys())[0]
first_records = grouped[first_person]
print(f"[INFO] Erste Person: {first_person} ({len(first_records)} Monate)")
# ── Reports generieren ────────────────────────────────────────────
print()
print("[INFO] Generiere Tätigkeitsnachweis…")
single_person = {first_person: first_records}
output_dir = Path(config.output_dir)
generated = generate_activity_reports(
employee_records=single_person,
template_path=template_path,
mapping=config.output_mapping,
output_dir=output_dir,
filename_pattern=config.filename_pattern,
)
output_file = generated[0]
print(f"[OK] Erstellt: {output_file.name}")
print(f" Pfad: {output_file}")
# ── Datei öffnen (macOS) ──────────────────────────────────────────
if sys.platform == "darwin":
print()
print("[INFO] Öffne Datei im Finder…")
subprocess.run(["open", str(output_file)], check=False)
print()
print("=" * 50)
print(" Fertig.")
print("=" * 50)
return 0
if __name__ == "__main__":
sys.exit(main())