-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
41 lines (36 loc) · 1.09 KB
/
config.py
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
import yaml
import os
from pathlib import Path
from typing import Dict
class Config:
"""
Configuration class
"""
def __init__(self, config_path: str = "config.yaml"):
self.config_path = Path(config_path)
if not self.config_path.exists():
CONFIG_PATH = os.getenv("CONFIG_PATH", "config.yaml")
self.config_path = Path(CONFIG_PATH)
if not self.config_path.exists():
raise ValueError(f"Configuration file {self.config_path} not found")
self.data = self.load_config(self.config_path)
def load_config(self, path:str):
"""
Load yaml configuration file
"""
config = None
with open(path, "r") as f:
config = yaml.safe_load(f)
return config
def get(self, key: str, default=None):
"""
Get configuration value with a fallback default
"""
return self.data.get(key, default)
# Initialize the config instance globally
cfg = Config()
def get_config() -> Dict:
"""
Get the global configuration instance
"""
return cfg.data