38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
import yaml
|
|
from pydantic_settings import BaseSettings
|
|
from logger import logger
|
|
|
|
# Configuration path
|
|
config_path = "config/config.yaml"
|
|
|
|
class Settings(BaseSettings):
|
|
|
|
class Config:
|
|
extra = "allow"
|
|
|
|
class Config:
|
|
def __init__(self, path: str = config_path):
|
|
self.path = path
|
|
self.config = None
|
|
|
|
|
|
def load_config(self):
|
|
try:
|
|
with open(self.path, 'r') as file:
|
|
logger.info({"config_load": f"load_config:{self.path}"})
|
|
data = yaml.safe_load(file)
|
|
logger.info({"config_load": f"config load successfully"})
|
|
self.config = Settings(**data)
|
|
#logger.info({"config_update": f"Config updated with: {data}"})
|
|
except FileNotFoundError as e:
|
|
logger.error({"config_err": f"File not found: {e}"})
|
|
raise Exception(f"File not found: {e}")
|
|
except yaml.YAMLError as e:
|
|
logger.error({"config_err": f"YAML error: {e}"})
|
|
raise Exception(f"YAML error: {e}")
|
|
except Exception as e:
|
|
logger.error({"config_err": f"Unexpected error: {e}"})
|
|
raise Exception(f"Failed to load config: {e}")
|
|
|
|
def reload_config(self) -> None:
|
|
self.load_config()
|