From 62f9e3f44a976ed5c45b5fd324bec230e397a74f Mon Sep 17 00:00:00 2001 From: Will Miao Date: Thu, 4 Jun 2026 07:17:26 +0800 Subject: [PATCH] fix(scripts): use platformdirs for cross-platform settings path resolution Both restore_suffixed_filenames.py and migrate_legacy_metadata.py hardcoded Path.home() / '.config' / APP_NAME for finding settings.json, which only works on Linux. On Windows this resolves to the wrong path (~/.config/ instead of %LOCALAPPDATA%). Replace the hand-rolled fallback with platformdirs.user_config_dir(), which correctly resolves to the OS-appropriate config directory on all platforms (Windows: %%LOCALAPPDATA%%, macOS: ~/Library/Application Support, Linux: ~/.config). The portable mode check (settings.json in repo root with use_portable_settings: true) is preserved unchanged. --- scripts/migrate_legacy_metadata.py | 7 +++---- scripts/restore_suffixed_filenames.py | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/scripts/migrate_legacy_metadata.py b/scripts/migrate_legacy_metadata.py index afa19920..0a312cf6 100644 --- a/scripts/migrate_legacy_metadata.py +++ b/scripts/migrate_legacy_metadata.py @@ -34,6 +34,8 @@ import sys from pathlib import Path from typing import Any +from platformdirs import user_config_dir + logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s", @@ -53,10 +55,7 @@ def resolve_settings_path() -> Path: if isinstance(payload, dict) and payload.get("use_portable_settings") is True: return portable - config_home = os.environ.get("XDG_CONFIG_HOME") - if config_home: - return Path(config_home).expanduser() / APP_NAME / "settings.json" - return Path.home() / ".config" / APP_NAME / "settings.json" + return Path(user_config_dir(APP_NAME, appauthor=False)) / "settings.json" def load_json(path: Path) -> dict[str, Any]: diff --git a/scripts/restore_suffixed_filenames.py b/scripts/restore_suffixed_filenames.py index 3d76f073..be3404b5 100644 --- a/scripts/restore_suffixed_filenames.py +++ b/scripts/restore_suffixed_filenames.py @@ -39,6 +39,8 @@ import sys from pathlib import Path from typing import Any +from platformdirs import user_config_dir + logging.basicConfig( level=logging.INFO, format="%(message)s", @@ -68,10 +70,7 @@ def resolve_settings_path() -> Path: if isinstance(payload, dict) and payload.get("use_portable_settings") is True: return portable - config_home = os.environ.get("XDG_CONFIG_HOME") - if config_home: - return Path(config_home).expanduser() / APP_NAME / "settings.json" - return Path.home() / ".config" / APP_NAME / "settings.json" + return Path(user_config_dir(APP_NAME, appauthor=False)) / "settings.json" def _load_json(path: Path) -> dict[str, Any]: