forked from bruhnn/BD2ModManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
175 lines (135 loc) · 5.32 KB
/
main.py
File metadata and controls
175 lines (135 loc) · 5.32 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
from src.version import __version__
from src.views import MainView
from src.controllers import MainController
from src.utils.logger import setup_logging
from src.utils.paths import app_paths
from argparse import ArgumentParser
import logging
import time
import sys
from PySide6.QtWidgets import QApplication, QMessageBox, QSplashScreen
from PySide6.QtGui import QIcon, QFontDatabase, QPixmap
from PySide6.QtCore import Qt
# Needs to add the correct organization name and application before initializing app_paths
QApplication.setOrganizationName("Bruhnn")
QApplication.setApplicationName("BD2ModManager")
logger = logging.getLogger(__name__)
class Application:
def __init__(self, start_time: float) -> None:
logger.info("Initializing Application...")
self._start_time = start_time
self.app = QApplication(sys.argv)
self.app.setOrganizationName("Bruhnn")
self.app.setApplicationName("BD2ModManager")
# self._show_splash_screen()
self._init_appdata()
self._add_resources()
self._create_ui()
logger.info("Application initialization complete.")
def _show_splash_screen(self) -> None:
logger.info("Showing splash screen...")
splash_img = QPixmap(app_paths.source_path /
"resources" / "splash.png")
splash_img = splash_img.scaled(
200, 200, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation
)
self.splash = QSplashScreen(
splash_img, Qt.WindowType.WindowStaysOnTopHint)
self.splash.show()
self.app.processEvents()
def _add_resources(self) -> None:
logger.info("Adding application resources...")
# load resources from QRC file: DON'T RUN RUFF --fix
from src.resources import icons_rc, characters_rc
icon_path = app_paths.source_path / "resources" / "icon.ico"
if icon_path.exists():
self.app.setWindowIcon(QIcon(str(icon_path)))
logger.debug(f"Application icon set from {icon_path}")
else:
logger.warning(f"Application icon not found at {icon_path}")
font_path = (
app_paths.source_path
/ "resources"
/ "fonts"
/ "Cinzel"
/ "Cinzel-VariableFont_wght.ttf"
)
if font_path.exists():
font_id = QFontDatabase.addApplicationFont(str(font_path))
if font_id != -1:
font_families = QFontDatabase.applicationFontFamilies(font_id)
logger.debug(
f"Font '{font_families[0]}' loaded from {font_path}")
else:
logger.error("Failed to load font from %s", font_path)
else:
logger.warning("Font not found at %s", font_path)
def _init_appdata(self) -> None:
from src.services.data_manager import DataManager
logger.info("Initializing application data...")
data_manager = DataManager()
if not data_manager.initialize_app_data():
# Show to the user data data initialize failed and so the update will not work
sys.exit(1)
def _create_ui(self) -> None:
logger.info("Creating user interface...")
self.main_view = MainView()
logger.debug("MainView instantiated.")
self.main_controller = MainController(view=self.main_view)
logger.debug("MainController instantiated and linked with MainView.")
logger.info("UI creation complete.")
def run(self) -> int:
end_time = time.perf_counter()
load_time = end_time - self._start_time
logger.info("Application loaded in %.3f seconds.", load_time)
logger.info("Showing main window and starting application event loop.")
self.main_controller.show()
if hasattr(self, "splash"):
self.splash.finish(self.main_view)
self.splash.deleteLater()
return self.app.exec()
def main() -> None:
parser = ArgumentParser(
description=f"BD2ModManager v{__version__}",
epilog="Example: python main.py --log-level debug"
)
parser.add_argument(
"--version",
action="version",
version=f"%(prog)s {__version__}"
)
logging_group = parser.add_argument_group("Logging")
logging_group.add_argument(
"-l", "--log-level",
type=str,
default="debug",
choices=["debug", "info", "warning", "error", "critical"],
help="Set the logging verbosity level (default: %(default)s)."
)
logging_group.add_argument(
"-f",
"--log-filter",
type=str,
metavar="NAME",
help="Filter logs by a specific module or function name."
)
logging_group.add_argument(
"-n",
"--no-logs",
action="store_true",
default=False
)
args = parser.parse_args()
if not args.no_logs:
setup_logging(app_paths.app_path / "BD2ModManager-logs.log",
args.log_level, args.log_filter)
logger.info("=" * 50)
logger.info("Starting BD2ModManager v%s", __version__)
logger.info("=" * 50)
start_time = time.perf_counter()
from src.themes import ThemeManager
ThemeManager.load_themes()
app = Application(start_time=start_time)
sys.exit(app.run())
if __name__ == "__main__":
main()