MCPcopy Create free account
hub / github.com/aaPanel/BaoTa / auto_fill_missing_fields

Method auto_fill_missing_fields

class/theme_config.py:334–378  ·  view source on GitHub ↗

自动补充配置中缺失的必要字段

(self, config)

Source from the content-addressed store, hash-verified

332 return self.return_message(False, f'字段检测异常: {str(e)}')
333
334 def auto_fill_missing_fields(self, config):
335 """自动补充配置中缺失的必要字段"""
336 try:
337 if not isinstance(config, dict):
338 return self.return_message(False, '配置必须是字典类型')
339
340 import copy
341 filled_config = copy.deepcopy(config)
342 filled_count = 0
343 filled_fields = []
344
345 def _fill_nested_fields(default_dict, current_dict, path_prefix=''):
346 """递归填充嵌套字段"""
347 nonlocal filled_count
348
349 for key, default_value in default_dict.items():
350 current_path = f"{path_prefix}.{key}" if path_prefix else key
351
352 if key not in current_dict:
353 # 缺失字段,进行补充
354 current_dict[key] = copy.deepcopy(default_value)
355 filled_count += 1
356 filled_fields.append({
357 'path': current_path,
358 'value': default_value,
359 'type': type(default_value).__name__
360 })
361 elif isinstance(default_value, dict):
362 # 确保当前字段也是字典类型
363 if not isinstance(current_dict.get(key), dict):
364 current_dict[key] = {}
365 _fill_nested_fields(default_value, current_dict[key], current_path)
366
367 _fill_nested_fields(self.DEFAULT_CONFIG, filled_config)
368
369 message = f'成功补充 {filled_count} 个缺失字段' if filled_count > 0 else '无需补充字段'
370
371 return self.return_message(True, message, {
372 'config': filled_config,
373 'filled_fields': filled_fields,
374 'filled_count': filled_count
375 })
376
377 except Exception as e:
378 return self.return_message(False, f'字段补充异常: {str(e)}')
379
380 def validate_config(self, config):
381 """验证配置数据"""

Callers 1

save_configMethod · 0.95

Calls 1

return_messageMethod · 0.95

Tested by

no test coverage detected