"""ChefSystem - Main Entry Point. This module initializes and runs the ChefSystem application. """ import sys import json import logging from pathlib import Path from PyQt6.QtWidgets import QApplication, QMessageBox from PyQt6.QtCore import Qt from src.database.db_manager import DatabaseManager from src.ui.main_window import MainWindow # Configure logging logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ) logger = logging.getLogger(__name__) def load_config() -> dict: """Load application configuration from settings.json. Returns: dict: Configuration dictionary. """ config_path = Path('config/settings.json') try: with open(config_path, 'r') as f: config = json.load(f) logger.info(f"Configuration loaded from {config_path}") return config except FileNotFoundError: logger.error(f"Configuration file not found: {config_path}") return { "database_path": "database/chef.db", "outputs_directory": "outputs/", "window_geometry": None, "recent_tags": [] } except json.JSONDecodeError as e: logger.error(f"Error parsing configuration file: {e}") return { "database_path": "database/chef.db", "outputs_directory": "outputs/", "window_geometry": None, "recent_tags": [] } def initialize_database(db_path: str) -> DatabaseManager: """Initialize the database connection and schema. Args: db_path: Path to the database file. Returns: DatabaseManager: Initialized database manager. """ try: db = DatabaseManager(db_path) db.init_database() logger.info("Database initialized successfully") return db except Exception as e: logger.error(f"Failed to initialize database: {e}", exc_info=True) raise def exception_hook(exctype, value, traceback): """Global exception handler for uncaught exceptions. Args: exctype: Exception type. value: Exception value. traceback: Exception traceback. """ logger.error("Uncaught exception", exc_info=(exctype, value, traceback)) sys.__excepthook__(exctype, value, traceback) def main() -> int: """Initialize and run the ChefSystem application. Returns: int: Exit code of the application. """ # Set global exception hook sys.excepthook = exception_hook # Create QApplication app = QApplication(sys.argv) app.setApplicationName("ChefSystem") app.setOrganizationName("ChefSystem") app.setApplicationDisplayName("ChefSystem - AI Prompt Manager") try: # Load configuration config = load_config() logger.info("Starting ChefSystem application") # Initialize database db = initialize_database(config["database_path"]) # Create and show main window main_window = MainWindow(db, config) main_window.show() logger.info("ChefSystem application started successfully") # Run application event loop return app.exec() except Exception as e: logger.error(f"Fatal error starting application: {e}", exc_info=True) # Show error dialog to user error_dialog = QMessageBox() error_dialog.setIcon(QMessageBox.Icon.Critical) error_dialog.setWindowTitle("ChefSystem - Fatal Error") error_dialog.setText("Failed to start ChefSystem") error_dialog.setInformativeText(str(e)) error_dialog.setDetailedText(f"Error type: {type(e).__name__}") error_dialog.exec() return 1 if __name__ == "__main__": sys.exit(main())