-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig_manager.py
30 lines (24 loc) · 999 Bytes
/
config_manager.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
import os
from dotenv import load_dotenv
class ConfigManager:
def __init__(self, mode='dev'):
self.config = {}
self.mode = mode
self.load_config()
def load_config(self):
if self.mode == 'prod':
env_path = os.path.join('config/production', '.env')
else:
env_path = os.path.join('config/development', '.env')
if not os.path.exists(env_path):
raise FileNotFoundError(f"The environment file '{env_path}' does not exist.")
load_dotenv(env_path)
self.config = {key: os.getenv(key) for key in os.environ if os.getenv(key) is not None}
def get_config_value(self, key, value_type=str):
value = self.config.get(key)
if value is None:
raise KeyError(f"Key '{key}' not found in config.")
try:
return value_type(value)
except ValueError:
raise ValueError(f"Value for key '{key}' cannot be converted to {value_type.__name__}")