Files
archmgr/config/__init__.py
T

109 lines
2.8 KiB
Python

from typing import Any, cast
import yaml
from config import validator
from config.dtype import ArchMgrConfig
from config.merger import merge_configs
def load_config_file(file: str):
"""Load and parse the config file.
No verification is done and is not cast
Args:
file: The path to the file to be loaded
Returns:
The loaded and parsed file.
"""
with open(file, "r") as f:
parsed = yaml.load(f, Loader=yaml.FullLoader)
return parsed
def default_config() -> ArchMgrConfig:
"""Get the default configuration
Returns:
The default config
"""
return {
"pkgs": {
"individual": [],
"bundles": [],
"repos": {
"enabled_repos": [
{
"name": "core",
"setup_cmds": [],
"mirrors": {"use_default": True, "extra_mirrors": []},
}
],
"reflector": {
"enabled": False,
"count": 0,
"countries": [],
"interval": 1,
},
},
},
"boot": {
"managed": False,
"bootloader": "grub",
"esp_dir": "/boot",
"os_prober": False,
"theme_folder": "/usr/share/themes/grub",
},
"cmds": {"always": [], "once": []},
"git": {
"repos": [],
"creds": {"manager": "git-credential-manager"},
},
"users": [],
"symlinks": [],
"template_data": [],
"themes": {
"cursor_theme": "oreo_spark_blue_cursor",
"font": "Comfortaa 11",
"gtk": "Adaptive-Theme",
"qt": "gtk3",
"icon_theme": "candy-icons",
},
}
requires_list: list[str] = []
source_list = {} # for each setting, in which files it appears
def load_config(file: str) -> ArchMgrConfig:
"""Load the configuration from the specified file path
Args:
file: Path to the file to be loaded
Returns:
The loaded, validated and parsed config
"""
global requires_list, source_list
# Load and validate initial config
try:
loaded_conf = load_config_file(file)
except Exception:
return default_config()
if not validator.validate(loaded_conf):
return default_config()
configuration = cast(dict[str, Any], loaded_conf)
requires = cast(list[str], configuration.pop("requires"))
# Keep track of all files
requires_list += requires
conf = cast(ArchMgrConfig, configuration)
# Recursively load files
for conf_file in requires:
conf = merge_configs(conf, load_config(conf_file))
return conf