astrbot_plugin_imagestudio/core/config_service.py

92 行
3.1 KiB
Python

"""Configuration loading helpers."""
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from core.runtime_paths import resolve_config_path
from database.db_helper import load_config_payload
PLUGIN_VERSION = "1.0.65"
@dataclass(frozen=True)
class PluginConfig:
name: str = "astrbot_plugin_imagestudio"
display_name: str = "ImageStudio"
version: str = PLUGIN_VERSION
@dataclass(frozen=True)
class StorageConfig:
images_path: str = "data/images"
thumbnails_path: str = "data/thumbnails"
materials_path: str = "data/materials"
advanced_materials_path: str = "data/advanced_materials"
styles_path: str = "data/styles"
archives_path: str = "data/archives"
temp_path: str = "data/temp"
@dataclass(frozen=True)
class ImageStudioConfig:
plugin: PluginConfig = PluginConfig()
features: dict[str, bool] | None = None
storage: StorageConfig = StorageConfig()
def feature_flags(self) -> dict[str, bool]:
defaults = {
"quick_profile": True,
"workspace": True,
"material_library": True,
"wardrobe": True,
"api_discovery": True,
"private_companion_integration": False,
}
defaults.update(self.features or {})
return defaults
class ConfigService:
def __init__(self, config_path: str | Path = "config.yaml"):
self.config_path = _resolve_path(config_path)
def load(self) -> ImageStudioConfig:
payload = load_config_payload(self.config_path)
if not payload:
return ImageStudioConfig()
plugin = payload.get("plugin", {})
features = payload.get("features", {})
storage = payload.get("storage", {})
return ImageStudioConfig(
plugin=PluginConfig(
name=str(plugin.get("name", "astrbot_plugin_imagestudio")),
display_name=str(plugin.get("display_name", "ImageStudio")),
# The installed package version is immutable metadata. A
# writable AstrBot config may survive plugin upgrades, so it
# must not override the version shown by ui/bootstrap.
version=PLUGIN_VERSION,
),
features={key: bool(value) for key, value in features.items()},
storage=StorageConfig(
images_path=str(storage.get("images_path", "data/images")),
thumbnails_path=str(storage.get("thumbnails_path", "data/thumbnails")),
materials_path=str(storage.get("materials_path", "data/materials")),
advanced_materials_path=str(
storage.get("advanced_materials_path", "data/advanced_materials")
),
styles_path=str(storage.get("styles_path", "data/styles")),
archives_path=str(storage.get("archives_path", "data/archives")),
temp_path=str(storage.get("temp_path", "data/temp")),
),
)
def _resolve_path(path: str | Path) -> Path:
return resolve_config_path(path)