-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.py
More file actions
69 lines (53 loc) · 1.73 KB
/
config.py
File metadata and controls
69 lines (53 loc) · 1.73 KB
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
68
69
# coding: utf-8
from os.path import exists
from logging import getLogger
from pydantic import BaseModel, ValidationError
from yaml import safe_load as load_yaml
from toml import loads as load_toml
import utils as u
l = getLogger(__name__)
class ConfigModel(BaseModel):
'''
主配置
'''
debug: bool = False
'''是否启用调试模式 (显示更多日志)'''
log_file: str | None = None
'''日志文件目录 (为空禁用)'''
token: str
'''Bot token'''
ncm_api: str
'''网易云音乐 api 地址 (结尾不加 /)'''
unm_api: str
'''网易云解灰 api 地址 (结尾不加 /)'''
proxy: str | None = None
'''代理地址'''
class Config:
'''
Config System
'''
config: ConfigModel
def __init__(self):
try:
if exists(u.get_path('config.yaml')):
# load yaml
with open(u.get_path('config.yaml'), 'r', encoding='utf-8') as f:
raw_config: dict = load_yaml(f)
elif exists(u.get_path('config.toml')):
# load toml
with open(u.get_path('config.toml'), 'r', encoding='utf-8') as f:
raw_config: dict = load_toml(f.read())
else:
# both not found
raise FileNotFoundError
# parse config
self.config = ConfigModel.model_validate(raw_config)
except FileNotFoundError:
l.error('Config file config.yaml / config.toml not found!')
exit(1)
except ValidationError as e:
l.error(f'Wrong config file!\n{e}')
exit(1)
except Exception as e:
l.error(f'Error when loading config.toml: {e}')
exit(1)