Spaces:
Sleeping
Sleeping
| """Configuration loading and validation for the Audio Labeling Tool.""" | |
| import os | |
| import logging | |
| from pathlib import Path | |
| import yaml | |
| logger = logging.getLogger(__name__) | |
| DEFAULT_CONFIG_PATH = "/app/config.yaml" | |
| def load_config() -> dict: | |
| """Load application configuration from YAML file. | |
| Config path resolution: | |
| 1. ALT_CONFIG_PATH environment variable | |
| 2. Default path: /app/config.yaml | |
| """ | |
| config_path = os.environ.get("ALT_CONFIG_PATH", DEFAULT_CONFIG_PATH) | |
| path = Path(config_path) | |
| if not path.exists(): | |
| raise FileNotFoundError(f"Configuration file not found: {config_path}") | |
| with open(path, "r", encoding="utf-8") as f: | |
| config = yaml.safe_load(f) | |
| _validate_config(config) | |
| return config | |
| def _validate_config(config: dict) -> None: | |
| """Validate config structure.""" | |
| if "labelers" not in config: | |
| raise ValueError("Config must contain 'labelers' key") | |
| if len(config["labelers"]) != 2: | |
| raise ValueError("Exactly 2 labelers must be configured") | |
| for name, labeler_cfg in config["labelers"].items(): | |
| if "password" not in labeler_cfg: | |
| raise ValueError(f"Labeler '{name}' missing 'password'") | |
| if "audio_folder" not in labeler_cfg: | |
| raise ValueError(f"Labeler '{name}' missing 'audio_folder'") | |
| if "reference_json" not in labeler_cfg: | |
| raise ValueError(f"Labeler '{name}' missing 'reference_json'") | |
| if "output_dir" not in labeler_cfg: | |
| raise ValueError(f"Labeler '{name}' missing 'output_dir'") | |
| if "clean_audios_dir" not in labeler_cfg: | |
| raise ValueError(f"Labeler '{name}' missing 'clean_audios_dir'") | |
| if "admin" not in config: | |
| raise ValueError("Config must contain 'admin' key") | |
| if "username" not in config["admin"] or "password" not in config["admin"]: | |
| raise ValueError("Admin must have 'username' and 'password'") | |
| if "shared_output_dir" not in config: | |
| raise ValueError("Config must contain 'shared_output_dir'") | |