2024-11-02 18:46:53 +00:00
|
|
|
"""
|
|
|
|
Configuration file handling
|
|
|
|
|
|
|
|
Takes care of parsing the configuration file if it exists.
|
|
|
|
"""
|
|
|
|
|
2024-11-02 22:30:39 +00:00
|
|
|
import pathlib
|
|
|
|
|
2024-11-02 18:46:53 +00:00
|
|
|
import pydantic
|
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
|
|
class Config(pydantic.BaseModel):
|
|
|
|
domain_aliases: dict[str, str] = pydantic.Field(default=dict())
|
|
|
|
|
|
|
|
|
|
|
|
def get_configuration(*, path: str | pathlib.Path | None = None) -> Config:
|
|
|
|
"""Reads and parses the configuration file at <path> if provided, otherwise defaults to ~/.config/frg.yaml"""
|
|
|
|
if not path:
|
|
|
|
path = pathlib.Path.home().joinpath(".config", "frg.yaml")
|
|
|
|
|
|
|
|
config_path = pathlib.Path(path)
|
|
|
|
|
|
|
|
if not config_path.exists():
|
|
|
|
raise FileNotFoundError("Configuration not found at %s", str(path))
|
|
|
|
|
|
|
|
with open(config_path, "r", encoding="utf8") as config_file:
|
|
|
|
return Config(**yaml.safe_load(config_file))
|