The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
feat(script): 移除 `convert_to_pyqt5.py`, 添加 `convert_to_pyqt6.py`
XiaoYouChR5ca660a6d018
diff --git a/convert_to_pyqt5.py b/convert_to_pyqt5.pydeleted file mode 100644index 3340b431..00000000--- a/convert_to_pyqt5.py+++ /dev/null@@ -1,136 +0,0 @@-# -*- coding: utf-8 -*---import argparse-import re-import shutil-from pathlib import Path--"""-核心转换逻辑 (按执行顺序):-1. 底层绑定转换:- - `shiboken6` -> `sip`- - `wrapInstance` -> `wrapinstance`-2. 核心API转换:- - `Signal` -> `pyqtSignal`- - `Slot` -> `pyqtSlot`- - `Property` -> `pyqtProperty`-3. 枚举 (Enum) 语法转换 (最关键部分):- - `QClassName.EnumType.Value` -> `QClassName.Value` (例如: QFileDialog.FileMode.ExistingFiles -> QFileDialog.ExistingFiles)- - `Qt.EnumType.Value` -> `Qt.Value` (例如: Qt.AlignmentFlag.AlignCenter -> Qt.AlignCenter)-4. 全局模块名转换:- - `PySide6` -> `PyQt5`-"""--# 定义替换规则. 顺序至关重要, 从最具体到最通用, 确保不会错误替换.-REPLACEMENT_RULES = [- # 规则 1: 转换底层绑定 (shiboken6 -> sip).- # PyQt5使用sip, PySide6使用shiboken6. wrapInstance函数名也需小写.- (re.compile(r'\bshiboken6\b'), 'sip'),- (re.compile(r'\bwrapInstance\b'), 'wrapinstance'),-- # 规则 2: 转换 Signal, Slot 和 Property.- # 使用负向前瞻 `(?!pyqt)` 避免重复替换, 例如将 `pyqtSignal` 错误地再次处理.- (re.compile(r'\b(?!pyqt)Signal\b'), 'pyqtSignal'),- (re.compile(r'\b(?!pyqt)Slot\b'), 'pyqtSlot'),- (re.compile(r'\b(?!pyqt)Property\b'), 'pyqtProperty'),-- # 规则 3: 转换 PySide6 风格的枚举 (Enum). 这是确保运行正确的关键.- # 规则 3a: 处理 QClassName 内的枚举, 例如 `QFileDialog.FileMode.ExistingFiles`.- # 模式匹配: (QClassName).(EnumTypeName).(EnumValue) -> \1.\3 (即 QClassName.EnumValue).- (re.compile(r'\b(Q[A-Z][a-zA-Z0-9_]+)\.([A-Z][a-zA-Z]+)\.([a-zA-Z0-9_]+)\b'), r'\1.\3'),-- # 规则 3b: 处理 Qt 命名空间内的枚举, 例如 `Qt.AlignmentFlag.AlignCenter`.- # 负向前瞻 `(?!emit|...|)` 避免错误替换 Qt 的方法名.- (re.compile(r'\bQt\.([A-Z][a-zA-Z]+)\.(?!emit|connect|disconnect|sender)([a-zA-Z0-9_]+)\b'), r'Qt.\2'),-- # 规则 4: 全局替换模块名. 必须在所有规则之后执行.- (re.compile(r'PySide6'), 'PyQt5'),-]--def convert_file_content(content: str) -> str:- """对单个文件的内容应用所有替换规则."""- for pattern, replacement in REPLACEMENT_RULES:- content = pattern.sub(replacement, content)- return content--def process_directory(source_dir: Path, output_dir: Path):- """处理整个目录, 转换 .py 文件并复制其他文件."""- if not source_dir.is_dir():- print(f"❌ 错误: 源路径 '{source_dir}' 不是一个有效的目录.")- return-- print(f"🚀 开始转换项目...")- print(f" 源目录: {source_dir}")- print(f" 目标目录: {output_dir}")- print("-" * 40)-- file_converted_count = 0- file_copied_count = 0-- for item in source_dir.rglob('*'):- relative_path = item.relative_to(source_dir)- dest_path = output_dir / relative_path-- if item.is_dir():- dest_path.mkdir(parents=True, exist_ok=True)- continue-- dest_path.parent.mkdir(parents=True, exist_ok=True)-- if item.suffix == '.py':- try:- # 增强的容错性: 尝试以 utf-8 读取- with open(item, 'r', encoding='utf-8') as f_in:- original_content = f_in.read()-- converted_content = convert_file_content(original_content)-- with open(dest_path, 'w', encoding='utf-8') as f_out:- f_out.write(converted_content)- print(f"🔧 正在转换: {relative_path}")- file_converted_count += 1-- except UnicodeDecodeError:- print(f"⚠️ 警告: 文件 '{relative_path}' 不是 UTF-8 编码, 无法转换. 将直接复制.")- shutil.copy2(item, dest_path)- file_copied_count += 1- except Exception as e:- print(f"❌ 处理文件 '{item}' 时发生意外错误: {e}")- print(f" -> 将作为副本复制原始文件以确保安全.")- shutil.copy2(item, dest_path)- file_copied_count += 1- else:- print(f"📋 正在复制: {relative_path}")- shutil.copy2(item, dest_path)- file_copied_count += 1-- print("-" * 40)- print("✅ 转换完成!")- print(f" - {file_converted_count} 个 Python 文件已转换.")- print(f" - {file_copied_count} 个其他文件已复制.")- print(f" - 转换后的项目已保存到: {output_dir}")--def main():- parser = argparse.ArgumentParser(- description="自动将 PySide6 Python 项目转换为 PyQt5 项目.",- formatter_class=argparse.RawTextHelpFormatter- )- parser.add_argument("source_dir", type=str, help="包含 PySide6 项目的源目录.")- parser.add_argument(- "-o", "--output_dir", type=str,- help="用于保存转换后的 PyQt5 项目的目标目录.\n如果未提供, 将自动创建 '源目录名_pyqt5'."- )-- args = parser.parse_args()-- source_path = Path(args.source_dir).resolve()-- if args.output_dir:- output_path = Path(args.output_dir).resolve()- else:- output_path = source_path.parent / f"{source_path.name}_pyqt5"-- process_directory(source_path, output_path)--if __name__ == "__main__":- main()\ No newline at end of filediff --git a/convert_to_pyqt6.py b/convert_to_pyqt6.pynew file mode 100644index 00000000..ec75c9a0--- /dev/null+++ b/convert_to_pyqt6.py@@ -0,0 +1,362 @@+from __future__ import annotations++import argparse+import os+import re+import shutil+from pathlib import Path+++SCRIPT_NAME = Path(__file__).name+DEFAULT_BACKUP_DIR = ".convert_to_pyqt6_backup"+DIRECTORY_EXCLUDES = {+ ".git",+ ".hg",+ ".idea",+ ".mypy_cache",+ ".pytest_cache",+ ".ruff_cache",+ ".venv",+ "__pycache__",+ "build",+ "dist",+ "node_modules",+}+FILE_EXCLUDES = {+ "convert_to_pyqt5.py",+ SCRIPT_NAME,+ "uv.lock",+}+TEXT_FILE_EXTENSIONS = {".md", ".py", ".toml", ".txt"}++QTCORE_IMPORT_RE = re.compile(+ r"^(\s*from\s+PyQt6\.QtCore\s+import\s+)(.+?)(\s*(?:#.*)?\r?\n?)$",+ re.MULTILINE,+)+SIGNAL_CALL_RE = re.compile(r"(?<!pyqt)\bSignal(?=\s*\()")+SLOT_CALL_RE = re.compile(r"(?<!pyqt)\bSlot(?=\s*\()")+PROPERTY_CALL_RE = re.compile(r"(?<!pyqt)\bProperty(?=\s*\()")++SYNC_RESOLVE_TOOL_OLD = "def resolve_tool(tool_name: str) -> str:"+SYNC_RESOLVE_TOOL_RE = re.compile(+ r"def resolve_tool\(tool_name: str\) -> str:\r?\n.*?(?=\r?\ndef get_py_files)",+ re.DOTALL,+)+SYNC_RESOLVE_TOOL_NEW = """def resolve_tool(*tool_names: str) -> str:+ for tool_name in tool_names:+ executable = shutil.which(tool_name)+ if executable:+ return executable++ suffix = ".exe" if os.name == "nt" else ""+ candidate = Path(sys.executable).resolve().with_name(f"{tool_name}{suffix}")+ if candidate.exists():+ return str(candidate)++ formatted_names = ", ".join(tool_names)+ raise FileNotFoundError(f"Required tool was not found in PATH: {formatted_names}")+"""+++def replace_qtcore_imports(line_match: re.Match[str]) -> str:+ prefix, imports, suffix = line_match.groups()+ imports = re.sub(r"(?<!pyqt)\bSignal\b", "pyqtSignal", imports)+ imports = re.sub(r"(?<!pyqt)\bSlot\b", "pyqtSlot", imports)+ imports = re.sub(r"(?<!pyqt)\bProperty\b", "pyqtProperty", imports)+ return f"{prefix}{imports}{suffix}"+++def convert_python_content(content: str, relative_path: Path) -> str:+ converted = content.replace("PySide6", "PyQt6")+ converted = QTCORE_IMPORT_RE.sub(replace_qtcore_imports, converted)+ converted = SIGNAL_CALL_RE.sub("pyqtSignal", converted)+ converted = SLOT_CALL_RE.sub("pyqtSlot", converted)+ converted = PROPERTY_CALL_RE.sub("pyqtProperty", converted)+ converted = converted.replace("plugin-enable=pyside6", "plugin-enable=pyqt6")++ if relative_path.as_posix() == "sync_i18n_res.py":+ converted = convert_sync_i18n_res(converted)++ return converted+++def convert_sync_i18n_res(content: str) -> str:+ newline = "\r\n" if "\r\n" in content else "\n"++ if SYNC_RESOLVE_TOOL_OLD in content:+ resolve_tool_block = SYNC_RESOLVE_TOOL_NEW.replace("\n", newline)+ content = SYNC_RESOLVE_TOOL_RE.sub(+ f"{resolve_tool_block}{newline}",+ content,+ count=1,+ )++ content = content.replace(+ 'resolve_tool("pyside6-lupdate")',+ 'resolve_tool("pylupdate6", "lupdate", "pyside6-lupdate")',+ )+ content = content.replace(+ 'resolve_tool("pyside6-lrelease")',+ 'resolve_tool("lrelease", "pyside6-lrelease")',+ )+ content = content.replace(+ 'resolve_tool("pyside6-rcc")',+ 'resolve_tool("rcc", "pyside6-rcc")',+ )+ return content+++def convert_text_content(content: str) -> str:+ converted = content.replace("pyside6-fluent-widgets", "pyqt6-fluent-widgets")+ converted = converted.replace("pyside6", "pyqt6")+ converted = converted.replace("PySide6", "PyQt6")+ return converted+++def is_skipped(relative_path: Path) -> bool:+ if any(part in DIRECTORY_EXCLUDES for part in relative_path.parts[:-1]):+ return True++ if relative_path.name in FILE_EXCLUDES:+ return True++ if relative_path.parts and relative_path.parts[0] == DEFAULT_BACKUP_DIR:+ return True++ return False+++def is_text_file(path: Path, include_markdown: bool) -> bool:+ suffix = path.suffix.lower()+ if suffix not in TEXT_FILE_EXTENSIONS:+ return False++ if suffix == ".md" and not include_markdown:+ return False++ return True+++def iter_project_files(root_dir: Path, include_markdown: bool) -> list[Path]:+ files: list[Path] = []++ for current_root, dir_names, file_names in os.walk(root_dir):+ current_path = Path(current_root)+ relative_root = current_path.relative_to(root_dir)++ dir_names[:] = [+ name+ for name in dir_names+ if name not in DIRECTORY_EXCLUDES+ and name != DEFAULT_BACKUP_DIR+ ]++ for file_name in sorted(file_names):+ candidate = current_path / file_name+ relative_path = candidate.relative_to(root_dir)++ if is_skipped(relative_path):+ continue++ if is_text_file(candidate, include_markdown):+ files.append(candidate)++ return files+++def ensure_parent(path: Path) -> None:+ path.parent.mkdir(parents=True, exist_ok=True)+++def read_text(path: Path) -> str:+ with path.open("r", encoding="utf-8", newline="") as file:+ return file.read()+++def write_text(path: Path, content: str) -> None:+ ensure_parent(path)+ with path.open("w", encoding="utf-8", newline="") as file:+ file.write(content)+++def create_backup(source_root: Path, backup_root: Path, file_path: Path) -> None:+ relative_path = file_path.relative_to(source_root)+ backup_path = backup_root / relative_path++ if backup_path.exists():+ return++ ensure_parent(backup_path)+ shutil.copy2(file_path, backup_path)+++def convert_file_content(content: str, relative_path: Path) -> str:+ if relative_path.suffix.lower() == ".py":+ return convert_python_content(content, relative_path)++ return convert_text_content(content)+++def copy_project_tree(source_dir: Path, output_dir: Path) -> None:+ for current_root, dir_names, file_names in os.walk(source_dir):+ current_path = Path(current_root)+ relative_root = current_path.relative_to(source_dir)++ dir_names[:] = [+ name+ for name in dir_names+ if name not in DIRECTORY_EXCLUDES+ and name != DEFAULT_BACKUP_DIR+ ]++ destination_root = output_dir / relative_root+ destination_root.mkdir(parents=True, exist_ok=True)++ for file_name in file_names:+ source_path = current_path / file_name+ relative_path = source_path.relative_to(source_dir)+ if is_skipped(relative_path):+ continue++ destination_path = output_dir / relative_path+ ensure_parent(destination_path)+ shutil.copy2(source_path, destination_path)+++def process_directory(+ source_dir: Path,+ output_dir: Path | None,+ *,+ in_place: bool,+ dry_run: bool,+ backup_dir_name: str,+ include_markdown: bool,+) -> int:+ changed_files = 0+ inspected_files = 0+ backup_root = source_dir / backup_dir_name++ if output_dir is not None and not in_place and not dry_run:+ copy_project_tree(source_dir, output_dir)++ for file_path in iter_project_files(source_dir, include_markdown=include_markdown):+ relative_path = file_path.relative_to(source_dir)+ inspected_files += 1++ try:+ original_content = read_text(file_path)+ except UnicodeDecodeError:+ print(f"[skip] {relative_path} is not UTF-8 text")+ continue++ converted_content = convert_file_content(original_content, relative_path)+ if converted_content == original_content:+ continue++ changed_files += 1+ destination = file_path if in_place else output_dir / relative_path+ print(f"[change] {relative_path}")++ if dry_run:+ continue++ if in_place and backup_dir_name:+ create_backup(source_dir, backup_root, file_path)++ write_text(destination, converted_content)++ print(+ f"Done. inspected={inspected_files}, changed={changed_files}, "+ f"mode={'in-place' if in_place else 'copy'}"+ )++ if in_place and changed_files and not dry_run and backup_dir_name:+ print(f"Backups saved to: {backup_root}")++ if output_dir is not None and not in_place:+ print(f"Converted project saved to: {output_dir}")++ return 0+++def parse_args() -> argparse.Namespace:+ parser = argparse.ArgumentParser(+ description="Convert a PySide6 project to PyQt6 with minimal code changes.",+ )+ parser.add_argument(+ "source_dir",+ nargs="?",+ default=".",+ help="Project root to convert. Defaults to the current directory.",+ )+ parser.add_argument(+ "-o",+ "--output-dir",+ help="Write the converted project to this directory. Ignored with --in-place.",+ )+ parser.add_argument(+ "--in-place",+ action="store_true",+ help="Rewrite the project in place instead of writing a copy.",+ )+ parser.add_argument(+ "--dry-run",+ action="store_true",+ help="Report the files that would change without writing them.",+ )+ parser.add_argument(+ "--backup-dir",+ default=DEFAULT_BACKUP_DIR,+ help=(+ "Backup directory used with --in-place. "+ "Use an empty string to disable backups."+ ),+ )+ parser.add_argument(+ "--include-markdown",+ action="store_true",+ help="Also rewrite Markdown documentation files.",+ )+ return parser.parse_args()+++def main() -> int:+ args = parse_args()+ source_dir = Path(args.source_dir).resolve()++ if not source_dir.is_dir():+ raise SystemExit(f"Source directory does not exist: {source_dir}")++ if args.in_place and args.output_dir:+ raise SystemExit("--output-dir cannot be used together with --in-place")++ if args.in_place:+ output_dir = None+ elif args.output_dir:+ output_dir = Path(args.output_dir).resolve()+ else:+ output_dir = source_dir.parent / f"{source_dir.name}_pyqt6"++ if output_dir is not None and output_dir == source_dir:+ raise SystemExit("Output directory must be different from the source directory")++ if output_dir is not None:+ try:+ output_dir.relative_to(source_dir)+ except ValueError:+ pass+ else:+ raise SystemExit("Output directory must not be inside the source directory")++ return process_directory(+ source_dir=source_dir,+ output_dir=output_dir,+ in_place=args.in_place,+ dry_run=args.dry_run,+ backup_dir_name=args.backup_dir,+ include_markdown=args.include_markdown,+ )+++if __name__ == "__main__":+ raise SystemExit(main())