feat(typing): Start adding python types for the configuration

This commit is contained in:
2026-04-30 10:49:46 +02:00
parent 2fd69cc595
commit 31426c006b
5 changed files with 70 additions and 21 deletions
+1 -11
View File
@@ -183,20 +183,10 @@
"description": "The directory for the bootloader files. Has to end in slash", "description": "The directory for the bootloader files. Has to end in slash",
"pattern": "^(^(~|\\.|\\.\\.)?\\/)?([\\w\\/.-]+(?!.*[^\\w\\/.-]+))\\/$" "pattern": "^(^(~|\\.|\\.\\.)?\\/)?([\\w\\/.-]+(?!.*[^\\w\\/.-]+))\\/$"
}, },
"theme": { "theme_folder": {
"type": "object",
"description": "Configuration for the bootloader theme",
"properties": {
"folder": {
"type": "string", "type": "string",
"description": "Where the folder for the theme is found. Can be relative to the config repo or file system and has to end in slash", "description": "Where the folder for the theme is found. Can be relative to the config repo or file system and has to end in slash",
"pattern": "^(^(~|\\.|\\.\\.)?\\/)?([\\w\\/.-]+(?!.*[^\\w\\/.-]+))\\/$" "pattern": "^(^(~|\\.|\\.\\.)?\\/)?([\\w\\/.-]+(?!.*[^\\w\\/.-]+))\\/$"
}
},
"additionalProperties": false,
"required": [
"folder"
]
}, },
"os_prober": { "os_prober": {
"type": "boolean", "type": "boolean",
+1 -2
View File
@@ -23,8 +23,7 @@ users:
boot: boot:
bootloader: grub bootloader: grub
esp_dir: /boot/ esp_dir: /boot/
theme: theme_folder: ~/.path/to/theme/
folder: ~/.path/to/theme/
os_prober: False os_prober: False
# Also copies over the /etc/default/grub config or equivalent for other supported bootloaders # Also copies over the /etc/default/grub config or equivalent for other supported bootloaders
+5 -4
View File
@@ -2,6 +2,7 @@ from typing import Any, cast
import yaml import yaml
from config import validator from config import validator
from config.dtype import ArchMgrConfig
from config.merger import merge_configs from config.merger import merge_configs
@@ -11,7 +12,7 @@ def _load_config_file(file: str):
return parsed return parsed
def load_config(file: str): def load_config(file: str) -> ArchMgrConfig:
# Load and validate initial config # Load and validate initial config
try: try:
loaded_conf = _load_config_file(file) loaded_conf = _load_config_file(file)
@@ -20,9 +21,9 @@ def load_config(file: str):
if not validator.validate(loaded_conf): if not validator.validate(loaded_conf):
return {} return {}
conf = cast(dict[str, Any], loaded_conf) configuration = cast(dict[str, Any], loaded_conf)
requires = cast(list[str], conf["requires"]) requires = cast(list[str], configuration.pop("requires"))
conf.pop("requires") conf = cast(ArchMgrConfig, configuration)
# Recursively load files # Recursively load files
for conf_file in requires: for conf_file in requires:
+56
View File
@@ -0,0 +1,56 @@
from typing import Optional, TypedDict
class ArchMgrConfig(TypedDict):
pkgs: ArchMgrPkgConfig
users: list[ArchMgrUserConfig]
class ArchMgrPkgConfig(TypedDict):
individual: list[str]
bundles: list[object] # TODO: Better typing
repos: object # TODO: Repo typing
class ArchMgrUserConfig(TypedDict):
username: str
groups: list[str]
home_dir: bool
class ArchMgrBootConfig(TypedDict):
bootloader: str
esp_dir: str
theme_folder: str
os_prober: bool
class ArchMgrThemeConfig(TypedDict):
gtk: str
qt: str
font: str
icon_theme: str
cursor_theme: str
class ArchMgrGitConfig(TypedDict):
creds: object # TODO:
repos: object # TODO
class ArchMgrGitRepoConfig(TypedDict):
clone_path: str
url: str
class ArchMgrCmdsConfig(TypedDict):
always: ArchMgrCommand
once: ArchMgrCommand
class ArchMgrCommand(TypedDict):
cmd: str
name: Optional[str]
capture_output: Optional[bool]
hook: Optional[str]
user: Optional[str]
+4 -1
View File
@@ -1,4 +1,7 @@
def merge_configs(config: dict, new_config: dict): from config.dtype import ArchMgrConfig
def merge_configs(config: ArchMgrConfig, new_config: ArchMgrConfig) -> ArchMgrConfig:
if len(new_config) == 0 or len(config) == 0: if len(new_config) == 0 or len(config) == 0:
return config return config