Spaces:
Paused
Paused
File size: 1,458 Bytes
5a3b9db | 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 | import os
import yaml
from pathlib import Path
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
class ConfigManager:
"""
Singleton Configuration Manager that loads YAML configurations
based on the SENTINEL_ENV environment variable.
"""
_instance = None
_config = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(ConfigManager, cls).__new__(cls)
cls._instance._load_config()
return cls._instance
def _load_config(self):
env = os.getenv("SENTINEL_ENV", "development")
# Determine the project root assuming this file is in src/utils/
project_root = Path(__file__).parent.parent.parent
config_path = project_root / "config" / env / "config.yaml"
if not config_path.exists():
raise FileNotFoundError(f"Configuration file not found: {config_path}")
with open(config_path, "r") as f:
self._config = yaml.safe_load(f)
def get(self, key_path: str, default=None):
"""
Retrieve a configuration value using dot-notation.
Example: config.get("paths.raw_data")
"""
keys = key_path.split('.')
val = self._config
for k in keys:
if isinstance(val, dict) and k in val:
val = val[k]
else:
return default
return val
|