mirror of
https://github.com/KenanZhu/AutoLibrary.git
synced 2026-06-17 23:13:03 +08:00
refactor(AppInitializer): 将初始化逻辑提取到 AppInitializer 模块中
- 本次提交将 Main.py 中的 ConfigManager, LogManager 等初始化逻辑提取到 AppInitializer 模块中 - 更改默认的配置文件路径从 config 目录变为 configs 目录,并考虑兼容性问题
This commit is contained in:
+4
-22
@@ -10,32 +10,15 @@ See the LICENSE file for details.
|
||||
import os
|
||||
import sys
|
||||
|
||||
from PySide6.QtCore import QTranslator, QStandardPaths, QDir
|
||||
from PySide6.QtCore import QTranslator
|
||||
from PySide6.QtWidgets import QApplication
|
||||
|
||||
from gui.ALMainWindow import ALMainWindow
|
||||
from gui.resources import ALResource
|
||||
|
||||
from utils.ConfigManager import instance as configInstance
|
||||
from utils.LogManager import instance as logInstance
|
||||
from utils.AppInitializer import initializeApp
|
||||
|
||||
|
||||
def initializeConfigManager():
|
||||
|
||||
app_dir = QStandardPaths.writableLocation(QStandardPaths.StandardLocation.AppDataLocation)
|
||||
config_dir = os.path.join(app_dir, "config")
|
||||
if not QDir(config_dir).exists():
|
||||
QDir().mkpath(config_dir)
|
||||
configInstance(config_dir)
|
||||
|
||||
def initializeLogManager():
|
||||
|
||||
app_dir = QStandardPaths.writableLocation(QStandardPaths.StandardLocation.AppDataLocation)
|
||||
log_dir = os.path.join(app_dir, "logs")
|
||||
if not QDir(log_dir).exists():
|
||||
QDir().mkpath(log_dir)
|
||||
logInstance(log_dir)
|
||||
|
||||
def main():
|
||||
|
||||
app = QApplication(sys.argv)
|
||||
@@ -44,13 +27,12 @@ def main():
|
||||
app.installTranslator(translator)
|
||||
app.setStyle('Fusion')
|
||||
app.setApplicationName("AutoLibrary")
|
||||
initializeConfigManager()
|
||||
initializeLogManager()
|
||||
if not initializeApp():
|
||||
sys.exit(-1)
|
||||
window = ALMainWindow()
|
||||
window.show()
|
||||
sys.exit(app.exec_())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,52 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Copyright (c) 2025 - 2026 KenanZhu.
|
||||
All rights reserved.
|
||||
|
||||
This software is provided "as is", without any warranty of any kind.
|
||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||
See the LICENSE file for details.
|
||||
"""
|
||||
import os
|
||||
|
||||
from PySide6.QtCore import QStandardPaths, QDir
|
||||
|
||||
from utils.ConfigManager import instance as configInstance
|
||||
from utils.LogManager import instance as logInstance
|
||||
|
||||
|
||||
def initializeConfigManager(
|
||||
) -> bool:
|
||||
|
||||
app_dir = QStandardPaths.writableLocation(QStandardPaths.StandardLocation.AppDataLocation)
|
||||
old_config_dir = os.path.join(app_dir, "config")
|
||||
new_config_dir = os.path.join(app_dir, "configs")
|
||||
if QDir(old_config_dir).exists(): # old config dir exists
|
||||
#we rename it to compatible with new version
|
||||
if not QDir().rename(old_config_dir, new_config_dir):
|
||||
return False
|
||||
elif not QDir(new_config_dir).exists():
|
||||
if not QDir().mkpath(new_config_dir):
|
||||
return False
|
||||
configInstance(new_config_dir)
|
||||
return True
|
||||
|
||||
def initializeLogManager(
|
||||
) -> bool:
|
||||
|
||||
app_dir = QStandardPaths.writableLocation(QStandardPaths.StandardLocation.AppDataLocation)
|
||||
log_dir = os.path.join(app_dir, "logs")
|
||||
if not QDir(log_dir).exists():
|
||||
if not QDir().mkpath(log_dir):
|
||||
return False
|
||||
logInstance(log_dir)
|
||||
return True
|
||||
|
||||
def initializeApp(
|
||||
) -> bool:
|
||||
|
||||
if not initializeConfigManager():
|
||||
return False
|
||||
if not initializeLogManager():
|
||||
return False
|
||||
return True
|
||||
Reference in New Issue
Block a user