-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.py
67 lines (50 loc) · 1.99 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
60
61
62
63
64
65
66
67
import configparser
import logging
import sys
from typing import Dict, Optional, Set, TypedDict
logger = logging.getLogger(__name__)
CONFIG_REQUIRED_SECTIONS: Set[str] = {"default", "telegram", "directories"}
class AppConfig(TypedDict):
telegram_bot_token: str
telegram_user_id: str
log_path: Optional[str]
whispers_notifier_interval: float
telegram_polling_interval: float
CONFIG_REQUIRED_FIELDS_MAP: Dict[str, str] = {
"telegram.TelegramBotToken": "telegram_bot_token",
"telegram.TelegramUserId": "telegram_user_id",
"directories.LogPath": "log_path",
"default.ObserverCooldownSecs": "whispers_notifier_interval",
"default.TelegramPollingCooldownSecs": "telegram_polling_interval",
}
REQUIRED_FIELDS_TYPES_OVERRIDE = {
"whispers_notifier_interval": float,
"telegram_polling_interval": float,
}
def get_tree_value_or_exception(config: configparser.ConfigParser, key: str) -> str:
section, field = key.split(".")
try:
return config[section][field]
except:
raise ValueError(f"Missing {key} variable in ini-config")
def init_app_config() -> AppConfig:
try:
config = configparser.ConfigParser()
config.read("config.ini")
for section in config.sections():
if section not in CONFIG_REQUIRED_SECTIONS:
raise ValueError("Missing section in config.ini: [%s]", section)
app_config = AppConfig()
for field in CONFIG_REQUIRED_FIELDS_MAP.keys():
value = get_tree_value_or_exception(config, field)
alias = CONFIG_REQUIRED_FIELDS_MAP[field]
# Cast to specific type if needed
if alias in REQUIRED_FIELDS_TYPES_OVERRIDE:
value = REQUIRED_FIELDS_TYPES_OVERRIDE[alias](value)
app_config[alias] = value
return app_config
except ValueError as e:
logging.error(str(e))
sys.exit()
except Exception as e:
logging.warning(f"Unhandled error in init_configs: {str(e)}")