Team Ai
Apppublic

Underground-Digital/Workflow-Engine

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
yaml_utils.py36 linesDownload Raw Back to utils
1import logging2from pathlib import Path3from typing import Any4 5import yaml6from yaml import YAMLError7 8logger = logging.getLogger(__name__)9 10 11def load_yaml_file(file_path: str, ignore_error: bool = True, default_value: Any = {}) -> Any:12    """13    Safe loading a YAML file14    :param file_path: the path of the YAML file15    :param ignore_error:16        if True, return default_value if error occurs and the error will be logged in debug level17        if False, raise error if error occurs18    :param default_value: the value returned when errors ignored19    :return: an object of the YAML content20    """21    if not file_path or not Path(file_path).exists():22        if ignore_error:23            return default_value24        else:25            raise FileNotFoundError(f"File not found: {file_path}")26 27    with open(file_path, encoding="utf-8") as yaml_file:28        try:29            yaml_content = yaml.safe_load(yaml_file)30            return yaml_content or default_value31        except Exception as e:32            if ignore_error:33                return default_value34            else:35                raise YAMLError(f"Failed to load YAML file {file_path}: {e}") from e36