fix: escape special characters in save_config TOML output
This commit is contained in:
@@ -105,3 +105,11 @@ class TestSaveConfig:
|
|||||||
path = tmp_path / "subdir" / "config.toml"
|
path = tmp_path / "subdir" / "config.toml"
|
||||||
save_config(Config(), path)
|
save_config(Config(), path)
|
||||||
assert path.exists()
|
assert path.exists()
|
||||||
|
|
||||||
|
def test_save_handles_special_characters_in_microphone(self, tmp_path):
|
||||||
|
from whisper_local.config import save_config
|
||||||
|
path = tmp_path / "config.toml"
|
||||||
|
config = Config(microphone='USB "Pro" Mic')
|
||||||
|
save_config(config, path)
|
||||||
|
loaded = load_config(path)
|
||||||
|
assert loaded.microphone == 'USB "Pro" Mic'
|
||||||
|
|||||||
+10
-5
@@ -63,17 +63,22 @@ def load_config(path: Path = DEFAULT_CONFIG_PATH) -> Config:
|
|||||||
return 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:
|
def save_config(config: Config, path: Path = DEFAULT_CONFIG_PATH) -> None:
|
||||||
"""Schreibt Config als TOML-Datei. Erstellt Verzeichnisse bei Bedarf."""
|
"""Schreibt Config als TOML-Datei. Erstellt Verzeichnisse bei Bedarf."""
|
||||||
path.parent.mkdir(parents=True, exist_ok=True)
|
path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
content = (
|
content = (
|
||||||
f'[hotkey]\nkey = "{config.hotkey}"\n\n'
|
f'[hotkey]\nkey = "{_toml_str(config.hotkey)}"\n\n'
|
||||||
f'[whisper]\nmodel = "{config.whisper_model}"\n'
|
f'[whisper]\nmodel = "{_toml_str(config.whisper_model)}"\n'
|
||||||
f'language = "{config.language}"\n'
|
f'language = "{_toml_str(config.language)}"\n'
|
||||||
f'compute_type = "{config.compute_type}"\n\n'
|
f'compute_type = "{_toml_str(config.compute_type)}"\n\n'
|
||||||
f'[audio]\nsample_rate = {config.sample_rate}\n'
|
f'[audio]\nsample_rate = {config.sample_rate}\n'
|
||||||
f'channels = {config.channels}\n'
|
f'channels = {config.channels}\n'
|
||||||
f'min_duration = {config.min_duration}\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")
|
path.write_text(content, encoding="utf-8")
|
||||||
|
|||||||
Reference in New Issue
Block a user