36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
import sys
|
|
|
|
from pydantic import ValidationError
|
|
from pydantic_settings import BaseSettings
|
|
|
|
RED = '\033[91m'
|
|
YELLOW = '\033[33m'
|
|
RESET = '\033[0m'
|
|
BOLD = '\033[1m'
|
|
|
|
|
|
def load_settings(settings_class: type[BaseSettings]) -> BaseSettings:
|
|
try:
|
|
return settings_class()
|
|
except ValidationError as e:
|
|
print(f'{RED}{BOLD}Missing environment variables:{RESET}')
|
|
|
|
missing = set()
|
|
for err in e.errors():
|
|
if err['type'] == 'missing' and len(err['loc']) == 1:
|
|
field = settings_class.model_fields.get(str(err['loc'][0]))
|
|
if field and hasattr(field.annotation, 'model_fields'):
|
|
for name, info in field.annotation.model_fields.items():
|
|
if info.is_required():
|
|
missing.add(f'{err["loc"][0]}__{name}'.upper())
|
|
else:
|
|
missing.add('__'.join(str(loc).upper() for loc in err['loc']))
|
|
else:
|
|
missing.add('__'.join(str(loc).upper() for loc in err['loc']))
|
|
|
|
for field in sorted(missing):
|
|
print(f' {YELLOW}{field}{RESET}')
|
|
|
|
print(f'\n{RED}Check .env file or set required variables{RESET}')
|
|
sys.exit(1)
|