S1005403_RisCC/target_simulator/__main__.py

81 lines
2.4 KiB
Python

# target_simulator/__main__.py
"""
Main entry point for the Target Simulator application.
This script initializes the logging system, creates the main application
window, and starts the Tkinter event loop.
"""
import sys
import os
import logging
import shutil
# Add the project root to the Python path to allow absolute imports
project_root = os.path.dirname(os.path.abspath(__file__))
if project_root not in sys.path:
sys.path.insert(0, project_root)
from target_simulator.gui.main_view import MainView
from target_simulator.utils.logger import (
setup_basic_logging,
get_logger,
add_tkinter_handler,
apply_saved_logger_levels,
)
# Import configurations
from target_simulator.config import LOGGING_CONFIG, DEBUG_CONFIG
def _clear_temp_folder():
"""
Ensures the temporary directory is empty at application startup.
It deletes the directory and all its contents, then recreates it.
"""
temp_folder = DEBUG_CONFIG.get("temp_folder_name", "Temp")
try:
if os.path.exists(temp_folder):
shutil.rmtree(temp_folder)
# Recreate the folder to ensure it's available for the new session.
os.makedirs(temp_folder, exist_ok=True)
except OSError as e:
# This can happen if files are locked. It's not critical, so we just warn.
print(f"Warning: Could not fully clear temp folder '{temp_folder}': {e}")
def main():
"""Initializes and runs the application."""
# Clean up the entire temp folder from the previous run at application startup.
_clear_temp_folder()
# Silence verbose loggers from third-party libraries
logging.getLogger("matplotlib").setLevel(logging.WARNING)
logging.getLogger("PIL").setLevel(logging.INFO)
logging.getLogger("PIL.PngImagePlugin").setLevel(logging.INFO)
app = MainView()
# Setup the global logging system, connecting it to the GUI's main loop
setup_basic_logging(app, LOGGING_CONFIG)
# Apply any saved logger levels so preferences take effect even if the
# LoggerPanel is never opened.
try:
apply_saved_logger_levels()
except Exception:
pass
# Now that the logging system is active, add the handler for the GUI widget
add_tkinter_handler(app.log_text_widget, LOGGING_CONFIG)
logger = get_logger(__name__)
logger.info("Application starting up.")
app.mainloop()
if __name__ == "__main__":
main()