1
1
mirror of https://github.com/KenanZhu/AutoLibrary.git synced 2026-06-18 07:23:03 +08:00
Files
AutoLibrary/src/managers/driver/WebBrowserDetector.py
T
KenanZhu 5e898180c7 refactor(style): 统一项目代码风格,整理导入顺序、间距规范与方法排列
- GUI 模块统一 QtCore → QtGui → QtWidgets 导入排列,各类独占一行按字母排序
- 统一类间两空行、类内方法间一空行、函数间一空行的间距规范
- 统一方法排列顺序:__init__ → setupUi → connectSignals → public → Slot → private
- 统一 _widgets 中 ConditionRowFrame/ActionStepFrame 方法命名(populate* / toScript / updateValueWidget)
- LibTimeSelector 迁入 operators/abs 抽象层

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 13:14:27 +08:00

183 lines
4.9 KiB
Python

# -*- coding: utf-8 -*-
"""
Copyright (c) 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 platform
import browsers
from pathlib import Path
from enum import Enum
from dataclasses import dataclass
class WebBrowserType(Enum):
"""
Web browser type
"""
CHROME = "chrome"
FIREFOX = "firefox"
EDGE = "edge"
class WebBrowserArch(Enum):
"""
Web browser architecture
"""
WINX86_32 = 0
WINX86_64 = 1
WINARM = 2
LINUXX86_32 = 3
LINUXX86_64 = 4
LINUXARM = 5
MACX86_64 = 6
MACARM = 7
@dataclass
class WebBrowserInfo:
"""
Web browser information
Attributes:
browser_arch (WebBrowserArch): Web browser architecture
browser_type (WebBrowserType): Web browser type
browser_version (str): Web browser version
browser_path (Path): Web browser executable file path
"""
browser_arch: WebBrowserArch
browser_type: WebBrowserType
browser_version: str
browser_path: Path
class WebBrowserArchDetector:
"""
Web browser architecture detector
"""
def __init__(
self
):
pass
def detect(
self
) -> WebBrowserArch:
"""
Detect system architecture
Returns:
WebBrowserArch: System architecture
"""
system = platform.system()
machine = platform.machine().lower()
if system == "Windows":
if machine in ["amd64", "x86_64"]:
return WebBrowserArch.WINX86_64
elif machine in ["i386", "i686", "x86"]:
return WebBrowserArch.WINX86_32
elif machine in ["arm64", "aarch64"]:
return WebBrowserArch.WINARM
else:
return WebBrowserArch.WINX86_64
elif system == "Darwin":
if machine in ["arm64", "aarch64"]:
return WebBrowserArch.MACARM
else:
return WebBrowserArch.MACX86_64
elif system == "Linux":
if machine in ["amd64", "x86_64"]:
return WebBrowserArch.LINUXX86_64
elif machine in ["i386", "i686", "x86"]:
return WebBrowserArch.LINUXX86_32
elif machine in ["arm64", "aarch64"]:
return WebBrowserArch.LINUXARM
elif machine.startswith("arm"):
return WebBrowserArch.LINUXARM
else:
return WebBrowserArch.LINUXX86_64
raise ValueError(f"不支持的系统架构 : {system} {machine}")
class WebBrowserDetector:
"""
Web browser detector
"""
def __init__(
self
):
self.browser_arch = WebBrowserArchDetector().detect()
self.browser_infos : list[WebBrowserInfo] = []
def detect(
self
) -> list[WebBrowserInfo]:
"""
Detect installed web browsers on the system.
Returns:
list[WebBrowserInfo]: List of detected browser information objects.
"""
self.browser_infos = []
try:
all_browsers = list(browsers.browsers())
except Exception as e:
self.browser_infos = []
return self.browser_infos
# Mapping from internal library name to our enum
type_map = {
'chrome': WebBrowserType.CHROME,
'firefox': WebBrowserType.FIREFOX,
'msedge': WebBrowserType.EDGE,
}
for browser in all_browsers:
internal_name = browser.get("browser_type", "").lower()
if internal_name not in type_map:
continue # Not one of the browsers we care about
version = browser.get("version", "")
if not version:
# Skip browsers with no version info (unlikely, but defensive)
continue
exe_path = browser.get("path", "")
if not exe_path:
continue
try:
path = Path(exe_path)
if not path.is_file():
continue
except Exception:
continue # Invalid path
info = WebBrowserInfo(
browser_arch=self.browser_arch, # Use system architecture as fallback
browser_type=type_map[internal_name],
browser_version=version,
browser_path=path,
)
self.browser_infos.append(info)
# Deduplicate: keep only one entry per (type, version)
seen = set()
unique = []
for info in self.browser_infos:
key = (info.browser_type, info.browser_version)
if key not in seen:
seen.add(key)
unique.append(info)
self.browser_infos = unique
return self.browser_infos