-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
59 lines (42 loc) · 1.12 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import os
from attr import dataclass
from dotenv import load_dotenv
load_dotenv()
class EnvIsNotDefined(Exception):
def __init__(self, key: str):
super().__init__(f"environment variable {key} is not defined")
def env_required(key: str) -> str:
value = os.getenv(key)
if not value:
raise EnvIsNotDefined(key)
return value
def env_with_default(key: str, default: str = "") -> str:
return os.getenv(key, default)
@dataclass
class TgBot:
token: str
admin_ids: list[int]
@dataclass
class DBConfig:
host: str
port: str
user: str
name: str
password: str
@dataclass
class Config:
tg_bot: TgBot
db: DBConfig
config = Config(
tg_bot=TgBot(
token=env_required("TELEGRAM_BOT_TOKEN"),
admin_ids=list(map(int, env_required("ADMIN_IDS").split())),
),
db=DBConfig(
host=env_with_default("DB_HOST", "localhost"),
port=env_with_default("DB_PORT", "5432"),
user=env_with_default("DB_USER", "postgres"),
name=env_with_default("DB_NAME", "postgres"),
password=env_with_default("DB_PASSWORD", "postgres"),
)
)