226 lines
7.0 KiB
Python
226 lines
7.0 KiB
Python
"""Konfigurationsverwaltung fuer SjAudioSearch.
|
|
|
|
Die Konfiguration wird aus einer TOML-Datei gelesen. Fehlende Werte werden
|
|
durch Defaults ersetzt, unbekannte Schluessel werden gesammelt und gemeldet,
|
|
statt stillschweigend ignoriert zu werden.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import tomllib
|
|
from dataclasses import dataclass, field, fields
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
PROJECT_ROOT = Path(__file__).resolve().parent.parent
|
|
DEFAULT_CONFIG_FILENAME = "config.toml"
|
|
ENV_CONFIG_PATH = "SJAUDIOSEARCH_CONFIG"
|
|
|
|
|
|
def _xdg_dir(env_var: str, fallback: str) -> Path:
|
|
"""Ermittelt ein XDG-Verzeichnis (Konfiguration bzw. Daten)."""
|
|
base = os.environ.get(env_var)
|
|
root = Path(base).expanduser() if base else Path.home() / fallback
|
|
return root / "sjaudiosearch"
|
|
|
|
|
|
# ~/.config/sjaudiosearch/config.toml (bzw. $XDG_CONFIG_HOME/...)
|
|
USER_CONFIG_DIR = _xdg_dir("XDG_CONFIG_HOME", ".config")
|
|
# Ablage fuer Datenbank und Logs, wenn das Programm systemweit installiert ist.
|
|
USER_DATA_DIR = _xdg_dir("XDG_DATA_HOME", ".local/share")
|
|
SYSTEM_CONFIG_PATH = Path("/etc/sjaudiosearch/config.toml")
|
|
|
|
|
|
@dataclass
|
|
class TranscriptionConfig:
|
|
model: str = "large-v3"
|
|
device: str = "cuda"
|
|
compute_type: str = "int8_float32"
|
|
language: str | None = "de"
|
|
beam_size: int = 5
|
|
word_timestamps: bool = True
|
|
vad_filter: bool = True
|
|
model_dir: str = "./data/models"
|
|
|
|
@property
|
|
def language_for_whisper(self) -> str | None:
|
|
"""Gibt ``None`` zurueck, wenn die Sprache automatisch erkannt werden soll."""
|
|
if self.language is None:
|
|
return None
|
|
value = self.language.strip().lower()
|
|
if value in {"", "auto", "none"}:
|
|
return None
|
|
return value
|
|
|
|
|
|
@dataclass
|
|
class SearchConfig:
|
|
default_limit: int = 50
|
|
default_min_score: float = 70.0
|
|
context_words: int = 50
|
|
window_before: int = 2
|
|
window_after: int = 4
|
|
candidate_limit: int = 4000
|
|
min_token_overlap: float = 0.34
|
|
exhaustive_fallback: bool = True
|
|
max_hits_per_file: int = 5
|
|
|
|
|
|
@dataclass
|
|
class ProcessingConfig:
|
|
workers: int = 1
|
|
resume: bool = True
|
|
compute_hash: str = "auto"
|
|
hash_max_size_mb: int = 4096
|
|
|
|
|
|
@dataclass
|
|
class DatabaseConfig:
|
|
path: str = "./data/sjaudiosearch.db"
|
|
|
|
|
|
@dataclass
|
|
class LoggingConfig:
|
|
level: str = "INFO"
|
|
path: str = "./logs/sjaudiosearch.log"
|
|
|
|
|
|
@dataclass
|
|
class Config:
|
|
transcription: TranscriptionConfig = field(default_factory=TranscriptionConfig)
|
|
search: SearchConfig = field(default_factory=SearchConfig)
|
|
processing: ProcessingConfig = field(default_factory=ProcessingConfig)
|
|
database: DatabaseConfig = field(default_factory=DatabaseConfig)
|
|
logging: LoggingConfig = field(default_factory=LoggingConfig)
|
|
|
|
# Metadaten zur Herkunft der Konfiguration
|
|
source_path: Path | None = None
|
|
base_dir: Path = field(default_factory=lambda: PROJECT_ROOT)
|
|
unknown_keys: list[str] = field(default_factory=list)
|
|
load_warnings: list[str] = field(default_factory=list)
|
|
|
|
def resolve_path(self, value: str) -> Path:
|
|
"""Loest einen (moeglicherweise relativen) Pfad gegen das Config-Verzeichnis auf."""
|
|
path = Path(value).expanduser()
|
|
if path.is_absolute():
|
|
return path
|
|
return (self.base_dir / path).resolve()
|
|
|
|
@property
|
|
def database_path(self) -> Path:
|
|
return self.resolve_path(self.database.path)
|
|
|
|
@property
|
|
def log_path(self) -> Path:
|
|
return self.resolve_path(self.logging.path)
|
|
|
|
@property
|
|
def model_path(self) -> Path:
|
|
return self.resolve_path(self.transcription.model_dir)
|
|
|
|
|
|
def _coerce(current: Any, value: Any) -> Any:
|
|
"""Konvertiert TOML-Werte tolerant auf den Typ des Defaults."""
|
|
if current is None:
|
|
return value
|
|
if isinstance(current, bool):
|
|
if isinstance(value, bool):
|
|
return value
|
|
if isinstance(value, str):
|
|
return value.strip().lower() in {"1", "true", "yes", "on"}
|
|
return bool(value)
|
|
if isinstance(current, int) and not isinstance(current, bool):
|
|
try:
|
|
return int(value)
|
|
except (TypeError, ValueError):
|
|
return current
|
|
if isinstance(current, float):
|
|
try:
|
|
return float(value)
|
|
except (TypeError, ValueError):
|
|
return current
|
|
if isinstance(current, str):
|
|
return str(value)
|
|
return value
|
|
|
|
|
|
def _apply_section(target: Any, table: dict[str, Any], section: str, config: Config) -> None:
|
|
known = {f.name: f for f in fields(target)}
|
|
for key, value in table.items():
|
|
if key not in known:
|
|
config.unknown_keys.append(f"{section}.{key}")
|
|
continue
|
|
current = getattr(target, key)
|
|
setattr(target, key, _coerce(current, value))
|
|
|
|
|
|
def _is_writable_dir(path: Path) -> bool:
|
|
try:
|
|
return path.is_dir() and os.access(path, os.W_OK)
|
|
except OSError:
|
|
return False
|
|
|
|
|
|
def find_config_path(explicit: str | Path | None = None) -> Path | None:
|
|
"""Ermittelt die zu verwendende Konfigurationsdatei."""
|
|
candidates: list[Path] = []
|
|
if explicit:
|
|
candidates.append(Path(explicit).expanduser())
|
|
env_value = os.environ.get(ENV_CONFIG_PATH)
|
|
if env_value:
|
|
candidates.append(Path(env_value).expanduser())
|
|
candidates.append(Path.cwd() / DEFAULT_CONFIG_FILENAME)
|
|
candidates.append(PROJECT_ROOT / DEFAULT_CONFIG_FILENAME)
|
|
candidates.append(USER_CONFIG_DIR / DEFAULT_CONFIG_FILENAME)
|
|
candidates.append(SYSTEM_CONFIG_PATH)
|
|
|
|
for candidate in candidates:
|
|
if candidate.is_file():
|
|
return candidate.resolve()
|
|
return None
|
|
|
|
|
|
def load_config(explicit: str | Path | None = None) -> Config:
|
|
"""Laedt die Konfiguration; ohne Datei werden die Defaults verwendet."""
|
|
path = find_config_path(explicit)
|
|
config = Config()
|
|
|
|
if path is None:
|
|
# Im Quellbaum (beschreibbar) bleiben Daten im Projekt, nach einer
|
|
# systemweiten Installation liegen sie im Benutzerverzeichnis.
|
|
config.base_dir = PROJECT_ROOT if _is_writable_dir(PROJECT_ROOT) else USER_DATA_DIR
|
|
if explicit:
|
|
config.load_warnings.append(
|
|
f"Konfigurationsdatei nicht gefunden: {explicit} - es gelten die Defaults."
|
|
)
|
|
return config
|
|
|
|
config.source_path = path
|
|
config.base_dir = path.parent
|
|
|
|
try:
|
|
with path.open("rb") as handle:
|
|
raw = tomllib.load(handle)
|
|
except (OSError, tomllib.TOMLDecodeError) as exc:
|
|
config.load_warnings.append(f"Konfiguration konnte nicht gelesen werden ({path}): {exc}")
|
|
return config
|
|
|
|
section_map = {
|
|
"transcription": config.transcription,
|
|
"search": config.search,
|
|
"processing": config.processing,
|
|
"database": config.database,
|
|
"logging": config.logging,
|
|
}
|
|
for section_name, section_obj in section_map.items():
|
|
table = raw.get(section_name)
|
|
if isinstance(table, dict):
|
|
_apply_section(section_obj, table, section_name, config)
|
|
|
|
for key in raw:
|
|
if key not in section_map:
|
|
config.unknown_keys.append(key)
|
|
|
|
return config
|