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
|
|
@ -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}_*")
|
||||
|
|
|
|||
Loading…
Reference in a new issue