feat: add shared utility modules

This commit is contained in:
lgldlk
2026-03-08 01:31:26 +08:00
parent bb7deb8e08
commit 9dfd8b6646
4 changed files with 45 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
"""Shared helpers for CLI scripts."""
+15
View File
@@ -0,0 +1,15 @@
from __future__ import annotations
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent.parent
CONFIG_PATH = BASE_DIR / "config.json"
OUTPUTS_DIR = BASE_DIR / "outputs"
DEFAULT_COMFYUI_SERVER_URL = "http://127.0.0.1:8188"
def default_config() -> dict[str, str]:
return {
"comfyui_server_url": DEFAULT_COMFYUI_SERVER_URL,
"output_dir": str(OUTPUTS_DIR),
}
+10
View File
@@ -0,0 +1,10 @@
from __future__ import annotations
import json
from pathlib import Path
from typing import Any
def load_json(path: str | Path) -> Any:
with Path(path).open("r", encoding="utf-8") as file:
return json.load(file)
+19
View File
@@ -0,0 +1,19 @@
from __future__ import annotations
from shared.config import CONFIG_PATH, default_config
from shared.json_utils import load_json
def get_runtime_config() -> dict[str, str]:
if not CONFIG_PATH.exists():
return default_config()
loaded = load_json(CONFIG_PATH)
if not isinstance(loaded, dict):
return default_config()
defaults = default_config()
return {
"comfyui_server_url": str(loaded.get("comfyui_server_url") or defaults["comfyui_server_url"]),
"output_dir": str(loaded.get("output_dir") or defaults["output_dir"]),
}