mirror of
https://github.com/Xe138/AI-Trader.git
synced 2026-04-08 19:57:24 -04:00
Implement load_config() function with comprehensive error handling - Loads and parses JSON config files - Raises ConfigValidationError for missing files - Raises ConfigValidationError for malformed JSON - Includes 3 passing tests for all error cases Test coverage: - test_load_config_valid_json: Verifies successful JSON parsing - test_load_config_file_not_found: Validates error on missing file - test_load_config_invalid_json: Validates error on malformed JSON
37 lines
843 B
Python
37 lines
843 B
Python
"""Configuration merging and validation for AI-Trader."""
|
|
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Dict, Any, Optional
|
|
|
|
|
|
class ConfigValidationError(Exception):
|
|
"""Raised when config validation fails."""
|
|
pass
|
|
|
|
|
|
def load_config(path: str) -> Dict[str, Any]:
|
|
"""
|
|
Load and parse JSON config file.
|
|
|
|
Args:
|
|
path: Path to JSON config file
|
|
|
|
Returns:
|
|
Parsed config dictionary
|
|
|
|
Raises:
|
|
ConfigValidationError: If file not found or invalid JSON
|
|
"""
|
|
config_path = Path(path)
|
|
|
|
if not config_path.exists():
|
|
raise ConfigValidationError(f"Config file not found: {path}")
|
|
|
|
try:
|
|
with open(config_path, 'r') as f:
|
|
return json.load(f)
|
|
except json.JSONDecodeError as e:
|
|
raise ConfigValidationError(f"Invalid JSON in {path}: {e}")
|