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
7 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):
|
||||
|
|
|
|||
|
|
@ -204,7 +204,7 @@ class AVCallUI(QMainWindow):
|
|||
@classmethod
|
||||
async def run(cls, parent, call_data):
|
||||
"""Run PyQt loop and show the app"""
|
||||
media_dir = Path(await parent.host.bridge.config_get("", "media_dir"))
|
||||
media_dir = parent.host.settings.common.media_dir
|
||||
icons_path = media_dir / "fonts/fontawesome/svgs/solid"
|
||||
app = QApplication([])
|
||||
av_call_gui = cls(parent.host, icons_path)
|
||||
|
|
|
|||
|
|
@ -48,8 +48,7 @@ class Get(base.CommandBase):
|
|||
self.parser.add_argument("jid", nargs="?", default="", help=_("entity"))
|
||||
|
||||
async def show_image(self, path):
|
||||
sat_conf = config.parse_main_conf()
|
||||
cmd = config.config_get(sat_conf, C.CONFIG_SECTION, "image_cmd")
|
||||
cmd = self.host.cli_settings.image_cmd
|
||||
cmds = [cmd] + DISPLAY_CMD if cmd else DISPLAY_CMD
|
||||
for cmd in cmds:
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -770,7 +770,7 @@ class Edit(base.CommandBase, BlogPublishCommon, common.BaseEdit):
|
|||
async def start(self):
|
||||
# if there are user defined extension, we use them
|
||||
SYNTAX_EXT.update(
|
||||
config.config_get(self.sat_conf, C.CONFIG_SECTION, CONF_SYNTAX_EXT, {})
|
||||
self.host.cli_settings.syntax_ext
|
||||
)
|
||||
self.current_syntax = await self.get_current_syntax()
|
||||
|
||||
|
|
@ -948,24 +948,17 @@ class Preview(base.CommandBase, common.BaseEdit):
|
|||
else:
|
||||
aionotify = None
|
||||
|
||||
sat_conf = self.sat_conf
|
||||
SYNTAX_EXT.update(
|
||||
config.config_get(sat_conf, C.CONFIG_SECTION, CONF_SYNTAX_EXT, {})
|
||||
self.host.cli_settings.syntax_ext
|
||||
)
|
||||
|
||||
try:
|
||||
self.open_cb_cmd = config.config_get(
|
||||
sat_conf, C.CONFIG_SECTION, "blog_preview_open_cmd", Exception
|
||||
)
|
||||
except (NoOptionError, NoSectionError):
|
||||
self.open_cb_cmd = None
|
||||
self.open_cb_cmd = self.cli_settings.blog_preview_open_cmd
|
||||
if self.open_cb_cmd is None:
|
||||
open_cb = self.show_preview
|
||||
else:
|
||||
open_cb = self.open_preview_ext
|
||||
open_cp = self.open_preview_ext
|
||||
|
||||
self.update_cb_cmd = config.config_get(
|
||||
sat_conf, C.CONFIG_SECTION, "blog_preview_update_cmd", self.open_cb_cmd
|
||||
)
|
||||
self.update_cb_cmd = self.cli_settings.blog_preview_update_cmd
|
||||
if self.update_cb_cmd is None:
|
||||
update_cb = self.show_preview
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ import asyncio
|
|||
import shlex
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
from libervia.cli.constants import Const as C
|
||||
from libervia.backend.core.i18n import _
|
||||
from libervia.backend.core import exceptions
|
||||
|
|
@ -36,6 +37,9 @@ from libervia.backend.tools import config
|
|||
from configparser import NoSectionError, NoOptionError
|
||||
from collections import namedtuple
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .base import LiberviaCli
|
||||
|
||||
# default arguments used for some known editors (editing with metadata)
|
||||
VIM_SPLIT_ARGS = "-c 'set nospr|vsplit|wincmd w|next|wincmd w'"
|
||||
EMACS_SPLIT_ARGS = '--eval "(split-window-horizontally)"'
|
||||
|
|
@ -83,7 +87,7 @@ def ansi_rjust(s, width):
|
|||
return " " * (width - len(cleaned)) + s
|
||||
|
||||
|
||||
def get_tmp_dir(sat_conf, cat_dir, sub_dir=None):
|
||||
def get_tmp_dir(config: config.Settings, cat_dir, sub_dir=None):
|
||||
"""Return directory used to store temporary files
|
||||
|
||||
@param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration
|
||||
|
|
@ -94,8 +98,7 @@ def get_tmp_dir(sat_conf, cat_dir, sub_dir=None):
|
|||
initial str)
|
||||
@return (Path): path to the dir
|
||||
"""
|
||||
local_dir = config.config_get(sat_conf, "", "local_dir", Exception)
|
||||
path_elts = [local_dir, cat_dir]
|
||||
path_elts = [config.common.local_dir, cat_dir]
|
||||
if sub_dir is not None:
|
||||
path_elts.append(regex.path_escape(sub_dir))
|
||||
return Path(*path_elts)
|
||||
|
|
@ -126,7 +129,7 @@ class BaseEdit(object):
|
|||
It works with temporary files in SàT local_dir, in a "cat_dir" subdir
|
||||
"""
|
||||
|
||||
def __init__(self, host, cat_dir, use_metadata=False):
|
||||
def __init__(self, host: "LiberviaCli", cat_dir, use_metadata=False):
|
||||
"""
|
||||
@param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration
|
||||
@param cat_dir(unicode): directory to use for drafts
|
||||
|
|
@ -152,7 +155,7 @@ class BaseEdit(object):
|
|||
path = Path(path).resolve()
|
||||
if not path.is_file:
|
||||
raise OSError("path must link to a regular file")
|
||||
if path.parent != get_tmp_dir(self.sat_conf, self.cat_dir):
|
||||
if path.parent != get_tmp_dir(self.host.settings, self.cat_dir):
|
||||
self.disp(
|
||||
f"File {path} is not in SàT temporary hierarchy, we do not remove " f"it",
|
||||
2,
|
||||
|
|
@ -160,7 +163,7 @@ class BaseEdit(object):
|
|||
return
|
||||
# we have 2 files per draft with use_metadata, so we double max
|
||||
unlink_max = SECURE_UNLINK_MAX * 2 if self.use_metadata else SECURE_UNLINK_MAX
|
||||
backup_dir = get_tmp_dir(self.sat_conf, self.cat_dir, SECURE_UNLINK_DIR)
|
||||
backup_dir = get_tmp_dir(self.host.settings, self.cat_dir, SECURE_UNLINK_DIR)
|
||||
if not os.path.exists(backup_dir):
|
||||
os.makedirs(backup_dir)
|
||||
filename = os.path.basename(path)
|
||||
|
|
@ -210,15 +213,9 @@ class BaseEdit(object):
|
|||
content_file_obj.close()
|
||||
|
||||
# we prepare arguments
|
||||
editor = config.config_get(
|
||||
self.sat_conf, C.CONFIG_SECTION, "editor"
|
||||
) or os.getenv("EDITOR", "vi")
|
||||
try:
|
||||
# is there custom arguments in sat.conf ?
|
||||
editor_args = config.config_get(
|
||||
self.sat_conf, C.CONFIG_SECTION, editor_args_opt, Exception
|
||||
)
|
||||
except (NoOptionError, NoSectionError):
|
||||
editor = self.host.cli_settings.editor
|
||||
editor_args = self.host.cli_settings.editor_args_opt
|
||||
if editor_args is None:
|
||||
# no, we check if we know the editor and have special arguments
|
||||
if self.use_metadata:
|
||||
editor_args = EDITOR_ARGS_MAGIC.get(os.path.basename(editor), "")
|
||||
|
|
@ -291,7 +288,7 @@ class BaseEdit(object):
|
|||
|
||||
if len(content) == 0:
|
||||
self.disp("Content is empty, cancelling the edition")
|
||||
if content_file_path.parent != get_tmp_dir(self.sat_conf, self.cat_dir):
|
||||
if content_file_path.parent != get_tmp_dir(self.host.settings, self.cat_dir):
|
||||
self.disp(
|
||||
"File are not in SàT temporary hierarchy, we do not remove them",
|
||||
2,
|
||||
|
|
@ -350,7 +347,7 @@ class BaseEdit(object):
|
|||
"""
|
||||
suff = "." + self.get_tmp_suff()
|
||||
cat_dir_str = self.cat_dir
|
||||
tmp_dir = get_tmp_dir(self.sat_conf, self.cat_dir, self.profile)
|
||||
tmp_dir = get_tmp_dir(self.host.settings, self.cat_dir, self.profile)
|
||||
if not tmp_dir.exists():
|
||||
try:
|
||||
tmp_dir.mkdir(parents=True)
|
||||
|
|
@ -381,7 +378,7 @@ class BaseEdit(object):
|
|||
# we guess the item currently edited by choosing
|
||||
# the most recent file corresponding to temp file pattern
|
||||
# in tmp_dir, excluding metadata files
|
||||
tmp_dir = get_tmp_dir(self.sat_conf, self.cat_dir, profile)
|
||||
tmp_dir = get_tmp_dir(self.host.settings, self.cat_dir, profile)
|
||||
available = [
|
||||
p
|
||||
for p in tmp_dir.glob(f"{self.cat_dir}_*")
|
||||
|
|
|
|||
|
|
@ -28,7 +28,6 @@ class Const(constants.Const):
|
|||
APP_COMPONENT = "CLI"
|
||||
APP_NAME_ALT = "jp"
|
||||
APP_NAME_FILE = "libervia_cli"
|
||||
CONFIG_SECTION = APP_COMPONENT.lower()
|
||||
PLUGIN_CMD = "commands"
|
||||
PLUGIN_OUTPUT = "outputs"
|
||||
#: All outputs are managed directly by the command, ``extra_outputs`` must be
|
||||
|
|
|
|||
|
|
@ -68,8 +68,6 @@ class Template(object):
|
|||
command's template_data_mapping attribute will be used if it exists to convert
|
||||
data to a dict usable by the template.
|
||||
"""
|
||||
# media_dir is needed for the template
|
||||
self.host.media_dir = self.host.bridge.config_get("", "media_dir")
|
||||
cmd = self.host.command
|
||||
try:
|
||||
template_path = cmd.TEMPLATE
|
||||
|
|
|
|||
Loading…
Reference in a new issue