-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
101 lines (87 loc) · 3.38 KB
/
main.py
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
import logging
import os
import sys
from pathlib import Path
from typing import Callable, Optional
from controller import Controller
from gui import GUI
import customtkinter as ctk
def setup_logging() -> None:
"""Set up logging configuration for the application."""
try:
user_dir: str = os.path.expanduser("~")
log_dir: str = os.path.join(user_dir, "The_Library")
os.makedirs(log_dir, exist_ok=True)
log_file: str = os.path.join(log_dir, "the_library.log")
logging.basicConfig(filename=log_file, level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console: logging.StreamHandler = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter: logging.Formatter = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
console.setFormatter(formatter)
logging.getLogger('').addHandler(console)
except Exception as e:
print(f"Error setting up logging: {str(e)}")
sys.exit(1)
def get_base_dir() -> str:
try:
if getattr(sys, 'frozen', False):
return sys._MEIPASS
else:
return os.path.dirname(os.path.abspath(__file__))
except Exception as e:
logging.error(f"Error getting base directory: {str(e)}")
raise
def get_user_data_dir() -> str:
"""
Get the user data directory for storing application data.
Returns:
str: The path to the user data directory.
Raises:
OSError: If there's an error creating the directory.
"""
try:
home: Path = Path.home()
if sys.platform == "win32":
user_data_dir: Path = home / "AppData/Local/TheLibrary"
elif sys.platform == "darwin":
user_data_dir: Path = home / "Library/Application Support/TheLibrary"
user_data_dir.mkdir(parents=True, exist_ok=True)
return str(user_data_dir)
except OSError as e:
logging.error(f"Error creating user data directory: {str(e)}")
raise
def main() -> None:
setup_logging()
logger: logging.Logger = logging.getLogger(__name__)
logger.info("Starting The Library application")
try:
logger.info("Initializing Controller")
controller: Controller = Controller(get_user_data_dir)
logger.info("Controller initialized successfully")
logger.info("Initializing GUI")
gui: GUI = GUI(controller, get_user_data_dir)
logger.info("GUI initialized successfully")
logger.info("Running GUI")
gui.run()
logger.info("GUI run completed")
except Exception as e:
logger.exception(f"A critical error occurred while running the application: {str(e)}")
print(f"A critical error occurred: {str(e)}")
print("Please check the log file for more details.")
finally:
logger.info("Cleaning up resources")
cleanup_ctk(logger)
def cleanup_ctk(logger: logging.Logger) -> None:
try:
if hasattr(ctk, 'destroy'):
ctk.destroy()
elif hasattr(ctk, 'quit'):
ctk.quit()
else:
logger.warning("No method found to destroy CustomTkinter context")
logger.info("CustomTkinter context cleanup attempted")
except Exception as e:
logger.error(f"Error during CustomTkinter context cleanup: {e}")
if __name__ == "__main__":
main()