1
1
mirror of https://github.com/KenanZhu/AutoLibrary.git synced 2026-06-17 23:13:03 +08:00

refactor(ALConfigWidget, ALTimerTaskManageWidget): 优化界面的错误异常处理

This commit is contained in:
2026-02-21 15:38:56 +08:00
parent 169de92d5b
commit a1ff85256a
3 changed files with 97 additions and 37 deletions
+58 -26
View File
@@ -301,21 +301,36 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
run_config: dict
):
self.HostUrlEdit.setText(run_config["library"]["host_url"])
self.LoginUrlEdit.setText(run_config["library"]["login_url"])
self.AutoCaptchaCheckBox.setChecked(run_config["login"]["auto_captcha"])
self.LoginAttemptSpinBox.setValue(run_config["login"]["max_attempt"])
self.BrowserTypeComboBox.setCurrentText(run_config["web_driver"]["driver_type"])
if run_config["web_driver"]["driver_path"]:
driver_path = os.path.abspath(run_config["web_driver"]["driver_path"])
else:
driver_path = ""
self.BrowseBrowserDriverEdit.setText(QDir.toNativeSeparators(driver_path))
self.HeadlessCheckBox.setChecked(run_config["web_driver"]["headless"])
run_mode = run_config["mode"]["run_mode"]
self.AutoReserveCheckBox.setChecked(run_mode&0x01)
self.AutoCheckinCheckBox.setChecked(run_mode&0x02)
self.AutoRenewalCheckBox.setChecked(run_mode&0x04)
try:
self.HostUrlEdit.setText(run_config["library"]["host_url"])
self.LoginUrlEdit.setText(run_config["library"]["login_url"])
self.AutoCaptchaCheckBox.setChecked(run_config["login"]["auto_captcha"])
self.LoginAttemptSpinBox.setValue(run_config["login"]["max_attempt"])
self.BrowserTypeComboBox.setCurrentText(run_config["web_driver"]["driver_type"])
if run_config["web_driver"]["driver_path"]:
driver_path = os.path.abspath(run_config["web_driver"]["driver_path"])
else:
driver_path = ""
self.BrowseBrowserDriverEdit.setText(QDir.toNativeSeparators(driver_path))
self.HeadlessCheckBox.setChecked(run_config["web_driver"]["headless"])
run_mode = run_config["mode"]["run_mode"]
self.AutoReserveCheckBox.setChecked(run_mode&0x01)
self.AutoCheckinCheckBox.setChecked(run_mode&0x02)
self.AutoRenewalCheckBox.setChecked(run_mode&0x04)
except KeyError as e:
QMessageBox.warning(
self,
"警告 - AutoLibrary",
f"运行配置文件: {self.__config_paths['run']}\n"
f"读取时键 '{e}' 发生错误,文件可能被意外修改或已经损坏\n"
)
except Exception as e:
QMessageBox.warning(
self,
"警告 - AutoLibrary",
f"运行配置文件: {self.__config_paths['run']}\n"
f"读取时键 '{e}' 发生未知错误,文件可能被意外修改或已经损坏\n"
)
def initilizeUserInfoWidget(
@@ -423,12 +438,19 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
self.ExpectRenewDurationSpinBox.setValue(user["reserve_info"]["renew_time"]["expect_duration"])
self.MaxRenewTimeDiffSpinBox.setValue(user["reserve_info"]["renew_time"]["max_diff"])
self.PreferLateRenewTimeCheckBox.setChecked(not user["reserve_info"]["renew_time"]["prefer_early"])
except:
except KeyError as e:
QMessageBox.warning(
self,
"警告 - AutoLibrary",
"用户配置文件读取发生错误 !\n"\
f"用户: {user['username']} 配置文件可能已损坏"
f"用户配置文件: {self.__config_paths['user']}\n"\
f"读取时键 '{e}' 发生错误,文件可能被意外修改或已经损坏\n"
)
except Exception as e:
QMessageBox.warning(
self,
"警告 - AutoLibrary",
f"用户配置文件: {self.__config_paths['user']}\n"\
f"读取时发生未知错误 '{e}',文件可能被意外修改或已经损坏\n"
)
@@ -454,6 +476,20 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
user_item.setCheckState(1, Qt.Checked if user_config.get("enabled", True) else Qt.Unchecked)
user_item.setDisabled(not group_config.get("enabled", True))
group_item.setExpanded(True)
except KeyError as e:
QMessageBox.warning(
self,
"警告 - AutoLibrary",
f"用户配置文件: {self.__config_paths['user']}\n"\
f"读取时键 '{e}' 发生错误,文件可能被意外修改或已经损坏\n"
)
except Exception as e:
QMessageBox.warning(
self,
"警告 - AutoLibrary",
f"用户配置文件: {self.__config_paths['user']}\n"\
f"读取时发生未知错误 '{e}',文件可能被意外修改或已经损坏\n"
)
finally:
self.UserTreeWidget.itemChanged.connect(self.onUserTreeWidgetItemChanged)
@@ -476,8 +512,7 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
QMessageBox.warning(
self,
"警告 - AutoLibrary",
f"运行配置文件读取发生错误 ! : {e}\n"\
f"文件路径: {run_config_path}"
f"运行配置文件读取发生错误 ! : \n{e}"
)
return None
@@ -499,8 +534,7 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
QMessageBox.warning(
self,
"警告 - AutoLibrary",
f"配置文件写入发生错误 ! : {e}\n"\
f"文件路径: {run_config_path}"
f"配置文件写入发生错误 ! : \n{e}"
)
return False
@@ -533,8 +567,7 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
QMessageBox.warning(
self,
"警告 - AutoLibrary",
f"用户配置文件读取发生错误 ! : {e}\n"\
f"文件路径: {user_config_path}"
f"用户配置文件读取发生错误 ! : \n{e}"
)
return None
@@ -556,8 +589,7 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
QMessageBox.warning(
self,
"警告 - AutoLibrary",
f"用户配置文件写入发生错误 ! : {e}\n"\
f"文件路径: \n{user_config_path}"
f"用户配置文件写入发生错误 ! : \n{e}"
)
return False
+13 -2
View File
@@ -15,7 +15,7 @@ from PySide6.QtCore import (
Qt, Signal, Slot, QDir, QFileInfo, QTimer, QUrl,
)
from PySide6.QtWidgets import (
QMainWindow, QMenu, QSystemTrayIcon
QMainWindow, QMenu, QSystemTrayIcon, QMessageBox
)
from PySide6.QtGui import (
QTextCursor, QCloseEvent, QFont, QIcon, QDesktopServices
@@ -78,7 +78,18 @@ class ALMainWindow(MsgBase, QMainWindow, Ui_ALMainWindow):
self.AboutAction.triggered.connect(self.onAboutActionTriggered)
# initialize timer task widget, but not show it
self.__alTimerTaskWidget = ALTimerTaskManageWidget(self, self.__config_paths["timer_task"])
try:
self.__alTimerTaskWidget = ALTimerTaskManageWidget(self, self.__config_paths["timer_task"])
except Exception as e:
QMessageBox.critical(
self,
"错误 - AutoLibrary",
f"初始化定时任务功能失败: \n{e}"
)
self.__alTimerTaskWidget = None
self.TimerTaskWidgetButton.setEnabled(False)
self.TimerTaskWidgetButton.setToolTip("定时任务功能初始化失败, 请检查配置文件。")
return
self.timerTaskIsRunning.connect(self.__alTimerTaskWidget.onTimerTaskIsRunning)
self.timerTaskIsExecuted.connect(self.__alTimerTaskWidget.onTimerTaskIsExecuted)
self.timerTaskIsError.connect(self.__alTimerTaskWidget.onTimerTaskIsError)
+26 -9
View File
@@ -8,13 +8,14 @@ You may use, modify, and distribute this file under the terms of the MIT License
See the LICENSE file for details.
"""
import os
import sys
import copy
from enum import Enum
from datetime import datetime, timedelta
from PySide6.QtCore import (
Qt, Signal, Slot, QTimer
Qt, Signal, Slot, QTimer, QFileInfo, QDir
)
from PySide6.QtWidgets import (
QDialog, QWidget, QListWidgetItem, QMessageBox,
@@ -179,15 +180,27 @@ 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:
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
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)):
self.__timer_tasks = timer_tasks
@@ -212,7 +225,12 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
task["status"] = ALTimerTaskStatus(task["status"])
return timer_tasks["timer_tasks"]
raise Exception("定时任务配置文件格式错误")
except Exception:
except Exception as e:
QMessageBox.warning(
self,
"警告 - AutoLibrary",
f"加载定时任务配置发生错误 ! : \n{e}"
)
return None
@@ -238,8 +256,7 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
QMessageBox.warning(
self,
"警告 - AutoLibrary",
f"保存定时任务配置发生错误 ! : {e}\n"\
f"文件路径: {timer_tasks_config_path}"
f"保存定时任务配置发生错误 ! : \n{e}"
)
return False