121 lines
4.6 KiB
Python
121 lines
4.6 KiB
Python
# controlpanel/__main__.py
|
|
"""
|
|
Entry point for the Control Panel application when run as a module
|
|
using `python -m controlpanel`.
|
|
"""
|
|
|
|
# --- Standard library imports ---
|
|
import tkinter as tk
|
|
import sys
|
|
import logging
|
|
import os
|
|
|
|
# --- Configuration and Core App Import ---
|
|
# Use relative imports within the package
|
|
from controlpanel import config
|
|
from controlpanel.logging_config import setup_logging
|
|
from controlpanel.app_main import ControlPanelApp # Renamed the class file to app_main.py
|
|
|
|
# --- Setup Logging ---
|
|
# It's good practice to setup logging as early as possible.
|
|
try:
|
|
setup_logging()
|
|
except ImportError:
|
|
print("ERROR: logging_config.py not found. Using basic logging.")
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s",
|
|
)
|
|
except Exception as log_setup_err:
|
|
print(f"ERROR setting up logging: {log_setup_err}")
|
|
# Basic logging as fallback if setup fails
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format="%(asctime)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s",
|
|
)
|
|
|
|
# --- Main Execution Block ---
|
|
def main():
|
|
"""Main function to create and run the application."""
|
|
root = None
|
|
app_instance = None
|
|
try:
|
|
# Check for critical map library dependencies if map is enabled
|
|
# Accessing MAP_MODULES_LOADED requires importing app_main first
|
|
# Or potentially moving the check inside ControlPanelApp.__init__
|
|
# For simplicity here, we assume the check happens within ControlPanelApp.
|
|
# if config.ENABLE_MAP_OVERLAY and not app_main.MAP_MODULES_LOADED:
|
|
# msg = "Map enabled but required modules failed. Cannot start."
|
|
# logging.critical(f"[App Main] {msg}")
|
|
# print(f"ERROR: {msg} Check logs for missing libraries.")
|
|
# sys.exit(1)
|
|
|
|
# Create main Tkinter window using helper from gui.ui
|
|
# Need to import the ui module first
|
|
try:
|
|
from controlpanel.gui.ui import create_main_window
|
|
except ImportError as ui_import_err:
|
|
logging.critical(f"Failed to import UI components: {ui_import_err}")
|
|
print(f"ERROR: Failed to import UI components: {ui_import_err}")
|
|
sys.exit(1)
|
|
|
|
root = create_main_window(
|
|
"Control Panel",
|
|
config.TKINTER_MIN_WIDTH,
|
|
config.TKINTER_MIN_HEIGHT,
|
|
10,
|
|
10, # Initial position, App constructor calculates final
|
|
)
|
|
# Instantiate the main application class
|
|
app_instance = ControlPanelApp(root) # Pass root window
|
|
|
|
# Set the window close protocol handler
|
|
root.protocol("WM_DELETE_WINDOW", app_instance.close_app)
|
|
|
|
# Start the Tkinter main event loop
|
|
logging.info("Starting Tkinter main loop...")
|
|
root.mainloop()
|
|
|
|
except SystemExit as exit_e:
|
|
# Handle clean exits initiated by sys.exit()
|
|
exit_code = exit_e.code if isinstance(exit_e.code, int) else 1
|
|
log_level = logging.INFO if exit_code == 0 else logging.WARNING
|
|
# Log the exit code without re-raising SystemExit
|
|
logging.log(
|
|
log_level, f"[App Main] Application exited via sys.exit({exit_code})."
|
|
)
|
|
except ImportError as imp_err:
|
|
# Handle critical import errors during startup
|
|
logging.critical(
|
|
f"[App Main] CRITICAL IMPORT ERROR: {imp_err}. Application cannot start.",
|
|
exc_info=True,
|
|
)
|
|
print(
|
|
f"\nCRITICAL ERROR: Missing required library - {imp_err}\n"
|
|
"Please install the necessary libraries (check logs/readme) and try again.\n"
|
|
)
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
# Catch any other unhandled exceptions during startup or main loop
|
|
logging.critical(
|
|
"[App Main] UNHANDLED EXCEPTION during startup or main loop:", exc_info=True
|
|
)
|
|
print(
|
|
"\nFATAL ERROR: An unhandled exception occurred. Check logs for details.\n"
|
|
)
|
|
# Optionally try a graceful shutdown if app_instance exists
|
|
if app_instance and hasattr(app_instance, 'close_app'):
|
|
try:
|
|
logging.info("Attempting graceful shutdown after unhandled exception...")
|
|
app_instance.close_app()
|
|
except Exception as shutdown_err:
|
|
logging.error(f"Error during forced shutdown: {shutdown_err}")
|
|
sys.exit(1)
|
|
finally:
|
|
# Ensure logging is shut down properly on any exit path
|
|
logging.info("=== App End ===")
|
|
logging.shutdown()
|
|
|
|
# Standard Python entry point check
|
|
if __name__ == "__main__":
|
|
main() |