File size: 4,892 Bytes
acd8e16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a026fe5
 
 
 
 
acd8e16
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
"""
Configuration Loader
Loads and manages configuration from YAML files.
"""

import yaml
import os
from typing import Dict, Any, Optional
from dataclasses import dataclass


@dataclass
class AppConfig:
    """Application configuration."""
    title: str
    description: str
    theme: str
    server_host: str
    server_port: int
    server_share: bool


@dataclass
class LeaderboardConfig:
    """Leaderboard configuration."""
    path: str
    columns: list
    top_results: int
    results_table_headers: list


@dataclass
class MetricsConfig:
    """Metrics configuration."""
    weights: Dict[str, float]
    descriptions: Dict[str, str]
    thresholds: Dict[str, float]
    formatting: Dict[str, str]


@dataclass
class PromptsConfig:
    """Prompts configuration."""
    files: Dict[str, str]
    fallback: str
    placeholders: Dict[str, str]
    sections: Dict[str, str]


class ConfigLoader:
    """Loads and manages configuration from YAML files."""
    
    def __init__(self, config_dir: str = "config"):
        self.config_dir = config_dir
        self._app_config = None
        self._leaderboard_config = None
        self._metrics_config = None
        self._prompts_config = None
    
    def _load_yaml(self, filename: str) -> Dict[str, Any]:
        """Load a YAML configuration file."""
        filepath = os.path.join(self.config_dir, filename)
        if not os.path.exists(filepath):
            raise FileNotFoundError(f"Configuration file not found: {filepath}")
        
        with open(filepath, 'r') as f:
            return yaml.safe_load(f)
    
    def get_app_config(self) -> AppConfig:
        """Get application configuration."""
        if self._app_config is None:
            config = self._load_yaml("app.yaml")
            app = config["app"]
            server = app["server"]
            
            self._app_config = AppConfig(
                title=app["title"],
                description=app["description"],
                theme=app["theme"],
                server_host=server["host"],
                server_port=server["port"],
                server_share=server["share"]
            )
        
        return self._app_config
    
    def get_leaderboard_config(self) -> LeaderboardConfig:
        """Get leaderboard configuration."""
        if self._leaderboard_config is None:
            config = self._load_yaml("app.yaml")
            leaderboard = config["leaderboard"]
            display = leaderboard["display"]
            
            self._leaderboard_config = LeaderboardConfig(
                path=leaderboard["path"],
                columns=leaderboard["columns"],
                top_results=display["top_results"],
                results_table_headers=display["results_table_headers"]
            )
        
        return self._leaderboard_config
    
    def get_metrics_config(self) -> MetricsConfig:
        """Get metrics configuration."""
        if self._metrics_config is None:
            config = self._load_yaml("metrics.yaml")
            metrics = config["metrics"]
            
            self._metrics_config = MetricsConfig(
                weights=metrics["weights"],
                descriptions=metrics["descriptions"],
                thresholds=metrics["thresholds"],
                formatting=metrics["formatting"]
            )
        
        return self._metrics_config
    
    def get_prompts_config(self) -> PromptsConfig:
        """Get prompts configuration."""
        if self._prompts_config is None:
            config = self._load_yaml("prompts.yaml")
            prompts = config["prompts"]
            
            self._prompts_config = PromptsConfig(
                files=prompts["files"],
                fallback=prompts["fallback"],
                placeholders=prompts["placeholders"],
                sections=prompts["sections"]
            )
        
        return self._prompts_config
    
    def get_dialects(self) -> list:
        """Get available SQL dialects."""
        config = self._load_yaml("app.yaml")
        return config["dialects"]
    
    def get_ui_config(self) -> Dict[str, Any]:
        """Get UI configuration."""
        config = self._load_yaml("app.yaml")
        return config["ui"]
    
    def get_environment_config(self) -> Dict[str, Any]:
        """Get environment configuration."""
        config = self._load_yaml("app.yaml")
        return config["environment"]
    
    def get_mock_sql_config(self) -> Dict[str, Any]:
        """Get mock SQL configuration."""
        config = self._load_yaml("metrics.yaml")
        return config["mock_sql"]
    
    def get_visible_datasets(self) -> list:
        """Get list of visible datasets from configuration."""
        config = self._load_yaml("app.yaml")
        return config.get("visible_datasets", [])


# Global configuration loader instance
config_loader = ConfigLoader()