style: Format code for consistency and readability across TTS modules

This commit is contained in:
JSC
2025-09-21 18:05:20 +02:00
parent 50eeae4c62
commit d3b6e90262
11 changed files with 36 additions and 27 deletions

View File

@@ -3,4 +3,4 @@
from .base import TTSProvider
from .service import TTSService
__all__ = ["TTSProvider", "TTSService"]
__all__ = ["TTSProvider", "TTSService"]

View File

@@ -17,6 +17,7 @@ class TTSProvider(ABC):
Returns:
Audio data as bytes
"""
@abstractmethod
@@ -35,4 +36,4 @@ class TTSProvider(ABC):
@property
@abstractmethod
def file_extension(self) -> str:
"""Return the default file extension for this provider."""
"""Return the default file extension for this provider."""

View File

@@ -2,4 +2,4 @@
from .gtts import GTTSProvider
__all__ = ["GTTSProvider"]
__all__ = ["GTTSProvider"]

View File

@@ -31,6 +31,7 @@ class GTTSProvider(TTSProvider):
Returns:
MP3 audio data as bytes
"""
lang = options.get("lang", "en")
tld = options.get("tld", "com")
@@ -60,7 +61,7 @@ class GTTSProvider(TTSProvider):
"lv", "mk", "ml", "mr", "ms", "mt", "my", "ne", "nl", "no", "pa",
"pl", "pt", "pt-br", "pt-pt", "ro", "ru", "si", "sk", "sl", "sq",
"sr", "su", "sv", "sw", "ta", "te", "th", "tl", "tr", "uk", "ur",
"vi", "yo", "zh", "zh-cn", "zh-tw", "zu"
"vi", "yo", "zh", "zh-cn", "zh-tw", "zu",
]
def get_option_schema(self) -> dict[str, Any]:
@@ -70,11 +71,11 @@ class GTTSProvider(TTSProvider):
"type": "string",
"default": "en",
"description": "Language code",
"enum": self.get_supported_languages()
"enum": self.get_supported_languages(),
},
"slow": {
"type": "boolean",
"default": False,
"description": "Speak slowly"
}
}
"description": "Speak slowly",
},
}

View File

@@ -33,6 +33,7 @@ class TTSService:
Args:
session: Database session
"""
self.session = session
self.sound_repo = SoundRepository(session)
@@ -51,6 +52,7 @@ class TTSService:
Args:
provider: TTS provider instance
"""
self.providers[provider.name] = provider
@@ -83,6 +85,7 @@ class TTSService:
Raises:
ValueError: If provider not found or text too long
Exception: If request creation fails
"""
provider_not_found_msg = f"Provider '{provider}' not found"
if provider not in self.providers:
@@ -164,7 +167,7 @@ class TTSService:
pass
async def _generate_tts_sync(
self, text: str, provider: str, user_id: int, options: dict[str, Any]
self, text: str, provider: str, user_id: int, options: dict[str, Any],
) -> Sound:
"""Generate TTS using a synchronous approach."""
# Generate the audio using the provider (avoid async issues by doing it directly)
@@ -200,7 +203,7 @@ class TTSService:
# Create Sound record with proper metadata
sound = await self._create_sound_record_complete(
original_path, text, provider, user_id
original_path, text, provider, user_id,
)
# Normalize the sound
@@ -209,7 +212,7 @@ class TTSService:
return sound
async def get_user_tts_history(
self, user_id: int, limit: int = 50, offset: int = 0
self, user_id: int, limit: int = 50, offset: int = 0,
) -> list[TTS]:
"""Get TTS history for a user.
@@ -220,12 +223,13 @@ class TTSService:
Returns:
List of TTS records
"""
result = await self.tts_repo.get_by_user_id(user_id, limit, offset)
return list(result)
async def _create_sound_record(
self, audio_path: Path, text: str, provider: str, user_id: int, file_hash: str
self, audio_path: Path, text: str, provider: str, user_id: int, file_hash: str,
) -> Sound:
"""Create a Sound record for the TTS audio."""
# Get audio metadata
@@ -253,7 +257,7 @@ class TTSService:
return sound
async def _create_sound_record_simple(
self, audio_path: Path, text: str, provider: str, user_id: int
self, audio_path: Path, text: str, provider: str, user_id: int,
) -> Sound:
"""Create a Sound record for the TTS audio with minimal processing."""
# Create sound data with basic info
@@ -278,7 +282,7 @@ class TTSService:
return sound
async def _create_sound_record_complete(
self, audio_path: Path, text: str, provider: str, user_id: int
self, audio_path: Path, text: str, provider: str, user_id: int,
) -> Sound:
"""Create a Sound record for the TTS audio with complete metadata."""
# Get audio metadata
@@ -328,7 +332,7 @@ class TTSService:
if result["status"] == "error":
print(
f"Warning: Failed to normalize TTS sound {sound_id}: {result.get('error')}"
f"Warning: Failed to normalize TTS sound {sound_id}: {result.get('error')}",
)
except Exception as e:
@@ -364,7 +368,7 @@ class TTSService:
# Check ownership
if tts.user_id != user_id:
raise PermissionError(
"You don't have permission to delete this TTS generation"
"You don't have permission to delete this TTS generation",
)
# If there's an associated sound, delete it and its files

View File

@@ -18,7 +18,7 @@ class TTSProcessor:
def __init__(self) -> None:
"""Initialize the TTS processor."""
self.max_concurrent = getattr(settings, 'TTS_MAX_CONCURRENT', 3)
self.max_concurrent = getattr(settings, "TTS_MAX_CONCURRENT", 3)
self.running_tts: set[int] = set()
self.processing_lock = asyncio.Lock()
self.shutdown_event = asyncio.Event()
@@ -190,4 +190,4 @@ class TTSProcessor:
# Global TTS processor instance
tts_processor = TTSProcessor()
tts_processor = TTSProcessor()