-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.py
51 lines (35 loc) · 1.06 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
"""Various utile to easier work with config files
"""
from typing import MutableMapping, Any
import toml
import os
def mkdir(path: str):
"""Make dir by path
Args:
path (str): Path to dir
"""
if not os.path.exists(path):
os.mkdir(path)
BOT_FOLDER = os.path.dirname(os.path.realpath(__file__))
CONFIG_FOLDER = os.path.join(BOT_FOLDER, "config")
MODULES_FOLDER = os.path.join(BOT_FOLDER, "modules")
LOGS_FOLDER = os.path.join(BOT_FOLDER, "logs")
mkdir(LOGS_FOLDER)
def config_path(file: str) -> str:
"""Get path to config file by file name
Args:
file (str): Config file name
Returns:
str: Path to config file
"""
return os.path.join(CONFIG_FOLDER, f"{file}.toml")
def load_config(file: str) -> MutableMapping[str, Any]:
"""Load config file by file name
Args:
file (str): Config file name
Returns:
MutableMapping[str, Any]: Config dict
"""
if os.path.exists(config_path(file)):
with open(config_path(file), "r", encoding="utf-8") as f:
return toml.load(f)