This commit is contained in:
2026-01-22 11:05:40 -08:00
parent 874939a65b
commit 2ae651c225
7 changed files with 106 additions and 108 deletions

View File

@@ -4,6 +4,7 @@ from __future__ import annotations
import re
import tempfile
import json
from pathlib import Path
from typing import Any, Dict, Optional, List
from SYS.logger import log
@@ -39,80 +40,6 @@ def clear_config_cache() -> None:
_CONFIG_CACHE.clear()
def reload_config(
config_dir: Optional[Path] = None, filename: str = "medios.db"
) -> Dict[str, Any]:
_CONFIG_CACHE.pop("db_config", None)
return load_config(config_dir=config_dir, filename=filename)
def load_config(
config_dir: Optional[Path] = None, filename: str = "medios.db"
) -> Dict[str, Any]:
# We no longer use config_dir or filename for the config file itself,
# but we keep them in the signature for backward compatibility.
cache_key = "db_config"
if cache_key in _CONFIG_CACHE:
return _CONFIG_CACHE[cache_key]
# Load from database
try:
from SYS.database import get_config_all
db_config = get_config_all()
if db_config:
_CONFIG_CACHE[cache_key] = db_config
return db_config
except Exception:
pass
return {}
def save_config(
config: Dict[str, Any],
config_dir: Optional[Path] = None,
filename: str = "medios.db",
) -> None:
"""Persist configuration to the database."""
try:
from SYS.database import save_config_value
for key, value in config.items():
if key in ('store', 'provider', 'tool'):
if isinstance(value, dict):
for subtype, instances in value.items():
if isinstance(instances, dict):
# provider/tool are usually config[cat][subtype][key]
# but store is config['store'][subtype][name][key]
if key == 'store':
for name, settings in instances.items():
if isinstance(settings, dict):
for k, v in settings.items():
save_config_value(key, subtype, name, k, v)
else:
for k, v in instances.items():
save_config_value(key, subtype, "default", k, v)
else:
# global settings
if not key.startswith("_"):
save_config_value("global", "none", "none", key, value)
except Exception as e:
log(f"Failed to save config to database: {e}")
_CONFIG_CACHE["db_config"] = config
def load() -> Dict[str, Any]:
"""Return the parsed configuration from database."""
return load_config()
def save(config: Dict[str, Any]) -> None:
"""Persist *config* back to database."""
save_config(config)
def _make_cache_key(config_dir: Optional[Path], filename: str, actual_path: Optional[Path]) -> str:
if actual_path:
return str(actual_path.resolve())
@@ -436,6 +363,11 @@ def _validate_config_safety(config: Dict[str, Any]) -> None:
return
def _serialize_conf(config: Dict[str, Any]) -> str:
"""Serialize configuration to a string for legacy .conf files."""
return json.dumps(config, indent=4)
def save_config(
config: Dict[str, Any],
config_dir: Optional[Path] = None,
@@ -448,27 +380,28 @@ def save_config(
try:
from SYS.database import db, save_config_value
# We want to clear and re-save or just update?
# For simplicity, we'll iterate and update.
for key, value in config.items():
if key in ('store', 'provider', 'tool'):
if isinstance(value, dict):
for subtype, instances in value.items():
if isinstance(instances, dict):
# provider/tool are usually config[cat][subtype][key]
# but store is config['store'][subtype][name][key]
if key == 'store':
for name, settings in instances.items():
if isinstance(settings, dict):
for k, v in settings.items():
save_config_value(key, subtype, name, k, v)
else:
for k, v in instances.items():
save_config_value(key, subtype, "default", k, v)
else:
# global settings
if not key.startswith("_"):
save_config_value("global", "none", "none", key, value)
with db.transaction():
# We want to clear and re-save or just update?
# For simplicity, we'll iterate and update.
for key, value in config.items():
if key in ('store', 'provider', 'tool'):
if isinstance(value, dict):
for subtype, instances in value.items():
if isinstance(instances, dict):
# provider/tool are usually config[cat][subtype][key]
# but store is config['store'][subtype][name][key]
if key == 'store':
for name, settings in instances.items():
if isinstance(settings, dict):
for k, v in settings.items():
save_config_value(key, subtype, name, k, v)
else:
for k, v in instances.items():
save_config_value(key, subtype, "default", k, v)
else:
# global settings
if not key.startswith("_"):
save_config_value("global", "none", "none", key, value)
except Exception as e:
log(f"Failed to save config to database: {e}")