forge-tools/frg/configuration.py

29 lines
788 B
Python
Raw Permalink Normal View History

"""
Configuration file handling
Takes care of parsing the configuration file if it exists.
"""
import pathlib
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))