mirror of
https://github.com/zvx-echo6/central.git
synced 2026-05-21 18:14:44 +02:00
refactor: remove TomlConfigSource, keep only DbConfigSource
TOML config is now retired. Database is the sole configuration source. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
98f3e578a4
commit
4376588baf
1 changed files with 80 additions and 187 deletions
|
|
@ -1,17 +1,13 @@
|
||||||
"""Configuration source abstraction.
|
"""Configuration source abstraction.
|
||||||
|
|
||||||
Provides a unified interface for loading adapter configuration from
|
Provides a unified interface for loading adapter configuration from
|
||||||
either TOML files or the database-backed config store.
|
the database-backed config store.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from collections.abc import Awaitable, Callable
|
from collections.abc import Awaitable, Callable
|
||||||
from pathlib import Path
|
from typing import Protocol, runtime_checkable
|
||||||
from typing import Any, Protocol, runtime_checkable
|
|
||||||
|
|
||||||
import tomllib
|
|
||||||
|
|
||||||
from central.config import NWSAdapterConfig
|
|
||||||
from central.config_models import AdapterConfig
|
from central.config_models import AdapterConfig
|
||||||
from central.config_store import ConfigStore
|
from central.config_store import ConfigStore
|
||||||
|
|
||||||
|
|
@ -36,8 +32,7 @@ class ConfigSource(Protocol):
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Watch for configuration changes.
|
"""Watch for configuration changes.
|
||||||
|
|
||||||
For TOML source, this is a no-op (returns immediately).
|
Runs forever, calling callback(table, key) on changes.
|
||||||
For DB source, this runs forever, calling callback(table, key) on changes.
|
|
||||||
"""
|
"""
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
@ -46,81 +41,6 @@ class ConfigSource(Protocol):
|
||||||
...
|
...
|
||||||
|
|
||||||
|
|
||||||
class TomlConfigSource:
|
|
||||||
"""Configuration source backed by a TOML file.
|
|
||||||
|
|
||||||
This is the legacy configuration path. Does not support hot-reload.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, toml_path: Path) -> None:
|
|
||||||
self._toml_path = toml_path
|
|
||||||
self._adapters: dict[str, AdapterConfig] = {}
|
|
||||||
self._loaded = False
|
|
||||||
|
|
||||||
def _load(self) -> None:
|
|
||||||
"""Load configuration from TOML file."""
|
|
||||||
if self._loaded:
|
|
||||||
return
|
|
||||||
|
|
||||||
with self._toml_path.open("rb") as f:
|
|
||||||
data = tomllib.load(f)
|
|
||||||
|
|
||||||
adapters_raw = data.get("adapters", {})
|
|
||||||
from datetime import datetime, timezone
|
|
||||||
|
|
||||||
now = datetime.now(timezone.utc)
|
|
||||||
|
|
||||||
for name, adapter_data in adapters_raw.items():
|
|
||||||
# Convert TOML adapter config to unified AdapterConfig
|
|
||||||
# TOML uses NWSAdapterConfig shape, we need to convert to AdapterConfig
|
|
||||||
enabled = adapter_data.get("enabled", True)
|
|
||||||
cadence_s = adapter_data.get("cadence_s", 60)
|
|
||||||
|
|
||||||
# Extract settings (everything except enabled/cadence_s)
|
|
||||||
settings = {
|
|
||||||
k: v
|
|
||||||
for k, v in adapter_data.items()
|
|
||||||
if k not in ("enabled", "cadence_s")
|
|
||||||
}
|
|
||||||
|
|
||||||
self._adapters[name] = AdapterConfig(
|
|
||||||
name=name,
|
|
||||||
enabled=enabled,
|
|
||||||
cadence_s=cadence_s,
|
|
||||||
settings=settings,
|
|
||||||
paused_at=None,
|
|
||||||
updated_at=now,
|
|
||||||
)
|
|
||||||
|
|
||||||
self._loaded = True
|
|
||||||
logger.info(
|
|
||||||
"Loaded TOML config",
|
|
||||||
extra={"path": str(self._toml_path), "adapters": list(self._adapters.keys())},
|
|
||||||
)
|
|
||||||
|
|
||||||
async def list_enabled_adapters(self) -> list[AdapterConfig]:
|
|
||||||
"""List all enabled adapters from TOML."""
|
|
||||||
self._load()
|
|
||||||
return [a for a in self._adapters.values() if a.enabled and not a.is_paused]
|
|
||||||
|
|
||||||
async def get_adapter(self, name: str) -> AdapterConfig | None:
|
|
||||||
"""Get a specific adapter from TOML."""
|
|
||||||
self._load()
|
|
||||||
return self._adapters.get(name)
|
|
||||||
|
|
||||||
async def watch_for_changes(
|
|
||||||
self,
|
|
||||||
callback: Callable[[str, str], Awaitable[None] | None],
|
|
||||||
) -> None:
|
|
||||||
"""TOML does not support hot-reload. Returns immediately."""
|
|
||||||
logger.debug("TOML config source does not support hot-reload")
|
|
||||||
return
|
|
||||||
|
|
||||||
async def close(self) -> None:
|
|
||||||
"""No resources to clean up for TOML source."""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class DbConfigSource:
|
class DbConfigSource:
|
||||||
"""Configuration source backed by the Postgres config store.
|
"""Configuration source backed by the Postgres config store.
|
||||||
|
|
||||||
|
|
@ -158,30 +78,3 @@ class DbConfigSource:
|
||||||
async def close(self) -> None:
|
async def close(self) -> None:
|
||||||
"""Close the underlying config store."""
|
"""Close the underlying config store."""
|
||||||
await self._store.close()
|
await self._store.close()
|
||||||
|
|
||||||
|
|
||||||
async def create_config_source(
|
|
||||||
source_type: str,
|
|
||||||
dsn: str | None = None,
|
|
||||||
toml_path: Path | None = None,
|
|
||||||
) -> ConfigSource:
|
|
||||||
"""Factory function to create the appropriate config source.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
source_type: "toml" or "db"
|
|
||||||
dsn: PostgreSQL DSN (required for "db")
|
|
||||||
toml_path: Path to TOML file (required for "toml")
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
ConfigSource implementation
|
|
||||||
"""
|
|
||||||
if source_type == "toml":
|
|
||||||
if toml_path is None:
|
|
||||||
raise ValueError("toml_path required for toml config source")
|
|
||||||
return TomlConfigSource(toml_path)
|
|
||||||
elif source_type == "db":
|
|
||||||
if dsn is None:
|
|
||||||
raise ValueError("dsn required for db config source")
|
|
||||||
return await DbConfigSource.create(dsn)
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Unknown config source type: {source_type}")
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue