from pathlib import Path from typing import Optional import yaml from datadog_tools.config import ( DEFAULT_DEFINITION_FILENAME, ) def get_full_entity_definition_file_path( repository_path: str, entity_path: str, entity_definition_file_path: Optional[str] = None, ) -> Path: """ Resolve the absolute path to an entity’s definition file. :param repository_path: The base directory of the whole repo. :param entity_path: The relative directory inside the repository where the entity lives. :param entity_definition_file_path: If supplied, this is interpreted as a path *relative to the entity's* base directory. :return: The absolute path to the entity definition file. """ repository_path = Path(repository_path).resolve() if entity_definition_file_path: return Path(repository_path / entity_definition_file_path) else: return Path(repository_path / entity_path / DEFAULT_DEFINITION_FILENAME) def get_entity_kind_from_entity_definition_file( entity_definition_file_path: Path, ) -> Optional[str]: """ Extract the kind field from a YAML definition file. Throws if the file cannot be read or parsed. :param entity_definition_file_path: The absolute path to the YAML file. :return: The kind of the entity, lowercased. If the kind is missing or not a string, returns ``None``. """ with entity_definition_file_path.open("r", encoding="utf-8") as f: content = yaml.safe_load(f) kind = content.get("kind") if isinstance(kind, str): return kind.strip().lower() return None