Skip to content

The only downloader you need. 下载器的集大成者。

refactor(paths): 统一命名与类型

XiaoYouChRa2fb418ca9cc
diff --git a/Ghost-Downloader-3.py b/Ghost-Downloader-3.py
index 16ace657..14d75d02 100644
--- a/Ghost-Downloader-3.py
+++ b/Ghost-Downloader-3.py
@@ -53,7 +53,7 @@ def startApp(application, isSilent=False):
import shutil
from PySide6.QtGui import QIcon
from app.config.cfg import cfg
- from app.config.paths import executableDir
+ from app.config.paths import EXECUTABLE_DIR
from app.view.shell.clipboard_listener import ClipboardListener
from app.signal_bus import signalBus
from app.startup import loadEngine, createServices, loadPacks, startEngine, bindNotifications, checkUpdateAtStartup, stopEngine
@@ -79,7 +79,7 @@ def startApp(application, isSilent=False):
coroutineRunner, categoryService, speedMeter = loadEngine(application)
- appDir = executableDir.parent.parent if sys.platform == "darwin" else executableDir
+ appDir = EXECUTABLE_DIR.parent.parent if sys.platform == "darwin" else EXECUTABLE_DIR
backupDir = appDir.parent / f"{appDir.name}_backup"
if backupDir.is_dir():
shutil.rmtree(backupDir, ignore_errors=True)
diff --git a/app/config/__init__.py b/app/config/__init__.py
index e803f8ed..7fc9d7f2 100644
--- a/app/config/__init__.py
+++ b/app/config/__init__.py
@@ -1,3 +1,3 @@
from app.config.cfg import cfg, proxy, BASE_HEADERS
-from app.config.paths import executableDir, APP_DATA_DIR
+from app.config.paths import EXECUTABLE_DIR, APP_DATA_DIR
from app.config.constants import VERSION, AUTHOR, DESKTOP_ID
diff --git a/app/config/cfg.py b/app/config/cfg.py
index 4bdc6ebe..15118e08 100644
--- a/app/config/cfg.py
+++ b/app/config/cfg.py
@@ -229,7 +229,7 @@ class Config(QConfig):
# 下载
downloadFolder = ConfigItem(
"GeneralDownload", "DownloadFolder",
- "/storage/emulated/0/Download" if IS_ANDROID else DOWNLOAD_DIR,
+ "/storage/emulated/0/Download" if IS_ANDROID else str(DOWNLOAD_DIR),
FolderValidator(),
)
memoryDownloadFolders = ConfigItem(
diff --git a/app/config/paths.py b/app/config/paths.py
index 2d8f235f..0aadb4a4 100644
--- a/app/config/paths.py
+++ b/app/config/paths.py
@@ -6,38 +6,39 @@ from pathlib import Path
from platformdirs import user_data_path, user_downloads_path
-executableDir = (
+EXECUTABLE_DIR = (
Path(sys.executable).resolve().parent
if "__compiled__" in globals()
else Path(".")
)
-APP_DATA_DIR: str = (
- str(executableDir / "GhostDownloader")
- if (executableDir / "GhostDownloader").is_dir()
- else str(user_data_path("GhostDownloader", appauthor=False))
+APP_DATA_DIR = (
+ EXECUTABLE_DIR / "GhostDownloader"
+ if (EXECUTABLE_DIR / "GhostDownloader").is_dir()
+ else user_data_path("GhostDownloader", appauthor=False)
)
-PORTABLE_PATH = executableDir / "GhostDownloader"
+PORTABLE_DIR = EXECUTABLE_DIR / "GhostDownloader"
-SEED_FEATURES_DIR = executableDir / "features"
+SEED_FEATURES_DIR = EXECUTABLE_DIR / "features"
FEATURES_DIR = (
SEED_FEATURES_DIR
if "__compiled__" not in globals()
- else Path(APP_DATA_DIR) / "features"
+ else APP_DATA_DIR / "features"
)
-USER_PATH = user_data_path("GhostDownloader", appauthor=False)
+USER_DATA_DIR = user_data_path("GhostDownloader", appauthor=False)
+
+DOWNLOAD_DIR = user_downloads_path()
-DOWNLOAD_DIR: str = str(user_downloads_path())
def isPortable() -> bool:
- return APP_DATA_DIR == str(PORTABLE_PATH)
+ return APP_DATA_DIR == PORTABLE_DIR
def migrate(target: Path) -> None:
from loguru import logger
logger.remove()
- source = Path(APP_DATA_DIR)
+ source = APP_DATA_DIR
target.mkdir(parents=True, exist_ok=True)
shutil.copytree(source, target, dirs_exist_ok=True)
if isPortable():
diff --git a/app/platform/file_association.py b/app/platform/file_association.py
index e3f069e2..5839ef6a 100644
--- a/app/platform/file_association.py
+++ b/app/platform/file_association.py
@@ -9,7 +9,7 @@ from loguru import logger
from PySide6.QtCore import QCoreApplication
from app.config.constants import DESKTOP_ID
-from app.config.paths import executableDir
+from app.config.paths import EXECUTABLE_DIR
if sys.platform == "win32":
import ctypes
@@ -32,7 +32,7 @@ def register(fileTypes: list[FileType]) -> None:
def _registerWindows(fileTypes: list[FileType]) -> None:
command = f'"{QCoreApplication.applicationFilePath().replace("/", chr(92))}" "%1"'
for fileType in fileTypes:
- iconPath = str(executableDir / "app" / "assets" / "file_icons" / f"{fileType.icon}.ico").replace("/", "\\")
+ iconPath = str(EXECUTABLE_DIR / "app" / "assets" / "file_icons" / f"{fileType.icon}.ico").replace("/", "\\")
for ext in fileType.extensions:
progId = f"GhostDownloader{ext}"
for regPath, regValue in (
diff --git a/app/services/browser_service.py b/app/services/browser_service.py
index fcb1a498..2eeb10ec 100644
--- a/app/services/browser_service.py
+++ b/app/services/browser_service.py
@@ -26,7 +26,7 @@ from app.models.task import MergeTaskOptions, PageTaskOptions
if TYPE_CHECKING:
from app.models.task import Task, TaskOptions, ResourceTaskOptions
-EXTENSION_UNPACK_DIR = Path(APP_DATA_DIR) / "browser_extension"
+EXTENSION_UNPACK_DIR = APP_DATA_DIR / "browser_extension"
async def extractBrowserExtension(loadCrx) -> Path:
diff --git a/app/services/task_service.py b/app/services/task_service.py
index 2444b64e..51879ac5 100644
--- a/app/services/task_service.py
+++ b/app/services/task_service.py
@@ -23,7 +23,7 @@ class TaskStore:
def __init__(self):
self._tasks: dict[str, Task] = {}
self._loaded = False
- self._path = Path(APP_DATA_DIR) / "tasks.jsonl"
+ self._path = APP_DATA_DIR / "tasks.jsonl"
def add(self, task: Task) -> None:
self._tasks[task.taskId] = task
diff --git a/app/services/update_service.py b/app/services/update_service.py
index dd8deba5..50883fb1 100644
--- a/app/services/update_service.py
+++ b/app/services/update_service.py
@@ -17,7 +17,7 @@ from app.signal import Signal
from loguru import logger
from app.config.constants import VERSION
-from app.config.paths import APP_DATA_DIR, FEATURES_DIR, executableDir
+from app.config.paths import APP_DATA_DIR, FEATURES_DIR, EXECUTABLE_DIR
from app.platform.android import IS_ANDROID
from app.platform.filesystem import matchChecksum
from app.models.pack import PackManifest
@@ -28,7 +28,7 @@ from app.update import APP_REPO, isNewer
if TYPE_CHECKING:
from app.services.coroutine_runner import CoroutineRunner
-STAGING_DIR = Path(APP_DATA_DIR) / "update_staging"
+STAGING_DIR = APP_DATA_DIR / "update_staging"
OS_MAP = {"win32": "Windows", "darwin": "macOS", "linux": "Linux"}
MACHINE_MAP = {"AMD64": "x86_64", "x86_64": "x86_64", "aarch64": "arm64", "arm64": "arm64"}
@@ -286,12 +286,12 @@ class UpdateService:
def _startUpdater(self) -> None:
updaterName = "updater.exe" if sys.platform == "win32" else "updater"
- updaterPath = executableDir / updaterName
+ updaterPath = EXECUTABLE_DIR / updaterName
if not updaterPath.is_file():
logger.error("updater not found: {}", updaterPath)
return
- appDir = executableDir.parent.parent if sys.platform == "darwin" else executableDir
+ appDir = EXECUTABLE_DIR.parent.parent if sys.platform == "darwin" else EXECUTABLE_DIR
patchPath = STAGING_DIR / "patch.hdiff"
stagingNewDir = STAGING_DIR / "app_new"
diff --git a/app/startup.py b/app/startup.py
index 124c2b72..7d161218 100644
--- a/app/startup.py
+++ b/app/startup.py
@@ -28,9 +28,9 @@ def loadEngine(application):
from PySide6.QtCore import QResource, QTimer
from shiboken6 import isValid
- from app.config.paths import executableDir
+ from app.config.paths import EXECUTABLE_DIR
- QResource.registerResource(str(executableDir / "app" / "assets" / "resources.rcc"))
+ QResource.registerResource(str(EXECUTABLE_DIR / "app" / "assets" / "resources.rcc"))
loadTranslators(application)
diff --git a/app/view/pages/setting_page.py b/app/view/pages/setting_page.py
index e873e8cb..73291846 100644
--- a/app/view/pages/setting_page.py
+++ b/app/view/pages/setting_page.py
@@ -493,9 +493,9 @@ class SettingPage(ScrollArea):
setRunAtLogin(enabled)
def _onMigrateClicked(self) -> None:
- from app.config.paths import isPortable, migrate, PORTABLE_PATH, USER_PATH
+ from app.config.paths import isPortable, migrate, PORTABLE_DIR, USER_DATA_DIR
- target = USER_PATH if isPortable() else PORTABLE_PATH
+ target = USER_DATA_DIR if isPortable() else PORTABLE_DIR
mode = self.tr("用户模式") if isPortable() else self.tr("Portable 模式")
dialog = MessageBox(
self.tr("切换数据存储模式"),
diff --git a/features/bittorrent_pack/session.py b/features/bittorrent_pack/session.py
index 4863ab5b..2f75713c 100644
--- a/features/bittorrent_pack/session.py
+++ b/features/bittorrent_pack/session.py
@@ -300,7 +300,7 @@ class BTSession(QObject):
def _loadDhtState(self) -> lt.session_params:
from app.config.paths import APP_DATA_DIR
- path = Path(APP_DATA_DIR) / DHT_STATE_FILE
+ path = APP_DATA_DIR / DHT_STATE_FILE
if path.exists():
try:
return lt.read_session_params(
@@ -318,7 +318,7 @@ class BTSession(QObject):
state = self._session.save_state(
lt.save_state_flags_t.save_dht_state
)
- (Path(APP_DATA_DIR) / DHT_STATE_FILE).write_bytes(
+ (APP_DATA_DIR / DHT_STATE_FILE).write_bytes(
lt.bencode(state)
)
except Exception as e:
diff --git a/features/ed2k_pack/session.py b/features/ed2k_pack/session.py
index 60441e9d..4c46f1c4 100644
--- a/features/ed2k_pack/session.py
+++ b/features/ed2k_pack/session.py
@@ -136,7 +136,7 @@ class ED2kSession:
raise TaskError(
"{name} 未安装,请在设置中安装", name=ed2kRuntime.name
)
- client = Client(Path(path), Path(APP_DATA_DIR) / "ed2k_data")
+ client = Client(Path(path), APP_DATA_DIR / "ed2k_data")
await client.start(Settings(
enableDht=ed2kConfig.enableDht.value,
enableUpnp=ed2kConfig.enableUpnp.value,
diff --git a/features/yt_dlp_pack/config.py b/features/yt_dlp_pack/config.py
index 3e6ee5d9..a2961b1b 100644
--- a/features/yt_dlp_pack/config.py
+++ b/features/yt_dlp_pack/config.py
@@ -26,7 +26,7 @@ AUTH_COOKIE_NAMES = ("LOGIN_INFO", "SAPISID", "__Secure-1PAPISID", "__Secure-3PA
def cookieFile() -> Path:
- return Path(APP_DATA_DIR) / "YtDlp" / "cookies.txt"
+ return APP_DATA_DIR / "YtDlp" / "cookies.txt"
def hasCookieFile() -> bool: