6f7cadfc63
- #1: check_preflight() prüft beim Start tesseract + gs, wirft PreflightError. CLI endet mit Exit 2 statt grün zu bleiben. - #2: run_once() gibt Anzahl fehlgeschlagener PDFs zurück, CLI endet mit Exit 1 wenn mindestens eine Datei scheiterte. - pytest-Suite mit 11 Tests für beide Szenarien - ocrmypdf-Import lazy in processor.py (Tests ohne ocrmypdf möglich) Closes #1, #2 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
64 lines
1.7 KiB
Python
64 lines
1.7 KiB
Python
"""CLI-Entrypoint."""
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import logging
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from . import __version__
|
|
from .config import load_config
|
|
from .service import HotfolderService, PreflightError
|
|
|
|
|
|
def _setup_logging(level: str) -> None:
|
|
logging.basicConfig(
|
|
level=getattr(logging, level.upper(), logging.INFO),
|
|
format="%(asctime)s %(levelname)-7s %(name)s: %(message)s",
|
|
datefmt="%Y-%m-%d %H:%M:%S",
|
|
)
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(
|
|
prog="pdf-ocr-hotfolder",
|
|
description="Wandelt eingehende PDFs per OCR in durchsuchbare PDFs um.",
|
|
)
|
|
parser.add_argument("--config", "-c", default="/etc/pdf-ocr-hotfolder/config.toml",
|
|
help="Pfad zur Konfigurationsdatei (TOML)")
|
|
parser.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
|
parser.add_argument("--once", action="store_true",
|
|
help="Nur bestehende Dateien verarbeiten und beenden")
|
|
args = parser.parse_args()
|
|
|
|
cfg_path = Path(args.config)
|
|
if not cfg_path.exists():
|
|
print(f"Config nicht gefunden: {cfg_path}", file=sys.stderr)
|
|
return 2
|
|
|
|
cfg = load_config(cfg_path)
|
|
_setup_logging(cfg.log_level)
|
|
|
|
service = HotfolderService(cfg)
|
|
|
|
if args.once:
|
|
try:
|
|
errors = service.run_once()
|
|
except PreflightError as e:
|
|
print(f"FEHLER: {e}", file=sys.stderr)
|
|
return 2
|
|
return 1 if errors > 0 else 0
|
|
|
|
try:
|
|
service.run()
|
|
except PreflightError as e:
|
|
print(f"FEHLER: {e}", file=sys.stderr)
|
|
return 2
|
|
except KeyboardInterrupt:
|
|
pass
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|