read_settings.py 992 B

1234567891011121314151617181920212223242526272829303132333435
  1. import logging
  2. import sys
  3. from pathlib import Path
  4. from typing import NoReturn
  5. import tomli
  6. from app.git import is_git_repo
  7. from app.schema import Settings
  8. logger = logging.getLogger(__name__)
  9. def read_settings(filepath: str, check_valid_repo: bool = True) -> Settings | NoReturn:
  10. """Read settings.toml from the root of the project folder"""
  11. try:
  12. with open(filepath, mode="rb") as f:
  13. config = tomli.load(f)
  14. if check_valid_repo:
  15. git_repo = str(Path(config["git_repo"]).expanduser())
  16. if not is_git_repo(str(git_repo)):
  17. raise FileNotFoundError("git-repo has not been found")
  18. config["git_repo"] = git_repo
  19. settings = Settings(**config)
  20. return settings
  21. except FileNotFoundError:
  22. logger.error(
  23. f"error => {filepath} has not been found\n",
  24. " please check the README for instructions",
  25. )
  26. sys.exit(1)