File size: 1,600 Bytes
b510add
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Seuils métier configurables via config.yaml (optionnel) ou variables d'environnement.

Recherche, dans l'ordre : argument explicite -> variable d'env CONFIG_PATH ->
config.yaml à la racine du module -> valeurs par défaut.
"""
from dataclasses import dataclass
from pathlib import Path
import logging
import os

logger = logging.getLogger(__name__)

MODULE_ROOT = Path(__file__).resolve().parent.parent


@dataclass
class RuleConfig:
    consistency_tolerance: float = 0.02
    match_tolerance: float = 0.03
    liters_upper_bound: float = 500.0
    price_min: float = 200.0
    price_max: float = 2000.0
    business_confidence_threshold: float = 0.5
    blur_threshold: float = 80.0
    dark_threshold: float = 40.0
    bright_threshold: float = 240.0


def load_config(path: str | None = None) -> RuleConfig:
    candidate = path or os.environ.get("CONFIG_PATH") or str(MODULE_ROOT / "config.yaml")
    candidate_path = Path(candidate)

    if not candidate_path.is_file():
        return RuleConfig()

    try:
        import yaml
        with open(candidate_path, "r", encoding="utf-8") as f:
            data = yaml.safe_load(f) or {}
    except Exception as e:
        logger.warning(f"Impossible de charger {candidate_path} : {e}. Utilisation des valeurs par défaut.")
        return RuleConfig()

    rules_data = data.get("rules", data) if isinstance(data, dict) else {}
    defaults = RuleConfig()
    known_fields = defaults.__dataclass_fields__.keys()
    kwargs = {k: v for k, v in rules_data.items() if k in known_fields}
    return RuleConfig(**{**defaults.__dict__, **kwargs})