优化程序
This commit is contained in:
1 parent
68e8eb91a7
commit
6df29c9254
27 files changed
+1974
-203
No files matched your search
+24
-4
@@ -1,12 +1,35 @@
|
||||
"""将用户选中的 .lnk / .url 解析为实际要存储的路径(目标程序或 URL)。"""
|
||||
import base64
|
||||
import json
|
||||
import locale
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def _decode_subprocess_output(raw: bytes) -> str:
|
||||
"""
|
||||
解码子进程输出:优先用系统默认编码,兼容中文 Windows 上 PowerShell 的 GBK 输出,
|
||||
同时也能正确处理 UTF-8 等情况。
|
||||
"""
|
||||
if not raw:
|
||||
return ""
|
||||
# 先尝试系统默认编码(中文 Windows 上是 GBK)
|
||||
encodings_to_try = []
|
||||
sys_enc = locale.getpreferredencoding(False)
|
||||
if sys_enc:
|
||||
encodings_to_try.append(sys_enc)
|
||||
# 再尝试 UTF-8(带/不带 BOM)
|
||||
encodings_to_try.extend(["utf-8-sig", "utf-8", "gbk"])
|
||||
for enc in encodings_to_try:
|
||||
try:
|
||||
return raw.decode(enc)
|
||||
except (UnicodeDecodeError, LookupError):
|
||||
continue
|
||||
return raw.decode("utf-8", errors="replace")
|
||||
|
||||
|
||||
def _looks_like_shell_lnk(path: str) -> bool:
|
||||
"""Windows Shell Link 文件头为 4C 00 00 00(扩展名异常时仍可识别)。"""
|
||||
try:
|
||||
@@ -50,14 +73,11 @@ def _resolve_windows_lnk(lnk_path: str) -> str | None:
|
||||
ps,
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
env=env,
|
||||
timeout=15,
|
||||
creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0),
|
||||
)
|
||||
raw = (r.stdout or "").strip().lstrip("\ufeff")
|
||||
raw = _decode_subprocess_output(r.stdout or b"").strip().lstrip("\ufeff")
|
||||
if not raw:
|
||||
return None
|
||||
data = json.loads(raw)
|
||||
|
||||
Reference in new issue
Block a user