core: Replace legacy INI config system with Pydantic-based TOML configuration.

This is a huge configuration refactoring.

The configuration system now uses Pydantic settings, with better validation, better
support for environment variables and other goodies.

The main config files now use TOML instead of INI, this allow for better handling of
complex types, notably dict.

Plugins, components and frontends can declare their config settings in a class which will
be imported dynamically.
This commit is contained in:
Goffi 2025-11-30 12:27:35 +01:00
commit 6a7b7bc36a

View file

@ -19,9 +19,11 @@
import json
import os
from pathlib import Path
from typing import Self
from urllib.parse import quote
from autobahn.twisted.websocket import WebSocketClientFactory, WebSocketClientProtocol
from pydantic import Field
from shortuuid import uuid
import treq
from twisted.internet import defer, reactor
@ -36,6 +38,7 @@ from twisted.words.xish import domish
from wokkel import disco, iwokkel
from zope.interface import implementer
from libervia.backend import G
from libervia.backend.core import exceptions
from libervia.backend.core.constants import Const as C
from libervia.backend.core.core_types import SatXMPPEntity
@ -47,7 +50,7 @@ from libervia.backend.plugins.plugin_xep_0167 import XEP_0167, mapping, NS_AV_CO
from libervia.backend.plugins.plugin_xep_0176 import XEP_0176
from libervia.backend.plugins.plugin_xep_0298 import XEP_0298
from libervia.backend.tools.common import async_process, regex
from libervia.backend.tools.config import ComponentSettings, ExecutableFilePath
log = getLogger(__name__)
@ -70,7 +73,21 @@ PLUGIN_INFO = {
),
}
CONF_SECTION = f"component {IMPORT_NAME}"
class ConferenceSettings(ComponentSettings, plugin_info=PLUGIN_INFO):
galene_path: ExecutableFilePath | None = Field(
default_factory=lambda: ConferenceSettings._find_galene_exec()
)
http_port: int = Field(
default=9443, description="Port to use for Galène HTTP server."
)
@staticmethod
def _find_galene_exec() -> Path | None:
try:
return Path(which("galene")[0])
except IndexError:
return None
class Conference:
@ -386,21 +403,16 @@ class ConferencesComponent:
self.client.identities.append(
disco.DiscoIdentity("conference", "audio-video", NAME)
)
settings = G.config.get_settings(ConferenceSettings)
galene_path = settings.galene_path
try:
galene_path = Path(
self.host.memory.config_get(CONF_SECTION, "galene_path")
or which("galene")[0]
)
except IndexError:
if galene_path is None:
raise exceptions.NotFound(
'Can\'t find "galene" executable, "conferences" component can\'t be '
"started without it. Please install it in location accessible in PATH, "
'or use "galene_path" setting.'
f'Can\'t find "galene" executable, "PLUGIN_INFO[C.PI_NAME]" component'
f" can't be started without it. Please install it in location accessible"
f' in PATH, or use "galene_path" setting.'
)
self.galene_http_port = self.host.memory.config_get(
CONF_SECTION, "http_port", "9443"
)
self.galene_http_port = settings.http_port
galene_data_path = self.host.memory.get_cache_path(IMPORT_NAME, "galene")
galene_static_path = galene_path.parent / "static"
self.galene_group_path = galene_data_path / "groups"
@ -460,7 +472,7 @@ class ConferencesComponent:
}
with data_file.open("w") as f:
json.dump(group_data, f)
log.debug(f"Conference data for {group_name!r} created at " f"{data_file} .")
log.debug(f"Conference data for {group_name!r} created at {data_file} .")
url = f"http://localhost:{self.galene_http_port}/group/{quote(stem)}"
status_url = f"{url}/.status"