-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
58 lines (44 loc) · 1.75 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
import configparser
import logging
CONFIG = None
def read_config():
global CONFIG
if CONFIG is None:
CONFIG = configparser.ConfigParser()
CONFIG.read("config.ini")
def get_server_settings():
read_config()
ip = CONFIG.get("default", "ip", fallback="0.0.0.0")
port = CONFIG.getint("default", "port", fallback=2525)
capture = CONFIG.get("default", "capture", fallback="ALL")
if capture not in ["ALL", "UNPARSED", "NONE"]:
logging.error(f'Unknown value in the config file for capture: "{capture}"')
return {"ip": ip, "port": port, "capture": capture}
def get_notif_settings():
read_config()
return {
"debug": CONFIG.get("slack", "debug", fallback=True),
"slack_token": CONFIG.get("slack", "token", fallback=""),
"channel_id": CONFIG.get("slack", "channel_id", fallback=""),
"threshold_percentage": CONFIG.get("slack", "threshold_percentage", fallback=0.25),
}
def setup_logging():
read_config()
loglevel = CONFIG.get("log", "level", fallback="DEBUG")
logfile = CONFIG.get("log", "filename", fallback=None)
numeric_level = getattr(logging, loglevel.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError(f"Invalid log level: {loglevel}")
logging.basicConfig(
filename=logfile,
level=numeric_level,
format="[%(asctime)s] [%(funcName)s:%(lineno)d] %(message)s",
)
def get_db_file():
read_config()
return CONFIG.get("database", "location", fallback="db.sqlite3")
if __name__ == "__main__":
setup_logging()
logging.debug(f"get_server_settings()={get_server_settings()}")
logging.debug(f"get_notif_settings()={get_notif_settings()}")
logging.debug(f"get_db_file()='{get_db_file()}'")