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:
parent
f3f40848b3
commit
6a7b7bc36a
1 changed files with 1392 additions and 1011 deletions
|
|
@ -29,9 +29,10 @@ from pathlib import Path
|
|||
import sys
|
||||
import termios
|
||||
import tty
|
||||
from typing import Optional, Set, Union
|
||||
from typing import Literal, Optional, Set, Union
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
from pydantic import Field
|
||||
from rich import console
|
||||
from twisted.internet.error import ReactorNotRunning
|
||||
|
||||
|
|
@ -54,11 +55,33 @@ from libervia.frontends.tools.jid import JID
|
|||
log.basicConfig(level=log.WARNING, format="[%(name)s] %(message)s")
|
||||
###
|
||||
|
||||
class CLISettings(config.FrontendSettings, key="cli"):
|
||||
background: Literal["auto", "dark", "light"] = Field(
|
||||
default="auto", description="Background handling strategy"
|
||||
)
|
||||
image_cmd: str|None = Field(
|
||||
default=None,
|
||||
description="Command to use to display images. Must be avaialble in path."
|
||||
)
|
||||
editor: str = Field(
|
||||
default = os.getenv("EDITOR", "vi")
|
||||
)
|
||||
editor_args_opt: str|None = Field(
|
||||
default=None
|
||||
)
|
||||
syntax_ext: dict = Field(default_factory=dict)
|
||||
blog_preview_open_cmd: str|None = Field(
|
||||
default=None
|
||||
)
|
||||
blog_preview_update_cmd: str|None = Field(
|
||||
default_factory=lambda data: data["blog_preview_open_cmd"]
|
||||
)
|
||||
|
||||
|
||||
## bridge handling
|
||||
# we get bridge name from conf and initialise the right class accordingly
|
||||
main_config = config.parse_main_conf()
|
||||
bridge_name = config.config_get(main_config, "", "bridge", "dbus")
|
||||
main_config = config.parse_main_conf(ignore_cache=True)
|
||||
bridge_name = next(iter(main_config.common.bridges)).type
|
||||
LiberviaCLILoop = get_libervia_cli_loop(bridge_name)
|
||||
|
||||
|
||||
|
|
@ -131,7 +154,9 @@ class LiberviaCli:
|
|||
by default display a message
|
||||
"""
|
||||
self.console = console.Console(theme=C.THEME_DEFAULT)
|
||||
self.sat_conf = main_config
|
||||
self.settings = main_config
|
||||
self.cli_settings = main_config.get_settings(CLISettings)
|
||||
self.media_dir = main_config.common.media_dir
|
||||
self.set_color_theme()
|
||||
bridge_module = dynamic_import.bridge(bridge_name, "libervia.frontends.bridge")
|
||||
if bridge_module is None:
|
||||
|
|
@ -141,10 +166,6 @@ class LiberviaCli:
|
|||
self.bridge = bridge_module.AIOBridge()
|
||||
self._on_quit_callbacks = []
|
||||
|
||||
def get_config(self, name, section=C.CONFIG_SECTION, default=None):
|
||||
"""Retrieve a setting value from sat.conf"""
|
||||
return config.config_get(self.sat_conf, section, name, default=default)
|
||||
|
||||
def guess_background(self):
|
||||
# cf. https://unix.stackexchange.com/a/245568 (thanks!)
|
||||
try:
|
||||
|
|
@ -210,16 +231,9 @@ class LiberviaCli:
|
|||
return "dark"
|
||||
|
||||
def set_color_theme(self):
|
||||
background = self.get_config("background", default="auto")
|
||||
background = self.cli_settings.background
|
||||
if background == "auto":
|
||||
background = self.guess_background()
|
||||
if background not in ("dark", "light"):
|
||||
raise exceptions.ConfigError(
|
||||
_(
|
||||
'Invalid value set for "background" ({background}), please check '
|
||||
"your settings in libervia.conf"
|
||||
).format(background=repr(background))
|
||||
)
|
||||
self.background = background
|
||||
if background == "light":
|
||||
C.A_HEADER = A.FG_MAGENTA
|
||||
|
|
@ -1272,8 +1286,8 @@ class CommandBase:
|
|||
self.add_parser_options()
|
||||
|
||||
@property
|
||||
def sat_conf(self):
|
||||
return self.host.sat_conf
|
||||
def cli_settings(self) -> CLISettings:
|
||||
return self.host.cli_settings
|
||||
|
||||
@property
|
||||
def args(self):
|
||||
|
|
|
|||
Loading…
Reference in a new issue