Improve settings reading reliablity

This commit is contained in:
Lucas Sköld
2025-06-28 16:03:18 +02:00
parent cefaf2bd6a
commit 8e9324de22
+10 -55
View File
@@ -31,81 +31,37 @@ class ConfigReader:
Check if comfy is running in portable mode.
"""
DEFAULT_PATH = os.path.abspath(os.path.join(os.getcwd(), "user/default/comfy.settings.json"))
PORTABLE_PATH = os.path.abspath(os.path.join(os.getcwd(), "ComfyUI/user/default/comfy.settings.json"))
portable = None
user_directory = folder_paths.get_user_directory()
SETTINGS_PATH = os.path.join(user_directory, "default/comfy.settings.json")
@classmethod
def print_sn0w(cls, message, color="\033[0;35m"):
"""Print a message with a specific color prefix."""
print(f"{color}[sn0w] \033[0m{message}")
@classmethod
def _get_path(cls):
return ConfigReader.PORTABLE_PATH if ConfigReader.portable else ConfigReader.DEFAULT_PATH
@classmethod
def is_comfy_portable(cls):
"""Check if the application is running in portable mode."""
if cls.portable is not None:
return cls.portable
# Check if default exists
if os.path.isfile(cls.DEFAULT_PATH):
cls.portable = False
ConfigReader.print_sn0w("Running standalone comfy.")
return False
# Check if portable exists
if os.path.isfile(cls.PORTABLE_PATH):
cls.portable = True
ConfigReader.print_sn0w("Running portable comfy.")
return True
# If neither exist
ConfigReader.print_sn0w("Could not find comfy settings.")
return None
@staticmethod
def get_setting(setting_id, default=None):
"""Retrieve a setting value from the configuration file."""
# Determine the correct path based on the portable attribute
if ConfigReader.portable is None:
ConfigReader.print_sn0w(
f"Local configuration file not found at either {ConfigReader.PORTABLE_PATH} or {ConfigReader.DEFAULT_PATH}.",
"\033[0;33m",
)
return default
path = ConfigReader._get_path()
# Try to read the settings from the determined path
try:
with open(path, "r", encoding="utf-8") as file:
with open(ConfigReader.SETTINGS_PATH, "r", encoding="utf-8") as file:
settings = json.load(file)
return settings.get(setting_id, default)
except FileNotFoundError:
ConfigReader.print_sn0w(f"Local configuration file not found at {path}.", "\033[0;33m")
ConfigReader.print_sn0w(
f"Local configuration file not found at {ConfigReader.SETTINGS_PATH}.", "\033[0;33m"
)
except json.JSONDecodeError:
ConfigReader.print_sn0w(f"Error decoding JSON from {path}.", "\033[0;31m")
ConfigReader.print_sn0w(f"Error decoding JSON from {ConfigReader.SETTINGS_PATH}.", "\033[0;31m")
return default
@staticmethod
def set_setting(setting_id: str, value):
"""Set a setting value in the configuration file."""
# Determine the correct path based on the portable attribute
if ConfigReader.portable is None:
ConfigReader.print_sn0w(
f"Local configuration file not found at either {ConfigReader.PORTABLE_PATH} or {ConfigReader.DEFAULT_PATH}.",
"\033[0;33m",
)
return False
path = ConfigReader._get_path()
# Try to read the existing settings from the determined path
path = ConfigReader.SETTINGS_PATH
try:
if os.path.exists(path):
with open(path, "r", encoding="utf-8") as file:
@@ -154,7 +110,7 @@ class ConfigReader:
"sn0w.TextboxSettings": "SyntaxHighlighting.textbox-colors",
}
path = ConfigReader._get_path()
path = ConfigReader.SETTINGS_PATH
with open(path, "r", encoding="utf-8") as file:
settings = json.load(file)
@@ -178,7 +134,6 @@ class ConfigReader:
# Initialize portable check when the class is defined
ConfigReader.is_comfy_portable()
ConfigReader.validate_settings()