fix: escape special characters in save_config TOML output

This commit is contained in:
2026-04-10 21:02:36 +02:00
parent 1637314c1d
commit 484df3b9fd
2 changed files with 18 additions and 5 deletions
+10 -5
View File
@@ -63,17 +63,22 @@ def load_config(path: Path = DEFAULT_CONFIG_PATH) -> Config:
return config
def _toml_str(value: str) -> str:
"""Escaped einen String-Wert für TOML (verhindert ungültiges TOML bei Sonderzeichen)."""
return value.replace("\\", "\\\\").replace('"', '\\"')
def save_config(config: Config, path: Path = DEFAULT_CONFIG_PATH) -> None:
"""Schreibt Config als TOML-Datei. Erstellt Verzeichnisse bei Bedarf."""
path.parent.mkdir(parents=True, exist_ok=True)
content = (
f'[hotkey]\nkey = "{config.hotkey}"\n\n'
f'[whisper]\nmodel = "{config.whisper_model}"\n'
f'language = "{config.language}"\n'
f'compute_type = "{config.compute_type}"\n\n'
f'[hotkey]\nkey = "{_toml_str(config.hotkey)}"\n\n'
f'[whisper]\nmodel = "{_toml_str(config.whisper_model)}"\n'
f'language = "{_toml_str(config.language)}"\n'
f'compute_type = "{_toml_str(config.compute_type)}"\n\n'
f'[audio]\nsample_rate = {config.sample_rate}\n'
f'channels = {config.channels}\n'
f'min_duration = {config.min_duration}\n'
f'device = "{config.microphone}"\n'
f'device = "{_toml_str(config.microphone)}"\n'
)
path.write_text(content, encoding="utf-8")