File size: 2,043 Bytes
d7efa84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""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'")