11from functools import lru_cache
22import os
3- from typing import Any , Literal , Optional
3+ import re
4+ from typing import Any , Optional
45
56from pydantic import BaseModel , field_validator , model_validator
67from pydantic_settings import BaseSettings
@@ -30,7 +31,6 @@ class Config(ConfigBaseModel):
3031
3132
3233class Settings (BaseSettings ):
33- log_level : Literal ["DEBUG" , "INFO" , "WARNING" , "ERROR" , "CRITICAL" ] = "INFO"
3434 config_file : str = "config.yml"
3535
3636 class Config :
@@ -43,9 +43,17 @@ def config_file_exists(cls, config_file):
4343
4444 @model_validator (mode = "after" )
4545 def setup_config (cls , values ) -> Any :
46- stream = open (file = values .config_file , mode = "r" )
47- config = Config (** yaml .safe_load (stream = stream ))
48- stream .close ()
46+ with open (file = values .config_file , mode = "r" ) as f :
47+ file_content = f .read ()
48+ f .close ()
49+
50+ # replace environment variables (pattern: ${VARIABLE_NAME})
51+ for match in set (re .findall (pattern = r"\${[A-Z_]+}" , string = file_content )):
52+ variable = match .replace ("${" , "" ).replace ("}" , "" )
53+ assert os .getenv (variable ), f"Environment variable { variable } not found or empty to replace { match } ."
54+ file_content = file_content .replace (match , os .environ [variable ])
55+
56+ config = Config (** yaml .safe_load (file_content ))
4957
5058 values .auth = config .auth
5159 values .playground = config .playground
0 commit comments