-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.py
50 lines (37 loc) · 1.15 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
import os
import sys
import toml
class ConfigError(Exception):
"""
triggered when theres an issue with the config file
"""
class Config:
"""
config related functions
"""
def __init__(self, config_path=None):
self.config_file = (
os.path.abspath(config_path)
if config_path
else os.path.abspath("config.toml")
)
def read_config(self):
"""
read the config file and return the data
"""
try:
with open(self.config_file, encoding="utf-8") as config_file:
return toml.load(config_file)
except FileNotFoundError:
sys.exit(f'[read_config] Config file "{self.config_file}" not found.')
def get_config_value(self, section, key):
"""
get value from config file
"""
config = self.read_config()
value = config.get(section).get(key)
if value is None or (isinstance(value, str) and not value.strip()):
raise ConfigError(
f"[get_config_value] Value for '{key}' in section '{section}' is missing or blank."
)
return value