config_mgmt.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. """
  2. 配置管理工具
  3. 实现配置查询和管理功能。
  4. """
  5. from typing import Dict, Optional
  6. from ..services.data_service import DataService
  7. from ..utils.validators import validate_config_section
  8. from ..utils.errors import MCPError
  9. class ConfigManagementTools:
  10. """配置管理工具类"""
  11. def __init__(self, project_root: str = None):
  12. """
  13. 初始化配置管理工具
  14. Args:
  15. project_root: 项目根目录
  16. """
  17. self.data_service = DataService(project_root)
  18. def get_current_config(self, section: Optional[str] = None) -> Dict:
  19. """
  20. 获取当前系统配置
  21. Args:
  22. section: 配置节 - all/crawler/push/keywords/weights,默认all
  23. Returns:
  24. 配置字典
  25. Example:
  26. >>> tools = ConfigManagementTools()
  27. >>> result = tools.get_current_config(section="crawler")
  28. >>> print(result['crawler']['platforms'])
  29. """
  30. try:
  31. # 参数验证
  32. section = validate_config_section(section)
  33. # 获取配置
  34. config = self.data_service.get_current_config(section=section)
  35. return {
  36. "config": config,
  37. "section": section,
  38. "success": True
  39. }
  40. except MCPError as e:
  41. return {
  42. "success": False,
  43. "error": e.to_dict()
  44. }
  45. except Exception as e:
  46. return {
  47. "success": False,
  48. "error": {
  49. "code": "INTERNAL_ERROR",
  50. "message": str(e)
  51. }
  52. }