91 lines
3 KiB
Python
91 lines
3 KiB
Python
"""Пути файловой системы приложения."""
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
import sys
|
||
from dataclasses import dataclass
|
||
from pathlib import Path
|
||
|
||
|
||
@dataclass(frozen=True, slots=True)
|
||
class AppPaths:
|
||
"""Все пути приложения в одном месте.
|
||
|
||
Args:
|
||
root: Корневая директория (~/.dictate).
|
||
config_file: Файл конфигурации.
|
||
usage_db: База данных статистики.
|
||
dictionary_file: Словарь подсказок для Whisper.
|
||
custom_sounds_dir: Директория пользовательских звуков.
|
||
log_file: Файл лога.
|
||
pid_file: PID-файл для single-instance lock.
|
||
tmp_dir: Временные WAV-файлы записей.
|
||
"""
|
||
|
||
root: Path
|
||
config_file: Path
|
||
usage_db: Path
|
||
dictionary_file: Path
|
||
custom_sounds_dir: Path
|
||
log_file: Path
|
||
pid_file: Path
|
||
tmp_dir: Path
|
||
|
||
@classmethod
|
||
def default(cls) -> AppPaths:
|
||
"""Создаёт AppPaths со стандартными путями в домашней директории.
|
||
|
||
Returns:
|
||
AppPaths с путями в ~/.dictate/.
|
||
"""
|
||
override = os.environ.get("VOICE_DICTATION_HOME")
|
||
beta = bool(getattr(sys, "frozen", False)) and any(
|
||
parent.name == "Voice Dictation Beta.app" for parent in Path(sys.executable).parents
|
||
)
|
||
root = (
|
||
Path(override).expanduser()
|
||
if override
|
||
else Path.home() / (".dictate-beta" if beta else ".dictate")
|
||
)
|
||
return cls(
|
||
root=root,
|
||
config_file=root / "config.json",
|
||
usage_db=root / "usage.db",
|
||
dictionary_file=root / "dictionary.txt",
|
||
custom_sounds_dir=root / "sounds",
|
||
log_file=root / "app.log",
|
||
pid_file=root / "dictate.pid",
|
||
tmp_dir=root / "tmp",
|
||
)
|
||
|
||
def ensure_exists(self) -> AppPaths:
|
||
"""Создаёт все необходимые директории.
|
||
|
||
Returns:
|
||
self для удобства цепочки вызовов.
|
||
"""
|
||
for directory in (
|
||
self.root,
|
||
self.custom_sounds_dir,
|
||
self.tmp_dir,
|
||
self.sessions_dir,
|
||
self.bin_dir,
|
||
):
|
||
directory.mkdir(parents=True, exist_ok=True)
|
||
return self
|
||
|
||
@property
|
||
def sessions_dir(self) -> Path:
|
||
"""Постоянные аудиосессии и промежуточные результаты транскрипции."""
|
||
return self.root / "sessions"
|
||
|
||
@property
|
||
def bin_dir(self) -> Path:
|
||
"""Downloaded helper binaries, preserved across application updates."""
|
||
return self.root / "bin"
|
||
|
||
@property
|
||
def models_dir(self) -> Path:
|
||
"""Downloaded models are independent of the application bundle."""
|
||
return self.root / "models"
|