1
1
mirror of https://github.com/KenanZhu/AutoLibrary.git synced 2026-06-18 15:33:03 +08:00

refactor(WebDriverManager, ALWebDriverDownloadDialog): 重命名驱动状态枚举并完善对话框状态感知

This commit is contained in:
2026-03-21 17:22:25 +08:00
parent 5c393595d7
commit 2c90008fcd
2 changed files with 95 additions and 55 deletions
+81 -41
View File
@@ -24,7 +24,8 @@ from PySide6.QtGui import (
from managers.driver.WebDriverManager import ( from managers.driver.WebDriverManager import (
instance as webdriver_manager_instance, instance as webdriver_manager_instance,
WebDriverManager, WebDriverInfo, WebDriverType WebDriverManager, WebDriverInfo, WebDriverType,
WebDriverStatus
) )
from gui.ALStatusLabel import ALStatusLabel from gui.ALStatusLabel import ALStatusLabel
@@ -141,6 +142,7 @@ class ALWebDriverDownloadDialog(QDialog):
self.initializeDriverManager() self.initializeDriverManager()
self.refreshDriverList() self.refreshDriverList()
def showEvent( def showEvent(
self, self,
event event
@@ -163,6 +165,7 @@ class ALWebDriverDownloadDialog(QDialog):
self.move(target_pos) self.move(target_pos)
return result return result
def setupUi( def setupUi(
self self
): ):
@@ -267,13 +270,16 @@ class ALWebDriverDownloadDialog(QDialog):
self.__driver_manager.refresh() self.__driver_manager.refresh()
self.__driver_infos = self.__driver_manager.getDriverInfos() self.__driver_infos = self.__driver_manager.getDriverInfos()
self.DriverComboBox.clear() self.DriverComboBox.clear()
for driver_info in self.__driver_infos: installed_idx = 0
for i, driver_info in enumerate(self.__driver_infos):
if driver_info.driver_status == WebDriverStatus.INSTALLED:
installed_idx = i # get the installed driver index
display_text = f"{driver_info.driver_type.value} - {driver_info.browser_version}" display_text = f"{driver_info.driver_type.value} - {driver_info.browser_version}"
self.DriverComboBox.addItem(display_text) self.DriverComboBox.addItem(display_text)
count = len(self.__driver_infos) count = len(self.__driver_infos)
self.BrowserCountLabel.setText(f"检测到 {count} 个可用浏览器:") self.BrowserCountLabel.setText(f"检测到 {count} 个可用浏览器:")
if self.__driver_infos: if self.__driver_infos:
self.onDriverComboBoxChanged(0) self.DriverComboBox.setCurrentIndex(installed_idx)
def onDriverComboBoxChanged( def onDriverComboBoxChanged(
@@ -285,6 +291,7 @@ class ALWebDriverDownloadDialog(QDialog):
return return
driver_info = self.__driver_infos[index] driver_info = self.__driver_infos[index]
self.updateDriverInfoDisplay(driver_info) self.updateDriverInfoDisplay(driver_info)
self.updateProgressBarStates(driver_info)
self.updateButtonStates(driver_info) self.updateButtonStates(driver_info)
@@ -328,18 +335,21 @@ class ALWebDriverDownloadDialog(QDialog):
self self
): ):
self.DriverComboBox.setEnabled(False)
index = self.DriverComboBox.currentIndex() index = self.DriverComboBox.currentIndex()
if index < 0 or index >= len(self.__driver_infos): if index < 0 or index >= len(self.__driver_infos):
return return
driver_info = self.__driver_infos[index] driver_info = self.__driver_infos[index]
if driver_info.driver_status.name == "INSTALLED": if driver_info.driver_status == WebDriverStatus.INSTALLED:
return return
self.StatusLabel.status = ALStatusLabel.Status.RUNNING driver_info.driver_status = WebDriverStatus.DOWNLOADING # we set this only to update
self.DownloadButton.setEnabled(False) # the display, and we will set to not installed in the download thread
self.RefreshButton.setEnabled(False) self.updateDriverInfoDisplay(driver_info)
self.DriverComboBox.setEnabled(False) self.updateProgressBarStates(driver_info)
self.ProgressBar.setValue(0) self.ProgressText.setText("准备开始下载...")
self.ProgressText.setText("正在下载驱动...") self.updateButtonStates(driver_info)
# set to not installed
driver_info.driver_status = WebDriverStatus.NOT_INSTALLED
self.__download_thread = DownloadWorker(self.__driver_manager, driver_info) self.__download_thread = DownloadWorker(self.__driver_manager, driver_info)
self.__download_thread.progress.connect(self.onDownloadProgress) self.__download_thread.progress.connect(self.onDownloadProgress)
self.__download_thread.downloadFinished.connect(self.onDownloadFinished) self.__download_thread.downloadFinished.connect(self.onDownloadFinished)
@@ -371,18 +381,14 @@ class ALWebDriverDownloadDialog(QDialog):
self self
): ):
self.ProgressBar.setValue(100) self.DriverComboBox.setEnabled(True)
self.ProgressText.setText("下载完成 !")
self.StatusLabel.status = ALStatusLabel.Status.SUCCESS
index = self.DriverComboBox.currentIndex() index = self.DriverComboBox.currentIndex()
if 0 <= index < len(self.__driver_infos): if 0 <= index < len(self.__driver_infos):
driver_info = self.__driver_infos[index] driver_info = self.__driver_infos[index]
driver_info.driver_status = WebDriverStatus.INSTALLED
self.updateDriverInfoDisplay(driver_info) self.updateDriverInfoDisplay(driver_info)
self.ConfirmButton.setEnabled(True) self.updateProgressBarStates(driver_info)
self.DownloadButton.setEnabled(False) self.updateButtonStates(driver_info)
self.RefreshButton.setEnabled(True)
self.DriverComboBox.setEnabled(True)
self.DeleteButton.setEnabled(True)
@Slot() @Slot()
def onDownloadError( def onDownloadError(
@@ -390,12 +396,15 @@ class ALWebDriverDownloadDialog(QDialog):
error_message: str error_message: str
): ):
self.StatusLabel.status = ALStatusLabel.Status.FAILURE
QMessageBox.critical(self, "下载失败 - AutoLibrary", error_message)
self.DownloadButton.setEnabled(True)
self.RefreshButton.setEnabled(True)
self.DriverComboBox.setEnabled(True) self.DriverComboBox.setEnabled(True)
self.CancelButton.setEnabled(True) index = self.DriverComboBox.currentIndex()
if 0 <= index < len(self.__driver_infos):
driver_info = self.__driver_infos[index]
driver_info.driver_status = WebDriverStatus.ERROR
self.updateDriverInfoDisplay(driver_info)
self.updateProgressBarStates(driver_info)
self.updateButtonStates(driver_info)
QMessageBox.critical(self, "下载失败 - AutoLibrary", error_message)
@Slot() @Slot()
@@ -403,19 +412,16 @@ class ALWebDriverDownloadDialog(QDialog):
self self
): ):
self.DriverComboBox.setEnabled(True)
index = self.DriverComboBox.currentIndex() index = self.DriverComboBox.currentIndex()
if 0 <= index < len(self.__driver_infos): if 0 <= index < len(self.__driver_infos):
driver_info = self.__driver_infos[index] driver_info = self.__driver_infos[index]
self.__driver_manager.cancelDriverDownload(driver_info) self.__driver_manager.cancelDriverDownload(driver_info)
driver_info.driver_status = WebDriverStatus.NOT_INSTALLED
self.updateDriverInfoDisplay(driver_info) self.updateDriverInfoDisplay(driver_info)
self.updateProgressBarStates(driver_info)
self.updateButtonStates(driver_info)
self.ProgressText.setText("下载已取消") self.ProgressText.setText("下载已取消")
self.ProgressBar.setValue(0)
self.StatusLabel.status = ALStatusLabel.Status.WAITING
self.DownloadButton.setEnabled(True)
self.RefreshButton.setEnabled(True)
self.DriverComboBox.setEnabled(True)
self.CancelButton.setEnabled(True)
self.DeleteButton.setEnabled(False)
@Slot() @Slot()
@@ -427,7 +433,7 @@ class ALWebDriverDownloadDialog(QDialog):
if index < 0 or index >= len(self.__driver_infos): if index < 0 or index >= len(self.__driver_infos):
return return
driver_info = self.__driver_infos[index] driver_info = self.__driver_infos[index]
if driver_info.driver_status.name != "INSTALLED": if driver_info.driver_status != WebDriverStatus.INSTALLED:
return return
self.__selected_driver_info = driver_info self.__selected_driver_info = driver_info
self.__confirmed = True self.__confirmed = True
@@ -510,27 +516,61 @@ class ALWebDriverDownloadDialog(QDialog):
self.PathLabel.setText(str(driver_info.driver_path)) self.PathLabel.setText(str(driver_info.driver_path))
else: else:
self.PathLabel.setText("未安装") self.PathLabel.setText("未安装")
match driver_info.driver_status.name: match driver_info.driver_status:
case "NOT_INSTALLED": case WebDriverStatus.NOT_INSTALLED:
self.StatusLabel.status = ALStatusLabel.Status.WAITING self.StatusLabel.status = ALStatusLabel.Status.WAITING
case "INSTALLED": case WebDriverStatus.INSTALLED:
self.StatusLabel.status = ALStatusLabel.Status.SUCCESS self.StatusLabel.status = ALStatusLabel.Status.SUCCESS
case "DOWNLOADING": case WebDriverStatus.DOWNLOADING:
self.StatusLabel.status = ALStatusLabel.Status.RUNNING self.StatusLabel.status = ALStatusLabel.Status.RUNNING
case "ERROR": case WebDriverStatus.ERROR:
self.StatusLabel.status = ALStatusLabel.Status.FAILURE self.StatusLabel.status = ALStatusLabel.Status.FAILURE
def updateProgressBarStates(
self,
driver_info: WebDriverInfo
):
if driver_info.driver_status == WebDriverStatus.NOT_INSTALLED:
self.ProgressBar.setValue(0)
self.ProgressText.setText("未安装")
elif driver_info.driver_status == WebDriverStatus.INSTALLED:
self.ProgressBar.setValue(100)
self.ProgressText.setText("已安装")
elif driver_info.driver_status == WebDriverStatus.DOWNLOADING:
pass # update by worker thread
elif driver_info.driver_status == WebDriverStatus.ERROR:
self.ProgressBar.setValue(0)
self.ProgressText.setText("下载失败")
def updateButtonStates( def updateButtonStates(
self, self,
driver_info: WebDriverInfo driver_info: WebDriverInfo
): ):
if driver_info.driver_status.name == "INSTALLED": if driver_info.driver_status == WebDriverStatus.NOT_INSTALLED:
self.DownloadButton.setEnabled(False) self.RefreshButton.setEnabled(True)
self.DeleteButton.setEnabled(True)
self.ConfirmButton.setEnabled(True)
else:
self.DeleteButton.setEnabled(False) self.DeleteButton.setEnabled(False)
self.DownloadButton.setEnabled(True) self.DownloadButton.setEnabled(True)
self.CancelButton.setEnabled(True)
self.ConfirmButton.setEnabled(False)
elif driver_info.driver_status == WebDriverStatus.INSTALLED:
self.RefreshButton.setEnabled(True)
self.DownloadButton.setEnabled(False)
self.DeleteButton.setEnabled(True)
self.CancelButton.setEnabled(True)
self.ConfirmButton.setEnabled(True)
elif driver_info.driver_status == WebDriverStatus.DOWNLOADING:
self.RefreshButton.setEnabled(False)
self.DownloadButton.setEnabled(False)
self.DeleteButton.setEnabled(False)
self.CancelButton.setEnabled(True)
self.ConfirmButton.setEnabled(False)
elif driver_info.driver_status == WebDriverStatus.ERROR:
self.RefreshButton.setEnabled(True)
self.DownloadButton.setEnabled(True)
self.DeleteButton.setEnabled(False)
self.CancelButton.setEnabled(True)
self.ConfirmButton.setEnabled(False) self.ConfirmButton.setEnabled(False)
+13 -13
View File
@@ -24,7 +24,7 @@ from managers.driver.WebDriverDownloader import (
) )
class DriverStatus(Enum): class WebDriverStatus(Enum):
""" """
Web driver status. Web driver status.
""" """
@@ -57,7 +57,7 @@ class WebDriverInfo:
self.driver_version = "" self.driver_version = ""
self.browser_version = "" self.browser_version = ""
self.driver_path: Optional[Path] = None self.driver_path: Optional[Path] = None
self.driver_status = DriverStatus.NOT_INSTALLED self.driver_status = WebDriverStatus.NOT_INSTALLED
class WebDriverManager: class WebDriverManager:
@@ -115,7 +115,7 @@ class WebDriverManager:
driver_path = self._getDriverPath(driver_info) driver_path = self._getDriverPath(driver_info)
if driver_path and driver_path.exists() and driver_path.is_file(): if driver_path and driver_path.exists() and driver_path.is_file():
driver_info.driver_path = driver_path driver_info.driver_path = driver_path
driver_info.driver_status = DriverStatus.INSTALLED driver_info.driver_status = WebDriverStatus.INSTALLED
def _mapWebBrowserTypeToDriver( def _mapWebBrowserTypeToDriver(
@@ -321,7 +321,7 @@ class WebDriverManager:
driver_info: WebDriverInfo driver_info: WebDriverInfo
) -> Optional[Path]: ) -> Optional[Path]:
if driver_info and driver_info.driver_status == DriverStatus.INSTALLED: if driver_info and driver_info.driver_status == WebDriverStatus.INSTALLED:
return driver_info.driver_path return driver_info.driver_path
return None return None
@@ -339,7 +339,7 @@ class WebDriverManager:
progress_callback(0, 0, 0, "未找到浏览器信息") progress_callback(0, 0, 0, "未找到浏览器信息")
else: else:
raise ValueError("未找到浏览器信息") raise ValueError("未找到浏览器信息")
if driver_info and driver_info.driver_status == DriverStatus.DOWNLOADING: if driver_info and driver_info.driver_status == WebDriverStatus.DOWNLOADING:
if progress_callback: if progress_callback:
progress_callback(0, 0, 0, f"{driver_info.driver_type} 驱动正在下载中") progress_callback(0, 0, 0, f"{driver_info.driver_type} 驱动正在下载中")
else: else:
@@ -375,19 +375,19 @@ class WebDriverManager:
else: else:
raise ValueError(f"不支持的 Web Driver 类型") raise ValueError(f"不支持的 Web Driver 类型")
with self.__lock: with self.__lock:
driver_info.driver_status = DriverStatus.DOWNLOADING driver_info.driver_status = WebDriverStatus.DOWNLOADING
driver_path = downloader.download(progress_callback=progress_callback, cancel_event=cancel_event) driver_path = downloader.download(progress_callback=progress_callback, cancel_event=cancel_event)
with self.__lock: with self.__lock:
if driver_path: if driver_path:
driver_info.driver_path = driver_path driver_info.driver_path = driver_path
driver_info.driver_version = driver_version driver_info.driver_version = driver_version
driver_info.driver_status = DriverStatus.INSTALLED driver_info.driver_status = WebDriverStatus.INSTALLED
else: else:
driver_info.driver_status = DriverStatus.ERROR driver_info.driver_status = WebDriverStatus.ERROR
return driver_path return driver_path
except Exception as e: except Exception as e:
with self.__lock: with self.__lock:
driver_info.driver_status = DriverStatus.ERROR driver_info.driver_status = WebDriverStatus.ERROR
raise e raise e
@@ -406,7 +406,7 @@ class WebDriverManager:
shutil.rmtree(download_dir, ignore_errors=True) shutil.rmtree(download_dir, ignore_errors=True)
with self.__lock: with self.__lock:
driver_info.driver_path = None driver_info.driver_path = None
driver_info.driver_status = DriverStatus.NOT_INSTALLED driver_info.driver_status = WebDriverStatus.NOT_INSTALLED
return True return True
except Exception: except Exception:
return False return False
@@ -424,7 +424,7 @@ class WebDriverManager:
progress_callback(0, 0, 0, "未找到浏览器信息") progress_callback(0, 0, 0, "未找到浏览器信息")
else: else:
raise ValueError("未找到浏览器信息") raise ValueError("未找到浏览器信息")
if driver_info.driver_status != DriverStatus.INSTALLED: if driver_info.driver_status != WebDriverStatus.INSTALLED:
if progress_callback: if progress_callback:
progress_callback(0, 0, 0, f"{driver_info.driver_type} 驱动未安装") progress_callback(0, 0, 0, f"{driver_info.driver_type} 驱动未安装")
else: else:
@@ -434,11 +434,11 @@ class WebDriverManager:
driver_path.unlink() driver_path.unlink()
with self.__lock: with self.__lock:
driver_info.driver_path = None driver_info.driver_path = None
driver_info.driver_status = DriverStatus.NOT_INSTALLED driver_info.driver_status = WebDriverStatus.NOT_INSTALLED
return True return True
except Exception: except Exception:
with self.__lock: with self.__lock:
driver_info.driver_status = DriverStatus.ERROR driver_info.driver_status = WebDriverStatus.ERROR
raise raise