Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

yaml_utils.py 997B

123456789101112131415161718192021222324252627282930313233
  1. import logging
  2. from functools import lru_cache
  3. from pathlib import Path
  4. from typing import Any
  5. import yaml # type: ignore
  6. from yaml import YAMLError
  7. logger = logging.getLogger(__name__)
  8. def _load_yaml_file(*, file_path: str):
  9. if not file_path or not Path(file_path).exists():
  10. raise FileNotFoundError(f"File not found: {file_path}")
  11. with open(file_path, encoding="utf-8") as yaml_file:
  12. try:
  13. yaml_content = yaml.safe_load(yaml_file)
  14. return yaml_content
  15. except Exception as e:
  16. raise YAMLError(f"Failed to load YAML file {file_path}: {e}") from e
  17. @lru_cache(maxsize=128)
  18. def load_yaml_file_cached(file_path: str) -> Any:
  19. """
  20. Cached version of load_yaml_file for static configuration files.
  21. Only use for files that don't change during runtime (e.g., position files)
  22. :param file_path: the path of the YAML file
  23. :return: an object of the YAML content
  24. """
  25. return _load_yaml_file(file_path=file_path)