config_loader.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 配置加载器模块
  5. 负责加载和验证配置文件
  6. """
  7. import yaml
  8. import os
  9. from pathlib import Path
  10. from typing import Dict, Any, List
  11. class ConfigLoader:
  12. def __init__(self, config_path: str = "config.yaml"):
  13. self.config_path = Path(config_path)
  14. self.config = None
  15. self._load_config()
  16. def _load_config(self):
  17. """加载配置文件"""
  18. if not self.config_path.exists():
  19. raise FileNotFoundError(f"配置文件不存在: {self.config_path}")
  20. with open(self.config_path, 'r', encoding='utf-8') as f:
  21. self.config = yaml.safe_load(f)
  22. def get_project_info(self) -> Dict[str, Any]:
  23. """获取项目信息"""
  24. return self.config.get('project', {})
  25. def get_repository_info(self) -> Dict[str, Any]:
  26. """获取仓库信息"""
  27. return self.config.get('repository', {})
  28. def get_categories(self) -> Dict[str, Any]:
  29. """获取分类配置"""
  30. return self.config.get('categories', {})
  31. def get_generation_config(self) -> Dict[str, Any]:
  32. """获取生成配置"""
  33. return self.config.get('generation', {})
  34. def get_sphinx_config(self) -> Dict[str, Any]:
  35. """获取Sphinx配置"""
  36. return self.config.get('sphinx', {})
  37. def validate_config(self) -> bool:
  38. """验证配置文件"""
  39. required_sections = ['project', 'repository', 'categories', 'generation', 'sphinx']
  40. for section in required_sections:
  41. if section not in self.config:
  42. raise ValueError(f"配置文件缺少必需的部分: {section}")
  43. return True