forked from nupurmadaan04/SOUL_SENSE_EXAM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_main.py
More file actions
222 lines (182 loc) · 8.14 KB
/
temp_main.py
File metadata and controls
222 lines (182 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import tkinter as tk
from tkinter import messagebox
import logging
from app.ui.app_initializer import AppInitializer
from app.ui.view_manager import ViewManager
from app.auth.app_auth import AppAuth
from app.shutdown_handler import ShutdownHandler
from app.ui.styles import UIStyles
from app.startup_checks import run_all_checks, get_check_summary, CheckStatus
from app.exceptions import IntegrityError
from app.logger import setup_logging
from app.error_handler import setup_global_exception_handlers
from app.questions import initialize_questions
from typing import Optional, Dict, Any
class SoulSenseApp:
def __init__(self, root: tk.Tk) -> None:
self.root = root
# Initialize modules
self.initializer = AppInitializer(self)
self.view_manager = ViewManager(self)
self.auth_handler = AppAuth(self)
self.shutdown_handler = ShutdownHandler(self)
# State for optimization
self.is_animating = False
# Expose some attributes for backward compatibility
self.colors = self.initializer.app.colors
self.fonts = self.initializer.app.fonts
self.username = self.initializer.app.username
self.current_user_id = self.initializer.app.current_user_id
self.settings = self.initializer.app.settings
self.questions = self.initializer.app.questions
self.main_container = self.initializer.app.main_container
self.sidebar = self.initializer.app.sidebar
self.content_area = self.initializer.app.content_area
self.exam_manager = self.initializer.app.exam_manager
self.ui_styles = self.initializer.app.ui_styles
self.logger = self.initializer.app.logger
self.i18n = self.initializer.app.i18n
self.age = self.initializer.app.age
self.age_group = self.initializer.app.age_group
def switch_view(self, view_id):
"""Delegate view switching to ViewManager"""
self.view_manager.switch_view(view_id)
def apply_theme(self, theme_name: str):
"""Delegate theme application to UIStyles"""
self.ui_styles.apply_theme(theme_name)
# Refresh current view
if hasattr(self, 'current_view') and self.current_view:
self.switch_view(self.current_view)
elif hasattr(self, 'sidebar') and self.sidebar.active_id:
self.switch_view(self.sidebar.active_id)
def show_home(self):
"""Delegate to ViewManager"""
self.view_manager.show_home()
def start_exam(self):
"""Delegate to ViewManager"""
self.view_manager.start_exam()
def show_dashboard(self):
"""Delegate to ViewManager"""
self.view_manager.show_dashboard()
def show_journal(self):
"""Delegate to ViewManager"""
self.view_manager.show_journal()
def show_profile(self):
"""Delegate to ViewManager"""
self.view_manager.show_profile()
def show_history(self):
"""Delegate to ViewManager"""
self.view_manager.show_history()
def show_assessments(self):
"""Delegate to ViewManager"""
self.view_manager.show_assessments()
def clear_screen(self):
"""Delegate to ViewManager"""
self.view_manager.clear_screen()
def graceful_shutdown(self):
"""Delegate to ShutdownHandler"""
self.shutdown_handler.graceful_shutdown()
def logout(self):
"""Handle user logout with confirmation"""
if messagebox.askyesno("Confirm Logout", "Are you sure you want to log out? Any unsaved changes will be lost."):
self.initializer.logout_user()
# --- Global Error Handlers ---
def show_error(title, message, exception=None):
"""Global error display function"""
if exception:
logging.error(f"{title}: {message} - {exception}")
else:
logging.error(f"{title}: {message}")
try:
messagebox.showerror(title, message)
except:
print(f"CRITICAL ERROR (No GUI): {title} - {message}")
def global_exception_handler(self, exc_type, exc_value, traceback_obj):
"""Handle uncaught exceptions"""
import traceback
traceback_str = "".join(traceback.format_exception(exc_type, exc_value, traceback_obj))
logging.critical(f"Uncaught Exception: {traceback_str}")
show_error("Unexpected Error", f"An unexpected error occurred:\n{exc_value}", exception=traceback_str)
if __name__ == "__main__":
# Setup centralized logging and error handling
setup_logging()
setup_global_exception_handlers()
try:
# Run startup integrity checks before initializing the app
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
try:
results = run_all_checks(raise_on_critical=True)
summary = get_check_summary(results)
logger.info(summary)
# Show warning dialog if there were any warnings
warnings = [r for r in results if r.status == CheckStatus.WARNING]
if warnings:
# Create a temporary root for the warning dialog
temp_root = tk.Tk()
temp_root.withdraw()
warning_msg = "\n".join([f"• {r.name}: {r.message}" for r in warnings])
messagebox.showwarning(
"Startup Warnings",
f"The application started with the following warnings:\n\n{warning_msg}\n\nThe application will continue with default settings."
)
temp_root.destroy()
except IntegrityError as e:
# Critical failure - show error and exit
temp_root = tk.Tk()
temp_root.withdraw()
messagebox.showerror(
"Startup Failed",
f"Critical integrity check failed:\n\n{str(e)}\n\nThe application cannot start."
)
temp_root.destroy()
raise SystemExit(1)
# All checks passed, start the application
# Initialize Questions Cache (Preload)
logger.info("Preloading questions into memory...")
if not initialize_questions():
logger.warning("Initial question preload failed. Application will attempt lazy-loading.")
root = tk.Tk()
# Register tkinter-specific exception handler
def tk_report_callback_exception(exc_type, exc_value, exc_tb):
"""Handle exceptions in tkinter callbacks."""
from app.error_handler import get_error_handler, ErrorSeverity
handler = get_error_handler()
handler.log_error(
exc_value,
module="tkinter",
operation="callback",
severity=ErrorSeverity.HIGH
)
user_msg = handler.get_user_message(exc_value)
show_error("Interface Error", user_msg, exc_value)
root.report_callback_exception = tk_report_callback_exception
app = SoulSenseApp(root)
# Show the initial home view
app.show_home()
# Set up graceful shutdown handlers
root.protocol("WM_DELETE_WINDOW", app.graceful_shutdown)
# Signal handlers for SIGINT (Ctrl+C) and SIGTERM
def signal_handler(signum, frame):
app.logger.info(f"Received signal {signum}, initiating shutdown")
app.graceful_shutdown()
import signal
signal.signal(signal.SIGINT, signal_handler)
# Try to register SIGTERM handler, but don't fail if it's not available
try:
signal.signal(signal.SIGTERM, signal_handler)
except (AttributeError, ValueError, OSError):
# SIGTERM may not be available on some platforms (e.g., older Windows)
app.logger.debug("SIGTERM not available on this platform, skipping registration")
# Register atexit handler as backup
import atexit
atexit.register(app.graceful_shutdown)
root.mainloop()
except SystemExit:
pass # Clean exit from integrity failure
except Exception as e:
import traceback
from app.error_handler import get_error_handler, ErrorSeverity
handler = get_error_handler()
handler.log_error(e, module="main", operation="startup", severity=ErrorSeverity.CRITICAL)
traceback.print_exc()