| 1 | from __future__ import annotations |
| 2 | |
| 3 | import json |
| 4 | |
| 5 | from helpers import files, plugins |
| 6 | |
| 7 | |
| 8 | PLUGIN_NAME = "_kokoro_tts" |
| 9 | LEGACY_SETTINGS_FILE = files.get_abs_path("usr/settings.json") |
| 10 | |
| 11 | |
| 12 | def ensure_migrated() -> bool: |
| 13 | legacy_settings = _read_legacy_settings() |
| 14 | legacy_enabled = _coerce_bool(legacy_settings.get("tts_kokoro"), default=True) |
| 15 | if legacy_enabled or _has_explicit_toggle(): |
| 16 | return False |
| 17 | |
| 18 | disabled_path = plugins.determine_plugin_asset_path( |
| 19 | PLUGIN_NAME, "", "", plugins.DISABLED_FILE_NAME |
| 20 | ) |
| 21 | files.write_file(disabled_path, "") |
| 22 | plugins.clear_plugin_cache([PLUGIN_NAME]) |
| 23 | return True |
| 24 | |
| 25 | |
| 26 | def _has_explicit_toggle() -> bool: |
| 27 | for root in plugins.get_plugin_roots(PLUGIN_NAME): |
| 28 | if files.exists(files.get_abs_path(root, plugins.ENABLED_FILE_NAME)): |
| 29 | return True |
| 30 | if files.exists(files.get_abs_path(root, plugins.DISABLED_FILE_NAME)): |
| 31 | return True |
| 32 | return False |
| 33 | |
| 34 | |
| 35 | def _read_legacy_settings() -> dict: |
| 36 | if not files.exists(LEGACY_SETTINGS_FILE): |
| 37 | return {} |
| 38 | |
| 39 | try: |
| 40 | return json.loads(files.read_file(LEGACY_SETTINGS_FILE)) |
| 41 | except Exception: |
| 42 | return {} |
| 43 | |
| 44 | |
| 45 | def _coerce_bool(value: object, default: bool) -> bool: |
| 46 | if isinstance(value, bool): |
| 47 | return value |
| 48 | if isinstance(value, str): |
| 49 | lowered = value.strip().lower() |
| 50 | if lowered in {"true", "1", "yes", "on"}: |
| 51 | return True |
| 52 | if lowered in {"false", "0", "no", "off"}: |
| 53 | return False |
| 54 | return default |