1
from typing import Any, List
4
from pybotx import BotAccountWithSecret
5
from pydantic import BaseSettings
8
class AppSettings(BaseSettings):
13
def parse_env_var(cls, field_name: str, raw_val: str) -> Any:
14
if field_name == "BOT_CREDENTIALS":
19
cls._build_credentials_from_string(credentials_str)
20
for credentials_str in raw_val.replace(",", " ").split()
22
elif field_name == "SMARTLOG_DEBUG_HUIDS":
23
return cls.parse_smartlog_debug_huids(raw_val)
25
return cls.json_loads(raw_val)
28
def parse_smartlog_debug_huids(cls, raw_huids: Any) -> List[UUID]:
29
"""Parse debug huids separated by comma."""
33
return [UUID(huid) for huid in raw_huids.split(",")]
36
def _build_credentials_from_string(
37
cls, credentials_str: str
38
) -> BotAccountWithSecret:
39
credentials_str = credentials_str.replace("|", "@")
40
assert credentials_str.count("@") == 2, "Have you forgot to add `bot_id`?"
42
cts_url, secret_key, bot_id = [
43
str_value.strip() for str_value in credentials_str.split("@")
46
if "://" not in cts_url:
47
cts_url = f"https://{cts_url}"
49
return BotAccountWithSecret(
50
id=UUID(bot_id), cts_url=cts_url, secret_key=secret_key
53
BOT_CREDENTIALS: List[BotAccountWithSecret]
59
SMARTLOG_DEBUG_HUIDS: List[UUID]
63
SQL_DEBUG: bool = False
66
WORKER_TIMEOUT_SEC: float = 4
69
settings = AppSettings()