Update root __init__.py:

- Add module reloading to ensure exceptions are available
- Import and re-export all necessary exceptions
- Maintain existing setup and teardown functions
- Add version information
- Update __all__ list with all exports
This commit is contained in:
pacnpal
2024-11-17 00:07:43 +00:00
parent e08ec2fa84
commit 824b8870e1

View File

@@ -1,14 +1,37 @@
"""VideoArchiver cog for Red-DiscordBot""" """VideoArchiver cog for Red-DiscordBot"""
import sys
import asyncio import asyncio
import logging import logging
import importlib
from typing import Optional from typing import Optional
from redbot.core.bot import Red from redbot.core.bot import Red
# Remove modules to force reload
modules_to_reload = [
'videoarchiver.utils.exceptions',
'videoarchiver.utils'
]
for module in modules_to_reload:
if module in sys.modules:
del sys.modules[module]
# Import and reload utils
from . import utils
importlib.reload(utils)
from .core.base import VideoArchiver from .core.base import VideoArchiver
from .core.initialization import initialize_cog, init_callback from .core.initialization import initialize_cog, init_callback
from .core.cleanup import cleanup_resources from .core.cleanup import cleanup_resources
from .exceptions import ProcessingError from .utils.exceptions import (
VideoArchiverError,
CommandError,
EventError,
CogError,
ErrorContext,
ErrorSeverity,
ProcessingError
)
from .database import VideoArchiveDB from .database import VideoArchiveDB
from .ffmpeg import FFmpegManager from .ffmpeg import FFmpegManager
from .queue import EnhancedVideoQueueManager from .queue import EnhancedVideoQueueManager
@@ -21,55 +44,75 @@ logger = logging.getLogger("VideoArchiver")
# Track initialization task # Track initialization task
_init_task: Optional[asyncio.Task] = None _init_task: Optional[asyncio.Task] = None
# Version information
__version__ = "1.0.0"
__author__ = "VideoArchiver Team"
__description__ = "Video archiving cog for Red-DiscordBot"
async def setup(bot: Red) -> None: async def setup(bot: Red) -> None:
"""Load VideoArchiver with proper initialization.""" """Load VideoArchiver with proper initialization."""
try: try:
# Create cog instance # Create cog instance
cog = VideoArchiver(bot) cog = VideoArchiver(bot)
# Start initialization in background # Start initialization in background
global _init_task global _init_task
_init_task = asyncio.create_task(initialize_cog(cog)) _init_task = asyncio.create_task(initialize_cog(cog))
_init_task.add_done_callback(lambda t: init_callback(cog, t)) _init_task.add_done_callback(lambda t: init_callback(cog, t))
# Add cog to bot # Add cog to bot
await bot.add_cog(cog) await bot.add_cog(cog)
logger.info("VideoArchiver cog loaded successfully") logger.info("VideoArchiver cog loaded successfully")
except Exception as e: except Exception as e:
logger.error(f"Failed to load VideoArchiver: {str(e)}") logger.error(f"Failed to load VideoArchiver: {str(e)}")
if _init_task and not _init_task.done(): if _init_task and not _init_task.done():
_init_task.cancel() _init_task.cancel()
raise raise
async def teardown(bot: Red) -> None: async def teardown(bot: Red) -> None:
"""Clean up when unloading.""" """Clean up when unloading."""
try: try:
# Cancel initialization if still running # Cancel initialization if still running
if _init_task and not _init_task.done(): if _init_task and not _init_task.done():
_init_task.cancel() _init_task.cancel()
# Remove cog and clean up resources # Remove cog and clean up resources
if "VideoArchiver" in bot.cogs: if "VideoArchiver" in bot.cogs:
cog = bot.get_cog("VideoArchiver") cog = bot.get_cog("VideoArchiver")
if cog: if cog:
await cleanup_resources(cog) await cleanup_resources(cog)
await bot.remove_cog("VideoArchiver") await bot.remove_cog("VideoArchiver")
logger.info("VideoArchiver cog unloaded successfully") logger.info("VideoArchiver cog unloaded successfully")
except Exception as e: except Exception as e:
logger.error(f"Error during teardown: {str(e)}") logger.error(f"Error during teardown: {str(e)}")
raise raise
__all__ = [ __all__ = [
'VideoArchiver', # Core classes
'VideoArchiveDB', "VideoArchiver",
'FFmpegManager', "VideoArchiveDB",
'EnhancedVideoQueueManager', "FFmpegManager",
'VideoProcessor', "EnhancedVideoQueueManager",
'ConfigManager', "VideoProcessor",
'UpdateChecker', "ConfigManager",
'ProcessingError' "UpdateChecker",
# Base exceptions
"VideoArchiverError",
"CommandError",
"EventError",
"CogError",
"ErrorContext",
"ErrorSeverity",
"ProcessingError",
# Setup functions
"setup",
"teardown"
] ]