Using TYPE_CHECKING for type hints

Moving runtime imports to appropriate locations
Using string literal type annotations
Importing shared utilities through the utils package
This commit is contained in:
pacnpal
2024-11-17 21:24:49 +00:00
parent e997c6f6b9
commit 439cf5ff07
5 changed files with 32 additions and 17 deletions

View File

@@ -15,10 +15,13 @@ from typing import (
Callable, Callable,
Awaitable, Awaitable,
Tuple, Tuple,
TYPE_CHECKING,
) )
from datetime import datetime, timedelta from datetime import datetime, timedelta
if TYPE_CHECKING:
from ..processor.queue_handler import QueueHandler from ..processor.queue_handler import QueueHandler
from ..ffmpeg.ffmpeg_manager import FFmpegManager from ..ffmpeg.ffmpeg_manager import FFmpegManager
from ..utils.exceptions import CleanupError from ..utils.exceptions import CleanupError
@@ -192,7 +195,7 @@ class CleanupManager:
def __init__( def __init__(
self, self,
queue_handler: QueueHandler, queue_handler: "QueueHandler",
ffmpeg_mgr: Optional[FFmpegManager] = None, ffmpeg_mgr: Optional[FFmpegManager] = None,
strategy: CleanupStrategy = CleanupStrategy.NORMAL, strategy: CleanupStrategy = CleanupStrategy.NORMAL,
) -> None: ) -> None:

View File

@@ -3,7 +3,7 @@
import asyncio import asyncio
import logging import logging
from datetime import datetime from datetime import datetime
from typing import Any, ClassVar, Dict, List, Optional, Tuple from typing import Any, ClassVar, Dict, List, Optional, Tuple, TYPE_CHECKING
import discord # type: ignore import discord # type: ignore
from discord.ext import commands # type: ignore from discord.ext import commands # type: ignore
@@ -15,14 +15,15 @@ from ..core.types import (
ProcessorState, ProcessorState,
ComponentStatus, ComponentStatus,
) )
from ..processor.cleanup_manager import CleanupManager, CleanupStrategy
from ..processor.constants import REACTIONS from ..processor.constants import REACTIONS
from ..processor.message_handler import MessageHandler
from ..processor.queue_handler import QueueHandler
from ..processor.status_display import StatusDisplay
from ..utils import progress_tracker from ..utils import progress_tracker
from ..utils.exceptions import ProcessorError from ..utils.exceptions import ProcessorError
if TYPE_CHECKING:
from ..processor.cleanup_manager import CleanupManager
from ..processor.message_handler import MessageHandler
from ..processor.queue_handler import QueueHandler
logger = logging.getLogger("VideoArchiver") logger = logging.getLogger("VideoArchiver")
@@ -171,10 +172,15 @@ class VideoProcessor(IComponent):
self.health_monitor = HealthMonitor(self) self.health_monitor = HealthMonitor(self)
try: try:
# Import handlers here to avoid circular imports
from ..processor.queue_handler import QueueHandler
from ..processor.message_handler import MessageHandler
from ..processor.cleanup_manager import CleanupManager, CleanupStrategy
# Initialize handlers # Initialize handlers
self.queue_handler = QueueHandler(bot, config_manager, components) self.queue_handler: "QueueHandler" = QueueHandler(bot, config_manager, components)
self.message_handler = MessageHandler(bot, config_manager, queue_manager) self.message_handler: "MessageHandler" = MessageHandler(bot, config_manager, queue_manager)
self.cleanup_manager = CleanupManager( self.cleanup_manager: "CleanupManager" = CleanupManager(
self.queue_handler, ffmpeg_mgr, CleanupStrategy.NORMAL self.queue_handler, ffmpeg_mgr, CleanupStrategy.NORMAL
) )
@@ -292,6 +298,9 @@ class VideoProcessor(IComponent):
# Get active operations # Get active operations
active_ops = self.operation_tracker.get_active_operations() active_ops = self.operation_tracker.get_active_operations()
# Import StatusDisplay here to avoid circular imports
from ..processor.status_display import StatusDisplay
# Create and send status embed # Create and send status embed
embed = await StatusDisplay.create_queue_status_embed( embed = await StatusDisplay.create_queue_status_embed(
queue_status, active_ops queue_status, active_ops

View File

@@ -4,7 +4,7 @@ import asyncio
import logging import logging
from datetime import datetime, timedelta from datetime import datetime, timedelta
from enum import auto, Enum from enum import auto, Enum
from typing import Any, ClassVar, Dict, List, Optional, Set, Tuple, TypedDict from typing import Any, ClassVar, Dict, List, Optional, Set, Tuple, TypedDict, TYPE_CHECKING
import discord # type: ignore import discord # type: ignore
from discord.ext import commands # type: ignore from discord.ext import commands # type: ignore
@@ -14,9 +14,11 @@ from ..processor.constants import REACTIONS
from ..processor.message_validator import MessageValidator, ValidationError from ..processor.message_validator import MessageValidator, ValidationError
from ..processor.url_extractor import URLExtractor, URLMetadata from ..processor.url_extractor import URLExtractor, URLMetadata
from ..queue.types import QueuePriority from ..queue.types import QueuePriority
from ..queue.manager import EnhancedVideoQueueManager
from ..utils.exceptions import MessageHandlerError from ..utils.exceptions import MessageHandlerError
if TYPE_CHECKING:
from ..queue.manager import EnhancedVideoQueueManager
logger = logging.getLogger("VideoArchiver") logger = logging.getLogger("VideoArchiver")
@@ -207,7 +209,7 @@ class MessageHandler:
self, self,
bot: discord.Client, bot: discord.Client,
config_manager: ConfigManager, config_manager: ConfigManager,
queue_manager: EnhancedVideoQueueManager, queue_manager: "EnhancedVideoQueueManager",
) -> None: ) -> None:
self.bot = bot self.bot = bot
self.config_manager = config_manager self.config_manager = config_manager

View File

@@ -8,7 +8,7 @@ from typing import Optional, Dict, Any, List, Tuple, Set, TypedDict, ClassVar, C
from datetime import datetime from datetime import datetime
import discord # type: ignore import discord # type: ignore
from ..utils import progress_tracker from .. import utils # Import the utils package
from ..database.video_archive_db import VideoArchiveDB from ..database.video_archive_db import VideoArchiveDB
from ..utils.download_manager import DownloadManager from ..utils.download_manager import DownloadManager
from ..utils.message_manager import MessageManager from ..utils.message_manager import MessageManager
@@ -378,7 +378,7 @@ class QueueHandler:
return return
# Update progress tracking # Update progress tracking
progress_tracker.update_download_progress( utils.progress_tracker.update_download_progress(
url, url,
{ {
"percent": progress, "percent": progress,
@@ -425,9 +425,9 @@ class QueueHandler:
download_task, timeout=self.DOWNLOAD_TIMEOUT download_task, timeout=self.DOWNLOAD_TIMEOUT
) )
if success: if success:
progress_tracker.complete_download(url) utils.progress_tracker.complete_download(url)
else: else:
progress_tracker.increment_download_retries(url) utils.progress_tracker.increment_download_retries(url)
return success, file_path, error return success, file_path, error
except asyncio.TimeoutError: except asyncio.TimeoutError:

View File

@@ -1,6 +1,7 @@
"""Enhanced queue manager for video processing""" """Enhanced queue manager for video processing"""
import asyncio import asyncio
from enum import Enum
import logging import logging
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import Optional, Tuple, Dict, Any, List, Set, Callable from typing import Optional, Tuple, Dict, Any, List, Set, Callable