mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
🐛 修复config.yaml可能丢失配置的问题
This commit is contained in:
parent
6769c724cb
commit
6bcd1873d0
@ -366,24 +366,42 @@ class ConfigsManager:
|
|||||||
|
|
||||||
if not module or not key:
|
if not module or not key:
|
||||||
raise ValueError("add_plugin_config: module和key不能为为空")
|
raise ValueError("add_plugin_config: module和key不能为为空")
|
||||||
|
|
||||||
|
key_upper = key.upper()
|
||||||
self.add_module.append(f"{module}:{key}".lower())
|
self.add_module.append(f"{module}:{key}".lower())
|
||||||
if module in self._data and (config := self._data[module].configs.get(key)):
|
|
||||||
config.help = help
|
# 检查配置是否已存在
|
||||||
config.arg_parser = arg_parser
|
config_exists = module in self._data and key_upper in self._data[module].configs
|
||||||
config.type = type
|
|
||||||
|
# 如果配置已存在,仅更新元数据,保留原始值
|
||||||
|
if config_exists:
|
||||||
|
config = self._data[module].configs[key_upper]
|
||||||
|
# 只更新元数据
|
||||||
|
if help is not None:
|
||||||
|
config.help = help
|
||||||
|
if arg_parser is not None:
|
||||||
|
config.arg_parser = arg_parser
|
||||||
|
if type is not None:
|
||||||
|
config.type = type
|
||||||
|
# 只在强制覆盖模式下才更新值
|
||||||
if _override:
|
if _override:
|
||||||
config.value = value
|
config.value = value
|
||||||
config.default_value = default_value
|
config.default_value = default_value
|
||||||
else:
|
else:
|
||||||
key = key.upper()
|
# 配置不存在,创建新配置
|
||||||
if not self._data.get(module):
|
if not self._data.get(module):
|
||||||
self._data[module] = ConfigGroup(module=module)
|
self._data[module] = ConfigGroup(module=module)
|
||||||
self._data[module].configs[key] = ConfigModel(
|
self._data[module].configs[key_upper] = ConfigModel(
|
||||||
value=value,
|
value=value,
|
||||||
help=help,
|
help=help,
|
||||||
default_value=default_value,
|
default_value=default_value,
|
||||||
type=type,
|
type=type,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# 同时更新simple_data
|
||||||
|
if module not in self._simple_data:
|
||||||
|
self._simple_data[module] = {}
|
||||||
|
self._simple_data[module][key_upper] = value
|
||||||
|
|
||||||
def set_config(
|
def set_config(
|
||||||
self,
|
self,
|
||||||
@ -486,30 +504,63 @@ class ConfigsManager:
|
|||||||
path: 路径.
|
path: 路径.
|
||||||
save_simple_data: 同时保存至config.yaml.
|
save_simple_data: 同时保存至config.yaml.
|
||||||
"""
|
"""
|
||||||
if save_simple_data:
|
try:
|
||||||
with open(self._simple_file, "w", encoding="utf8") as f:
|
if save_simple_data:
|
||||||
_yaml.dump(self._simple_data, f)
|
# 使用临时文件进行原子写入
|
||||||
path = path or self.file
|
temp_simple_file = self._simple_file.with_suffix(".yaml.tmp")
|
||||||
data = {}
|
with open(temp_simple_file, "w", encoding="utf8") as f:
|
||||||
for module in self._data:
|
_yaml.dump(self._simple_data, f)
|
||||||
data[module] = {}
|
# 原子替换
|
||||||
for config in self._data[module].configs:
|
temp_simple_file.replace(self._simple_file)
|
||||||
value = self._data[module].configs[config].dict()
|
|
||||||
del value["type"]
|
path = path or self.file
|
||||||
del value["arg_parser"]
|
data = {}
|
||||||
data[module][config] = value
|
for module in self._data:
|
||||||
with open(path, "w", encoding="utf8") as f:
|
data[module] = {}
|
||||||
_yaml.dump(data, f)
|
for config in self._data[module].configs:
|
||||||
|
value = self._data[module].configs[config].dict()
|
||||||
|
del value["type"]
|
||||||
|
del value["arg_parser"]
|
||||||
|
data[module][config] = value
|
||||||
|
|
||||||
|
# 使用临时文件进行原子写入
|
||||||
|
temp_file = Path(str(path) + ".tmp")
|
||||||
|
with open(temp_file, "w", encoding="utf8") as f:
|
||||||
|
_yaml.dump(data, f)
|
||||||
|
# 原子替换
|
||||||
|
temp_file.replace(path)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"保存配置文件失败: {str(e)}")
|
||||||
|
|
||||||
def reload(self):
|
def reload(self):
|
||||||
"""重新加载配置文件"""
|
"""重新加载配置文件"""
|
||||||
if self._simple_file.exists():
|
try:
|
||||||
with open(self._simple_file, encoding="utf8") as f:
|
# 先备份当前配置
|
||||||
self._simple_data = _yaml.load(f)
|
backup_data = copy.deepcopy(self._data)
|
||||||
for key in self._simple_data.keys():
|
|
||||||
for k in self._simple_data[key].keys():
|
if self._simple_file.exists():
|
||||||
self._data[key].configs[k].value = self._simple_data[key][k]
|
with open(self._simple_file, encoding="utf8") as f:
|
||||||
self.save()
|
self._simple_data = _yaml.load(f)
|
||||||
|
|
||||||
|
# 检查加载的数据是否为None
|
||||||
|
if self._simple_data is None:
|
||||||
|
logger.error("配置文件为空或格式错误,保留原有配置")
|
||||||
|
self._simple_data = {}
|
||||||
|
|
||||||
|
# 更新配置值
|
||||||
|
for key in self._simple_data.keys():
|
||||||
|
for k in self._simple_data[key].keys():
|
||||||
|
if key in self._data and k in self._data[key].configs:
|
||||||
|
self._data[key].configs[k].value = self._simple_data[key][k]
|
||||||
|
|
||||||
|
# 保存更新后的配置
|
||||||
|
self.save()
|
||||||
|
except ScannerError as e:
|
||||||
|
logger.error(f"配置文件解析失败: {str(e)},保留现有配置")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"重新加载配置失败: {str(e)}")
|
||||||
|
# 发生错误时恢复到备份配置
|
||||||
|
self._data = backup_data
|
||||||
|
|
||||||
def load_data(self):
|
def load_data(self):
|
||||||
"""加载数据
|
"""加载数据
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user