From f2ae130e2d2250a20047d393b862dc1ee777e2ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=3D=3DTIM=2E=C2=A9=2EB=20=20=3D=3D?= Date: Sat, 19 Sep 2026 00:05:01 +0200 Subject: [PATCH] refactor: extract _save_config_safely helper in TTS window Replace six identical try/except/pass config-save blocks in TtsWindow with a module-level helper next to _safe_unlink(). The helper is a module function rather than a method because tests call TtsWindow._ensure_openai_consent and _revert_provider_to_piper unbound with SimpleNamespace fakes. _on_speed_changed is left as is: its try also covers the tts_speed property setter, which can raise. Co-Authored-By: Claude Sonnet 5 --- app/tts_window.py | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/app/tts_window.py b/app/tts_window.py index 0add820..e7ff889 100644 --- a/app/tts_window.py +++ b/app/tts_window.py @@ -153,6 +153,14 @@ def _safe_unlink(path: Optional[str]) -> None: pass +def _save_config_safely(config) -> None: + """Persist the config; write errors are deliberately ignored.""" + try: + config.save() + except Exception: + pass + + def _make_tts_runtime_job_dir() -> Path: return Path(tempfile.mkdtemp(prefix=f"blitztext-tts-{uuid4().hex}-", dir=TTS_RUNTIME_DIR)) @@ -559,10 +567,7 @@ def _ensure_openai_consent(self) -> bool: if answer != QMessageBox.StandardButton.Yes: return False self._config.tts_openai_consent = True - try: - self._config.save() - except Exception: - pass + _save_config_safely(self._config) return True def _revert_provider_to_piper(self) -> None: @@ -571,10 +576,7 @@ def _revert_provider_to_piper(self) -> None: self._provider_combo.blockSignals(False) if self._config.tts_provider != "piper": self._config.tts_provider = "piper" - try: - self._config.save() - except Exception: - pass + _save_config_safely(self._config) @pyqtSlot(int) def _on_provider_changed(self, _idx: int) -> None: @@ -586,10 +588,7 @@ def _on_provider_changed(self, _idx: int) -> None: return if self._config.tts_provider != provider: self._config.tts_provider = provider - try: - self._config.save() - except Exception: - pass + _save_config_safely(self._config) self._populate_voices() self._refresh_status_hint() @@ -600,27 +599,18 @@ def _on_voice_changed(self, _idx: int) -> None: if provider == "openai": if isinstance(voice, str) and voice and self._config.tts_openai_voice != voice: self._config.tts_openai_voice = voice - try: - self._config.save() - except Exception: - pass + _save_config_safely(self._config) else: if isinstance(voice, str) and voice and self._config.tts_voice != voice: self._config.tts_voice = voice - try: - self._config.save() - except Exception: - pass + _save_config_safely(self._config) self._refresh_status_hint() def _on_model_changed(self) -> None: model = self._model_edit.text().strip() or OPENAI_TTS_MODEL_DEFAULT if self._config.tts_openai_model != model: self._config.tts_openai_model = model - try: - self._config.save() - except Exception: - pass + _save_config_safely(self._config) self._refresh_status_hint() @pyqtSlot(int)