mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-03-24 06:32:12 -03:00
feat(settings): migrate settings to user config dir
This commit is contained in:
@@ -6,6 +6,8 @@ import logging
|
||||
import json
|
||||
import urllib.parse
|
||||
|
||||
from py.utils.settings_paths import ensure_settings_file
|
||||
|
||||
# Use an environment variable to control standalone mode
|
||||
standalone_mode = os.environ.get("HF_HUB_DISABLE_TELEMETRY", "0") == "0"
|
||||
|
||||
@@ -40,12 +42,12 @@ class Config:
|
||||
try:
|
||||
# Check if we're running in ComfyUI mode (not standalone)
|
||||
# Load existing settings
|
||||
settings_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'settings.json')
|
||||
settings_path = ensure_settings_file(logger)
|
||||
settings = {}
|
||||
if os.path.exists(settings_path):
|
||||
with open(settings_path, 'r', encoding='utf-8') as f:
|
||||
settings = json.load(f)
|
||||
|
||||
|
||||
# Update settings with paths
|
||||
settings['folder_paths'] = {
|
||||
'loras': self.loras_roots,
|
||||
|
||||
@@ -8,6 +8,8 @@ import tempfile
|
||||
import asyncio
|
||||
from aiohttp import web, ClientError
|
||||
from typing import Dict, List
|
||||
|
||||
from py.utils.settings_paths import ensure_settings_file
|
||||
from ..services.downloader import get_downloader
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -121,7 +123,7 @@ class UpdateRoutes:
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
plugin_root = os.path.dirname(os.path.dirname(current_dir))
|
||||
|
||||
settings_path = os.path.join(plugin_root, 'settings.json')
|
||||
settings_path = ensure_settings_file(logger)
|
||||
settings_backup = None
|
||||
if os.path.exists(settings_path):
|
||||
with open(settings_path, 'r', encoding='utf-8') as f:
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import os
|
||||
import json
|
||||
import os
|
||||
import logging
|
||||
from typing import Any, Dict
|
||||
|
||||
from py.utils.settings_paths import ensure_settings_file
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -36,7 +38,7 @@ DEFAULT_SETTINGS: Dict[str, Any] = {
|
||||
|
||||
class SettingsManager:
|
||||
def __init__(self):
|
||||
self.settings_file = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'settings.json')
|
||||
self.settings_file = ensure_settings_file(logger)
|
||||
self.settings = self._load_settings()
|
||||
self._migrate_setting_keys()
|
||||
self._ensure_default_settings()
|
||||
|
||||
84
py/utils/settings_paths.py
Normal file
84
py/utils/settings_paths.py
Normal file
@@ -0,0 +1,84 @@
|
||||
"""Utilities for locating and migrating the LoRA Manager settings file."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
from typing import Optional
|
||||
|
||||
from platformdirs import user_config_dir
|
||||
|
||||
|
||||
APP_NAME = "ComfyUI-LoRA-Manager"
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_project_root() -> str:
|
||||
"""Return the root directory of the project repository."""
|
||||
|
||||
return os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
|
||||
|
||||
|
||||
def get_legacy_settings_path() -> str:
|
||||
"""Return the legacy location of ``settings.json`` within the project tree."""
|
||||
|
||||
return os.path.join(get_project_root(), "settings.json")
|
||||
|
||||
|
||||
def get_settings_dir(create: bool = True) -> str:
|
||||
"""Return the user configuration directory for the application.
|
||||
|
||||
Args:
|
||||
create: Whether to create the directory if it does not already exist.
|
||||
|
||||
Returns:
|
||||
The absolute path to the user configuration directory.
|
||||
"""
|
||||
|
||||
config_dir = user_config_dir(APP_NAME, appauthor=False)
|
||||
if create:
|
||||
os.makedirs(config_dir, exist_ok=True)
|
||||
return config_dir
|
||||
|
||||
|
||||
def get_settings_file_path(create_dir: bool = True) -> str:
|
||||
"""Return the path to ``settings.json`` in the user configuration directory."""
|
||||
|
||||
return os.path.join(get_settings_dir(create=create_dir), "settings.json")
|
||||
|
||||
|
||||
def ensure_settings_file(logger: Optional[logging.Logger] = None) -> str:
|
||||
"""Ensure the settings file resides in the user configuration directory.
|
||||
|
||||
If a legacy ``settings.json`` is detected in the project root it is migrated to
|
||||
the platform-specific user configuration folder. The caller receives the path
|
||||
to the settings file irrespective of whether a migration was needed.
|
||||
|
||||
Args:
|
||||
logger: Optional logger used for migration messages. Falls back to a
|
||||
module level logger when omitted.
|
||||
|
||||
Returns:
|
||||
The absolute path to ``settings.json`` in the user configuration folder.
|
||||
"""
|
||||
|
||||
logger = logger or _LOGGER
|
||||
target_path = get_settings_file_path(create_dir=True)
|
||||
legacy_path = get_legacy_settings_path()
|
||||
|
||||
if os.path.exists(legacy_path) and not os.path.exists(target_path):
|
||||
try:
|
||||
os.makedirs(os.path.dirname(target_path), exist_ok=True)
|
||||
shutil.move(legacy_path, target_path)
|
||||
logger.info("Migrated settings.json to %s", target_path)
|
||||
except Exception as exc: # pragma: no cover - defensive fallback path
|
||||
logger.warning("Failed to move legacy settings.json: %s", exc)
|
||||
try:
|
||||
shutil.copy2(legacy_path, target_path)
|
||||
logger.info("Copied legacy settings.json to %s", target_path)
|
||||
except Exception as copy_exc: # pragma: no cover - defensive fallback path
|
||||
logger.error("Could not migrate settings.json: %s", copy_exc)
|
||||
|
||||
return target_path
|
||||
|
||||
Reference in New Issue
Block a user