-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathconfiguration.py
More file actions
64 lines (56 loc) · 2.91 KB
/
configuration.py
File metadata and controls
64 lines (56 loc) · 2.91 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging.config# 导入logging.config模块,用于配置日志系统
import os # 导入os模块,用于操作文件和目录
import shutil # 导入shutil模块,用于复制文件
import yaml # 导入yaml模块,用于处理YAML配置文件
class Config(object):
def __init__(self) -> None:
self.reload()# 在类的实例被创建后,自动重新加载配置
def _load_config(self) -> dict:
pwd = os.path.dirname(os.path.abspath(__file__))
# 获取当前文件的目录路径
try:
# 尝试打开并加载config.yaml文件
with open(f"{pwd}/config.yaml", "rb") as fp:
yconfig = yaml.safe_load(fp)
except FileNotFoundError:
# 如果config.yaml文件不存在,则复制模板文件,并加载它
shutil.copyfile(f"{pwd}/config.yaml.template", f"{pwd}/config.yaml")
with open(f"{pwd}/config.yaml", "rb") as fp:
yconfig = yaml.safe_load(fp)
return yconfig# 返回加载的配置字典
def reload(self) -> None:
yconfig = self._load_config()# 重新加载配置文件# 调用_load_config方法获取配置字典
logging.config.dictConfig(yconfig["logging"])
# 从配置文件中获取并设置各个部分的配置
self.GROUPS = yconfig["groups"]["enable"]
self.NEWS = yconfig["news"]["receivers"]
self.REPORT_REMINDERS = yconfig["report_reminder"]["receivers"]
# 获取可选配置,如果不存在则返回空字典
self.CHATGPT = yconfig.get("chatgpt", {})
self.CHATGPTt = yconfig.get("chatgptt",{})
self.MOONSHOT = yconfig.get("moonshot",{})
self.QWEN = yconfig.get("qwen",{})
self.TIGERBOT = yconfig.get("tigerbot", {})
self.XINGHUO_WEB = yconfig.get("xinghuo_web", {})
self.CHATGLM = yconfig.get("chatglm", {})
self.BardAssistant = yconfig.get("bard", {})
self.ZHIPU = yconfig.get("zhipu", {})
self.DEEPSEEK = yconfig.get("deepseek", {})
#get 方法用于从配置字典中读取可选的配置项,如果这些项不存在,它将返回一个空字典而不是引发错误。
def update_config(self, key: str, value) -> None:
pwd = os.path.dirname(os.path.abspath(__file__))
try:
with open(f"{pwd}/config.yaml", "rb") as fp:
yconfig = yaml.safe_load(fp)
keys = key.split('.')
sub_config = yconfig
for k in keys[:-1]:
sub_config = sub_config.setdefault(k, {})
sub_config[keys[-1]] = value
with open(f"{pwd}/config.yaml", "w", encoding='utf-8') as fp:
yaml.safe_dump(yconfig, fp, default_flow_style=False, sort_keys=False, indent=2, allow_unicode=True)
self.reload()
except Exception as e:
print(f"Error updating configuration: {e}")