mirror of
https://github.com/KenanZhu/AutoLibrary.git
synced 2026-06-18 07:23:03 +08:00
feat(utils): 添加 ConfigManager 与 JSON 配置读写,替换旧实现
add: - src/utils/ConfigManager.py - src/utils/JSONReader.py - src/utils/JSONWriter.py remove: - src/utils/ConfigReader.py - src/utils/ConfigWriter.py refactor: - 更新调用方以使用 ConfigManager / JSONReader / JSONWriter(见 ALConfigWidget.py、ALMainWindow.py、ALTimerTaskManageWidget.py、ALMainWorkers.py 等) - 统一方法命名(initlize* -> initialize*)、改进错误提示与配置路径管理 BREAKING CHANGE: 删除 ConfigReader/ConfigWriter,外部调用需改为 JSONReader/JSONWriter 或通过 ConfigManager 访问配置
This commit is contained in:
+95
-68
@@ -21,8 +21,10 @@ from PySide6.QtGui import (
|
||||
QCloseEvent, QAction
|
||||
)
|
||||
|
||||
from utils.ConfigReader import ConfigReader
|
||||
from utils.ConfigWriter import ConfigWriter
|
||||
from utils.JSONReader import JSONReader
|
||||
from utils.JSONWriter import JSONWriter
|
||||
from utils.ConfigManager import ConfigType, instance
|
||||
from utils.ConfigManager import getValidateAutomationConfigPaths
|
||||
|
||||
from gui.resources.ui.Ui_ALConfigWidget import Ui_ALConfigWidget
|
||||
from gui.ALSeatMapSelectDialog import ALSeatMapSelectDialog
|
||||
@@ -32,27 +34,22 @@ from gui.ALUserTreeWidget import ALUserTreeWidget, ALUserTreeItemType
|
||||
|
||||
class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
|
||||
configWidgetIsClosed = Signal(dict)
|
||||
configWidgetIsClosed = Signal()
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
parent = None,
|
||||
config_paths = {
|
||||
"run": "",
|
||||
"user": ""
|
||||
}
|
||||
):
|
||||
|
||||
super().__init__(parent)
|
||||
self.__config_paths = config_paths
|
||||
self.__cfg_mgr = instance()
|
||||
self.__config_paths = getValidateAutomationConfigPaths()
|
||||
self.__config_data = {"run": {}, "user": {}}
|
||||
|
||||
self.setupUi(self)
|
||||
self.modifyUi()
|
||||
self.connectSignals()
|
||||
self.initlizeFloorRoomMap()
|
||||
self.initlizeDefaultConfigPaths()
|
||||
if not self.initlizeConfigs():
|
||||
if not self.initializeConfigs():
|
||||
self.close()
|
||||
|
||||
|
||||
@@ -68,8 +65,8 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
self.UserListLayout.insertWidget(0, self.UserTreeWidget)
|
||||
self.UserTreeWidget.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
|
||||
self.UserTreeWidget.customContextMenuRequested.connect(self.onUserTreeWidgetContextMenu)
|
||||
self.initlizeFloorRoomMap()
|
||||
self.initilizeUserInfoWidget()
|
||||
self.initializeFloorRoomMap()
|
||||
self.initializeUserInfoWidget()
|
||||
|
||||
|
||||
def connectSignals(
|
||||
@@ -124,11 +121,11 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
event: QCloseEvent
|
||||
):
|
||||
|
||||
self.configWidgetIsClosed.emit(self.__config_paths)
|
||||
self.configWidgetIsClosed.emit()
|
||||
super().closeEvent(event)
|
||||
|
||||
|
||||
def initlizeFloorRoomMap(
|
||||
def initializeFloorRoomMap(
|
||||
self
|
||||
):
|
||||
|
||||
@@ -162,19 +159,7 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
}
|
||||
|
||||
|
||||
def initlizeDefaultConfigPaths(
|
||||
self
|
||||
):
|
||||
|
||||
executable_path = sys.executable
|
||||
executable_dir = QFileInfo(executable_path).absoluteDir()
|
||||
self.__default_config_paths = {
|
||||
"user": QDir.toNativeSeparators(executable_dir.absoluteFilePath("user.json")),
|
||||
"run": QDir.toNativeSeparators(executable_dir.absoluteFilePath("run.json"))
|
||||
}
|
||||
|
||||
|
||||
def initlizeConfigToWidget(
|
||||
def initializeConfigToWidget(
|
||||
self,
|
||||
which: str,
|
||||
config_data: dict
|
||||
@@ -184,12 +169,12 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
self.setRunConfigToWidget(config_data)
|
||||
self.CurrentRunConfigEdit.setText(self.__config_paths["run"])
|
||||
elif which == "user":
|
||||
self.initilizeUserInfoWidget()
|
||||
self.initializeUserInfoWidget()
|
||||
self.setUsersToTreeWidget(config_data)
|
||||
self.CurrentUserConfigEdit.setText(self.__config_paths["user"])
|
||||
|
||||
|
||||
def initlizeConfig(
|
||||
def initializeConfig(
|
||||
self,
|
||||
which: str
|
||||
) -> bool:
|
||||
@@ -225,18 +210,16 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
return is_success
|
||||
|
||||
|
||||
def initlizeConfigs(
|
||||
def initializeConfigs(
|
||||
self
|
||||
) -> bool:
|
||||
|
||||
is_success = True
|
||||
for which in ["run", "user"]:
|
||||
if not self.__config_paths[which]:
|
||||
self.__config_paths[which] = self.__default_config_paths[which]
|
||||
if not self.initlizeConfig(which):
|
||||
if not self.initializeConfig(which):
|
||||
is_success = False
|
||||
break
|
||||
self.initlizeConfigToWidget(which, self.__config_data[which])
|
||||
self.initializeConfigToWidget(which, self.__config_data[which])
|
||||
return is_success
|
||||
|
||||
|
||||
@@ -321,19 +304,21 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"警告 - AutoLibrary",
|
||||
f"运行配置文件: {self.__config_paths['run']}\n"
|
||||
f"读取时键 '{e}' 发生错误,文件可能被意外修改或已经损坏\n"
|
||||
f"运行配置文件读取键 '{e}' 时发生错误 ! :\n"
|
||||
f"文件路径: {self.__config_paths['run']}\n"
|
||||
"文件可能被意外修改或已经损坏\n"
|
||||
)
|
||||
except Exception as e:
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"警告 - AutoLibrary",
|
||||
f"运行配置文件: {self.__config_paths['run']}\n"
|
||||
f"读取时键 '{e}' 发生未知错误,文件可能被意外修改或已经损坏\n"
|
||||
f"运行配置文件读取键 '{e}' 时发生未知错误 ! :\n"
|
||||
f"文件路径: {self.__config_paths['run']}\n"
|
||||
"文件可能被意外修改或已经损坏\n"
|
||||
)
|
||||
|
||||
|
||||
def initilizeUserInfoWidget(
|
||||
def initializeUserInfoWidget(
|
||||
self
|
||||
):
|
||||
|
||||
@@ -442,15 +427,17 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"警告 - AutoLibrary",
|
||||
f"用户配置文件: {self.__config_paths['user']}\n"\
|
||||
f"读取时键 '{e}' 发生错误,文件可能被意外修改或已经损坏\n"
|
||||
f"用户配置文件读取键 '{e}' 时发生错误 ! :\n"
|
||||
f"文件路径: {self.__config_paths['user']}\n"
|
||||
"文件可能被意外修改或已经损坏\n"
|
||||
)
|
||||
except Exception as e:
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"警告 - AutoLibrary",
|
||||
f"用户配置文件: {self.__config_paths['user']}\n"\
|
||||
f"读取时发生未知错误 '{e}',文件可能被意外修改或已经损坏\n"
|
||||
f"用户配置文件读取键 '{e}' 时发生未知错误 ! :\n"
|
||||
f"文件路径: {self.__config_paths['user']}\n"
|
||||
"文件可能被意外修改或已经损坏\n"
|
||||
)
|
||||
|
||||
|
||||
@@ -480,15 +467,17 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"警告 - AutoLibrary",
|
||||
f"用户配置文件: {self.__config_paths['user']}\n"\
|
||||
f"读取时键 '{e}' 发生错误,文件可能被意外修改或已经损坏\n"
|
||||
f"用户配置文件读取键 '{e}' 时发生错误 ! :\n"
|
||||
f"文件路径: {self.__config_paths['user']}\n"
|
||||
"文件可能被意外修改或已经损坏\n"
|
||||
)
|
||||
except Exception as e:
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"警告 - AutoLibrary",
|
||||
f"用户配置文件: {self.__config_paths['user']}\n"\
|
||||
f"读取时发生未知错误 '{e}',文件可能被意外修改或已经损坏\n"
|
||||
f"用户配置文件读取键 '{e}' 时发生未知错误 ! :\n"
|
||||
f"文件路径: {self.__config_paths['user']}\n"
|
||||
"文件可能被意外修改或已经损坏\n"
|
||||
)
|
||||
finally:
|
||||
self.UserTreeWidget.itemChanged.connect(self.onUserTreeWidgetItemChanged)
|
||||
@@ -502,17 +491,18 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
try:
|
||||
if not run_config_path or not os.path.exists(run_config_path):
|
||||
raise Exception("文件路径不存在")
|
||||
run_config = ConfigReader(run_config_path).getConfigs()
|
||||
run_config = JSONReader(run_config_path).data()
|
||||
if run_config and "library" in run_config\
|
||||
and "web_driver" in run_config\
|
||||
and "login" in run_config:
|
||||
return run_config
|
||||
return None
|
||||
else:
|
||||
return None
|
||||
except Exception as e:
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"警告 - AutoLibrary",
|
||||
f"运行配置文件读取发生错误 ! : \n{e}"
|
||||
f"运行配置文件读取发生错误 ! :\n{e}"
|
||||
)
|
||||
return None
|
||||
|
||||
@@ -528,7 +518,7 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
raise Exception("文件路径为空")
|
||||
if not run_config_data or not isinstance(run_config_data, dict):
|
||||
raise Exception("运行配置数据为空或类型错误")
|
||||
ConfigWriter(run_config_path, run_config_data)
|
||||
JSONWriter(run_config_path, run_config_data)
|
||||
return True
|
||||
except Exception as e:
|
||||
QMessageBox.warning(
|
||||
@@ -547,11 +537,11 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
try:
|
||||
if not user_config_path or not os.path.exists(user_config_path):
|
||||
raise Exception("文件路径不存在")
|
||||
user_config = ConfigReader(user_config_path).getConfigs()
|
||||
user_config = JSONReader(user_config_path).data()
|
||||
if user_config and "groups" in user_config:
|
||||
return user_config
|
||||
# compatibility with old version config format
|
||||
if user_config and "users" in user_config:
|
||||
elif user_config and "users" in user_config:
|
||||
user_config = {
|
||||
"groups": [
|
||||
{
|
||||
@@ -562,12 +552,13 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
]
|
||||
}
|
||||
return user_config
|
||||
return None
|
||||
else:
|
||||
return None
|
||||
except Exception as e:
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"警告 - AutoLibrary",
|
||||
f"用户配置文件读取发生错误 ! : \n{e}"
|
||||
f"用户配置文件读取发生错误 ! :\n{e}"
|
||||
)
|
||||
return None
|
||||
|
||||
@@ -583,13 +574,13 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
raise Exception("文件路径为空")
|
||||
if not user_config_data or not isinstance(user_config_data, dict):
|
||||
raise Exception("用户配置数据为空或类型错误")
|
||||
ConfigWriter(user_config_path, user_config_data)
|
||||
JSONWriter(user_config_path, user_config_data)
|
||||
return True
|
||||
except Exception as e:
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"警告 - AutoLibrary",
|
||||
f"用户配置文件写入发生错误 ! : \n{e}"
|
||||
f"用户配置文件写入发生错误 ! :\n{e}"
|
||||
)
|
||||
return False
|
||||
|
||||
@@ -838,7 +829,7 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
previous.setText(1, "" if user.get("enabled", True) else "跳过")
|
||||
previous.setData(0, Qt.UserRole, user)
|
||||
if current is None:
|
||||
self.initilizeUserInfoWidget()
|
||||
self.initializeUserInfoWidget()
|
||||
return
|
||||
if current.type() == ALUserTreeItemType.USER.value:
|
||||
user = current.data(0, Qt.UserRole)
|
||||
@@ -846,7 +837,7 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
self.setUserToWidget(user)
|
||||
self.UsernameEdit.textEdited.connect(lambda text: current.setText(0, text))
|
||||
else:
|
||||
self.initilizeUserInfoWidget()
|
||||
self.initializeUserInfoWidget()
|
||||
|
||||
@Slot()
|
||||
def onUserTreeWidgetItemChanged(
|
||||
@@ -973,9 +964,27 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
)[0]
|
||||
if run_config_path:
|
||||
run_config_path = QDir.toNativeSeparators(run_config_path)
|
||||
if self.loadConfig(run_config_path):
|
||||
data = self.loadRunConfig(run_config_path)
|
||||
if data is not None:
|
||||
self.__config_data["run"].update(data)
|
||||
self.setRunConfigToWidget(data)
|
||||
self.__config_paths["run"] = run_config_path
|
||||
self.CurrentRunConfigEdit.setText(run_config_path)
|
||||
paths = self.__cfg_mgr.get(ConfigType.GLOBAL, "automation.run_path.paths", [])
|
||||
if run_config_path not in paths:
|
||||
paths.append(run_config_path)
|
||||
index = len(paths) - 1
|
||||
else:
|
||||
index = paths.index(run_config_path)
|
||||
self.__cfg_mgr.set(ConfigType.GLOBAL, "automation.run_path", {"current": index, "paths": paths})
|
||||
else:
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"警告 - AutoLibrary",
|
||||
"运行配置文件读取发生错误 ! :\n"\
|
||||
"无法从选择的运行配置文件中加载数据 ! :\n"\
|
||||
"可能选择了错误的配置文件类型"
|
||||
)
|
||||
|
||||
@Slot()
|
||||
def onBrowseCurrentUserConfigButtonClicked(
|
||||
@@ -990,9 +999,27 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
)[0]
|
||||
if user_config_path:
|
||||
user_config_path = QDir.toNativeSeparators(user_config_path)
|
||||
if self.loadConfig(user_config_path):
|
||||
data = self.loadUserConfig(user_config_path)
|
||||
if data is not None:
|
||||
self.__config_data["user"].update(data)
|
||||
self.setUsersToTreeWidget(data)
|
||||
self.__config_paths["user"] = user_config_path
|
||||
self.CurrentUserConfigEdit.setText(user_config_path)
|
||||
paths = self.__cfg_mgr.get(ConfigType.GLOBAL, "automation.user_path.paths", [])
|
||||
if user_config_path not in paths:
|
||||
paths.append(user_config_path)
|
||||
index = len(paths) - 1
|
||||
else:
|
||||
index = paths.index(user_config_path)
|
||||
self.__cfg_mgr.set(ConfigType.GLOBAL, "automation.user_path", {"current": index, "paths": paths})
|
||||
else:
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"警告 - AutoLibrary",
|
||||
"用户配置文件读取发生错误 ! :\n"\
|
||||
"无法从选择的用户配置文件中加载数据 ! :\n"\
|
||||
"可能选择了错误的配置文件类型"
|
||||
)
|
||||
|
||||
@Slot()
|
||||
def onBrowseExportRunConfigButtonClicked(
|
||||
@@ -1079,9 +1106,9 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
if run_exists or user_exists:
|
||||
exist_files = []
|
||||
if run_exists:
|
||||
exist_files.append(run_config_path)
|
||||
exist_files.append(f"运行配置文件: \n{run_config_path}")
|
||||
if user_exists:
|
||||
exist_files.append(user_config_path)
|
||||
exist_files.append(f"用户配置文件: \n{user_config_path}")
|
||||
reply = QMessageBox.information(
|
||||
self,
|
||||
"提示 - AutoLibrary",
|
||||
@@ -1097,8 +1124,8 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
"run": run_config_path,
|
||||
"user": user_config_path
|
||||
}
|
||||
self.initlizeConfigToWidget("run", self.__config_data["run"])
|
||||
self.initlizeConfigToWidget("user", self.__config_data["user"])
|
||||
self.initializeConfigToWidget("run", self.__config_data["run"])
|
||||
self.initializeConfigToWidget("user", self.__config_data["user"])
|
||||
|
||||
@Slot()
|
||||
def onConfirmButtonClicked(
|
||||
@@ -1115,7 +1142,7 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
QMessageBox.information(
|
||||
self,
|
||||
"提示 - AutoLibrary",
|
||||
"配置文件保存成功 !\n"
|
||||
"配置文件保存成功 ! :\n"
|
||||
f"运行配置文件路径: \n{self.__config_paths['run']}\n"\
|
||||
f"用户配置文件路径: \n{self.__config_paths['user']}"
|
||||
)
|
||||
@@ -1123,7 +1150,7 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
||||
QMessageBox.warning(
|
||||
self,
|
||||
"警告 - AutoLibrary",
|
||||
"配置文件保存失败, 请检查文件路径权限"
|
||||
"配置文件保存失败 !\n"
|
||||
)
|
||||
self.close()
|
||||
|
||||
|
||||
+10
-18
@@ -7,12 +7,11 @@ 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 sys
|
||||
import time
|
||||
import os
|
||||
import queue
|
||||
|
||||
from PySide6.QtCore import (
|
||||
Qt, Signal, Slot, QDir, QFileInfo, QTimer, QUrl,
|
||||
Qt, Signal, Slot, QTimer, QDir, QUrl,
|
||||
)
|
||||
from PySide6.QtWidgets import (
|
||||
QMainWindow, QMenu, QSystemTrayIcon, QMessageBox
|
||||
@@ -23,6 +22,9 @@ from PySide6.QtGui import (
|
||||
|
||||
from base.MsgBase import MsgBase
|
||||
|
||||
from utils.ConfigManager import ConfigType, instance
|
||||
from utils.ConfigManager import getValidateAutomationConfigPaths
|
||||
|
||||
from gui.resources.ui.Ui_ALMainWindow import Ui_ALMainWindow
|
||||
from gui.resources import ALResource
|
||||
from gui.ALConfigWidget import ALConfigWidget
|
||||
@@ -44,14 +46,9 @@ class ALMainWindow(MsgBase, QMainWindow, Ui_ALMainWindow):
|
||||
|
||||
MsgBase.__init__(self, queue.Queue(), queue.Queue())
|
||||
QMainWindow.__init__(self)
|
||||
self.__cfg_mgr = instance()
|
||||
self.__timer_task_queue = queue.Queue()
|
||||
executable_path = sys.executable
|
||||
exectuable_dir = QFileInfo(executable_path).absoluteDir()
|
||||
self.__config_paths = {
|
||||
"run": QDir.toNativeSeparators(exectuable_dir.absoluteFilePath("run.json")),
|
||||
"user": QDir.toNativeSeparators(exectuable_dir.absoluteFilePath("user.json")),
|
||||
"timer_task": QDir.toNativeSeparators(exectuable_dir.absoluteFilePath("timer_task.json")),
|
||||
}
|
||||
self.__config_paths = getValidateAutomationConfigPaths()
|
||||
self.__alTimerTaskManageWidget = None
|
||||
self.__alConfigWidget = None
|
||||
self.__auto_lib_thread = None
|
||||
@@ -78,7 +75,7 @@ class ALMainWindow(MsgBase, QMainWindow, Ui_ALMainWindow):
|
||||
|
||||
# initialize timer task widget, but not show it
|
||||
try:
|
||||
self.__alTimerTaskManageWidget = ALTimerTaskManageWidget(self, self.__config_paths["timer_task"])
|
||||
self.__alTimerTaskManageWidget = ALTimerTaskManageWidget(self)
|
||||
except Exception as e:
|
||||
QMessageBox.critical(
|
||||
self,
|
||||
@@ -295,8 +292,7 @@ class ALMainWindow(MsgBase, QMainWindow, Ui_ALMainWindow):
|
||||
|
||||
@Slot(dict)
|
||||
def onConfigWidgetClosed(
|
||||
self,
|
||||
config_paths: dict
|
||||
self
|
||||
):
|
||||
|
||||
if self.__alConfigWidget:
|
||||
@@ -304,7 +300,6 @@ class ALMainWindow(MsgBase, QMainWindow, Ui_ALMainWindow):
|
||||
self.__alConfigWidget.deleteLater()
|
||||
self.__alConfigWidget = None
|
||||
self.setControlButtons(True, None, None)
|
||||
self.__config_paths = config_paths
|
||||
|
||||
@Slot(dict)
|
||||
def onTimerTaskIsReady(
|
||||
@@ -359,10 +354,7 @@ class ALMainWindow(MsgBase, QMainWindow, Ui_ALMainWindow):
|
||||
):
|
||||
|
||||
if self.__alConfigWidget is None:
|
||||
self.__alConfigWidget = ALConfigWidget(
|
||||
self,
|
||||
self.__config_paths
|
||||
)
|
||||
self.__alConfigWidget = ALConfigWidget(self)
|
||||
self.__alConfigWidget.configWidgetIsClosed.connect(self.onConfigWidgetClosed)
|
||||
self.__alConfigWidget.show()
|
||||
self.__alConfigWidget.raise_()
|
||||
|
||||
@@ -17,7 +17,7 @@ from PySide6.QtCore import (
|
||||
|
||||
from base.MsgBase import MsgBase
|
||||
from operators.AutoLib import AutoLib
|
||||
from utils.ConfigReader import ConfigReader
|
||||
from utils.JSONReader import JSONReader
|
||||
|
||||
|
||||
class AutoLibWorker(MsgBase, QThread):
|
||||
@@ -69,11 +69,11 @@ class AutoLibWorker(MsgBase, QThread):
|
||||
self._showTrace(
|
||||
f"正在加载配置文件, 运行配置文件路径: {self.__config_paths["run"]}"
|
||||
)
|
||||
self.__run_config = ConfigReader(self.__config_paths["run"]).getConfigs()
|
||||
self.__run_config = JSONReader(self.__config_paths["run"]).data()
|
||||
self._showTrace(
|
||||
f"正在加载配置文件, 用户配置文件路径: {self.__config_paths["user"]}"
|
||||
)
|
||||
self.__user_config = ConfigReader(self.__config_paths["user"]).getConfigs()
|
||||
self.__user_config = JSONReader(self.__config_paths["user"]).data()
|
||||
if self.__run_config is None or self.__user_config is None:
|
||||
self._showTrace("配置文件加载失败, 请检查配置文件是否正确")
|
||||
self._showTrace("配置文件加载失败, 请检查配置文件是否正确")
|
||||
|
||||
@@ -25,8 +25,7 @@ from PySide6.QtGui import (
|
||||
QCloseEvent
|
||||
)
|
||||
|
||||
from utils.ConfigReader import ConfigReader
|
||||
from utils.ConfigWriter import ConfigWriter
|
||||
from utils.ConfigManager import ConfigType, instance
|
||||
|
||||
from gui.resources.ui.Ui_ALTimerTaskManageWidget import Ui_ALTimerTaskManageWidget
|
||||
from gui.ALTimerTaskAddDialog import ALTimerTaskAddDialog, ALTimerTaskStatus
|
||||
@@ -142,16 +141,15 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
parent = None,
|
||||
timer_tasks_config_path: str = ""
|
||||
parent = None
|
||||
):
|
||||
|
||||
super().__init__(parent)
|
||||
self.__cfg_mgr = instance()
|
||||
self.__timer_tasks = []
|
||||
self.__check_timer = None
|
||||
self.__sort_policy = self.SortPolicy.BY_EXECUTE_TIME
|
||||
self.__sort_order = Qt.SortOrder.AscendingOrder
|
||||
self.__timer_tasks_config_path = timer_tasks_config_path
|
||||
|
||||
self.setupUi(self)
|
||||
self.connectSignals()
|
||||
@@ -180,44 +178,28 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
||||
self.__check_timer.start(500)
|
||||
|
||||
|
||||
def initlizeDefaultConfigPaths(
|
||||
self
|
||||
):
|
||||
|
||||
executable_path = sys.executable
|
||||
executable_dir = QFileInfo(executable_path).absoluteDir()
|
||||
self.__default_timer_tasks_config_path = QDir.toNativeSeparators(executable_dir.absoluteFilePath("timer_task.json"))
|
||||
|
||||
|
||||
def initializeTimerTasks(
|
||||
self
|
||||
) -> bool:
|
||||
|
||||
if not self.__timer_tasks_config_path:
|
||||
self.__timer_tasks_config_path = self.__default_timer_tasks_config_path
|
||||
if os.path.exists(self.__timer_tasks_config_path):
|
||||
timer_tasks = self.loadTimerTasks(self.__timer_tasks_config_path)
|
||||
if timer_tasks is not None:
|
||||
self.__timer_tasks = timer_tasks
|
||||
self.timerTasksChanged.emit()
|
||||
return True
|
||||
timer_tasks = []
|
||||
if self.saveTimerTasks(self.__timer_tasks_config_path, copy.deepcopy(timer_tasks)):
|
||||
timer_tasks = self.getTimerTasks()
|
||||
if timer_tasks is not None:
|
||||
self.__timer_tasks = timer_tasks
|
||||
self.timerTasksChanged.emit()
|
||||
return True
|
||||
timer_tasks = []
|
||||
if self.setTimerTasks(copy.deepcopy(timer_tasks)):
|
||||
self.__timer_tasks = timer_tasks
|
||||
self.updateTimerTaskList()
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def loadTimerTasks(
|
||||
self,
|
||||
timer_tasks_config_path: str
|
||||
def getTimerTasks(
|
||||
self
|
||||
) -> list:
|
||||
|
||||
try:
|
||||
if not timer_tasks_config_path or not os.path.exists(timer_tasks_config_path):
|
||||
raise Exception("定时任务配置文件不存在")
|
||||
timer_tasks = ConfigReader(timer_tasks_config_path).getConfigs()
|
||||
timer_tasks = self.__cfg_mgr.get(ConfigType.TIMERTASK)
|
||||
if timer_tasks and "timer_tasks" in timer_tasks:
|
||||
for task in timer_tasks["timer_tasks"]:
|
||||
task["add_time"] = datetime.strptime(task["add_time"], "%Y-%m-%d %H:%M:%S")
|
||||
@@ -234,23 +216,17 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
||||
return None
|
||||
|
||||
|
||||
def saveTimerTasks(
|
||||
def setTimerTasks(
|
||||
self,
|
||||
timer_tasks_config_path: str,
|
||||
timer_tasks: list
|
||||
) -> bool:
|
||||
|
||||
try:
|
||||
if not timer_tasks_config_path:
|
||||
raise Exception("配置文件路径为空")
|
||||
for task in timer_tasks:
|
||||
task["add_time"] = task["add_time"].strftime("%Y-%m-%d %H:%M:%S")
|
||||
task["execute_time"] = task["execute_time"].strftime("%Y-%m-%d %H:%M:%S")
|
||||
task["status"] = task["status"].value
|
||||
ConfigWriter(
|
||||
timer_tasks_config_path,
|
||||
{ "timer_tasks": timer_tasks }
|
||||
)
|
||||
self.__cfg_mgr.set(ConfigType.TIMERTASK, "", { "timer_tasks": timer_tasks })
|
||||
return True
|
||||
except Exception as e:
|
||||
QMessageBox.warning(
|
||||
@@ -470,7 +446,7 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
||||
self
|
||||
):
|
||||
|
||||
self.saveTimerTasks(self.__timer_tasks_config_path, copy.deepcopy(self.__timer_tasks))
|
||||
self.setTimerTasks(copy.deepcopy(self.__timer_tasks))
|
||||
self.updateTimerTaskList()
|
||||
self.updateStat()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user