Skip to content

Commit 05c9003

Browse files
author
leoguillaume
committed
feat: add secrets in playground settings
1 parent e672f85 commit 05c9003

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

ui/settings.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from functools import lru_cache
22
import os
3-
from typing import Any, Literal, Optional
3+
import re
4+
from typing import Any, Optional
45

56
from pydantic import BaseModel, field_validator, model_validator
67
from pydantic_settings import BaseSettings
@@ -30,7 +31,6 @@ class Config(ConfigBaseModel):
3031

3132

3233
class 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

Comments
 (0)