from importlib import resources import yaml import os from typing import Any from pathlib import Path from core.errors.logger import logger PRE_PACKAGED_YAML_FOLDER = 'core.assets.yaml_mappings' def load_yaml(which_yaml: str) -> Any | bool: try: # Load the YAML file text_content = resources.files(PRE_PACKAGED_YAML_FOLDER).joinpath(which_yaml).read_text() # Check if content is empty or blank if not text_content or not text_content.strip(): logger.error(f"YAML file {which_yaml} is empty or blank") return False # Parse and return the YAML return yaml.safe_load(text_content) except FileNotFoundError: logger.error(f"YAML file not found: {which_yaml}") return False except yaml.YAMLError as e: logger.error(f"Failed to parse YAML file {which_yaml}: {e}") return False except Exception as e: logger.error(f"Unexpected error loading YAML {which_yaml}: {e}") return False def save_yaml(data: Any, path: str) -> bool: try: # Convert to Path object for cleaner handling file_path = Path(path) # Create parent directories if they don't exist file_path.parent.mkdir(parents=True, exist_ok=True) # Write the YAML file with open(file_path, 'w') as file: yaml.safe_dump(data, file, default_flow_style=False) return True except TypeError as e: logger.error(f"Data is not YAML-serializable: {e}") return False except IOError as e: logger.error(f"Failed to write YAML file to {path}: {e}") return False except Exception as e: logger.error(f"Unexpected error saving YAML to {path}: {e}") return False def is_running_in_appimage(): """Check if running inside an AppImage""" return bool(os.environ.get('APPIMAGE'))