|
| 1 | +import os |
| 2 | +import yaml |
| 3 | +from typing import Dict, List, Optional |
| 4 | + |
| 5 | +class ConfigManager: |
| 6 | + """配置管理器:负责加载和管理配置""" |
| 7 | + |
| 8 | + def __init__(self, config_file: str = "config.yaml"): |
| 9 | + self.config_file = config_file |
| 10 | + self.config = self._load_config() |
| 11 | + self._init_directories() |
| 12 | + |
| 13 | + def _load_config(self) -> Dict: |
| 14 | + """加载配置文件""" |
| 15 | + try: |
| 16 | + with open(self.config_file, 'r', encoding='utf-8') as f: |
| 17 | + return yaml.safe_load(f) |
| 18 | + except Exception as e: |
| 19 | + print(f"加载配置文件失败: {str(e)}") |
| 20 | + return {} |
| 21 | + |
| 22 | + def _init_directories(self): |
| 23 | + """初始化所有必要的目录""" |
| 24 | + # 创建输出基础目录 |
| 25 | + os.makedirs(self.output_base_dir, exist_ok=True) |
| 26 | + # 创建报告目录 |
| 27 | + os.makedirs(self.reports_dir, exist_ok=True) |
| 28 | + # 创建树文件目录 |
| 29 | + os.makedirs(self.trees_dir, exist_ok=True) |
| 30 | + # 创建临时文件目录 |
| 31 | + os.makedirs(self.temp_dir, exist_ok=True) |
| 32 | + |
| 33 | + @property |
| 34 | + def tree_max_depth(self) -> int: |
| 35 | + """获取目录树最大深度""" |
| 36 | + return self.config.get('tree', {}).get('max_depth', 4) |
| 37 | + |
| 38 | + @property |
| 39 | + def max_file_size(self) -> int: |
| 40 | + """获取最大文件大小""" |
| 41 | + return self.config.get('file', {}).get('max_size', 10 * 1024 * 1024) |
| 42 | + |
| 43 | + @property |
| 44 | + def file_encoding(self) -> str: |
| 45 | + """获取文件编码""" |
| 46 | + return self.config.get('file', {}).get('encoding', 'utf-8') |
| 47 | + |
| 48 | + # 输入路径相关 |
| 49 | + @property |
| 50 | + def input_base_dir(self) -> str: |
| 51 | + """获取输入基础目录""" |
| 52 | + return self.config.get('paths', {}).get('input', {}).get('base_dir', os.getcwd()) |
| 53 | + |
| 54 | + @property |
| 55 | + def github_repo(self) -> str: |
| 56 | + """获取默认GitHub仓库地址""" |
| 57 | + return self.config.get('paths', {}).get('input', {}).get('github', '') |
| 58 | + |
| 59 | + @property |
| 60 | + def gitlab_repo(self) -> str: |
| 61 | + """获取默认GitLab仓库地址""" |
| 62 | + return self.config.get('paths', {}).get('input', {}).get('gitlab', '') |
| 63 | + |
| 64 | + # 输出路径相关 |
| 65 | + @property |
| 66 | + def output_base_dir(self) -> str: |
| 67 | + """获取输出基础目录""" |
| 68 | + return self.config.get('paths', {}).get('output', {}).get('base_dir', 'output') |
| 69 | + |
| 70 | + @property |
| 71 | + def reports_dir(self) -> str: |
| 72 | + """获取报告目录""" |
| 73 | + reports = self.config.get('paths', {}).get('output', {}).get('reports', 'reports') |
| 74 | + return os.path.join(self.output_base_dir, reports) |
| 75 | + |
| 76 | + @property |
| 77 | + def trees_dir(self) -> str: |
| 78 | + """获取树文件目录""" |
| 79 | + trees = self.config.get('paths', {}).get('output', {}).get('trees', 'trees') |
| 80 | + return os.path.join(self.output_base_dir, trees) |
| 81 | + |
| 82 | + @property |
| 83 | + def temp_dir(self) -> str: |
| 84 | + """获取临时文件目录""" |
| 85 | + temp = self.config.get('paths', {}).get('output', {}).get('temp', 'temp') |
| 86 | + return os.path.join(self.output_base_dir, temp) |
| 87 | + |
| 88 | + @property |
| 89 | + def supported_formats(self) -> List[str]: |
| 90 | + """获取支持的输出格式""" |
| 91 | + return self.config.get('output', {}).get('formats', ['md']) |
| 92 | + |
| 93 | + @property |
| 94 | + def default_format(self) -> str: |
| 95 | + """获取默认输出格式""" |
| 96 | + return self.config.get('output', {}).get('default_format', 'md') |
| 97 | + |
| 98 | + def get_output_file(self, format_type: str) -> str: |
| 99 | + """获取指定格式的输出文件名""" |
| 100 | + files = self.config.get('output', {}).get('files', {}) |
| 101 | + return files.get(format_type, f'analysis_result.{format_type}') |
| 102 | + |
| 103 | + def get_output_path(self, filename: str, output_type: str = 'reports') -> str: |
| 104 | + """ |
| 105 | + 获取输出文件的完整路径 |
| 106 | + :param filename: 文件名 |
| 107 | + :param output_type: 输出类型(reports/trees/temp) |
| 108 | + :return: 完整路径 |
| 109 | + """ |
| 110 | + if output_type == 'reports': |
| 111 | + base_dir = self.reports_dir |
| 112 | + elif output_type == 'trees': |
| 113 | + base_dir = self.trees_dir |
| 114 | + elif output_type == 'temp': |
| 115 | + base_dir = self.temp_dir |
| 116 | + else: |
| 117 | + base_dir = self.output_base_dir |
| 118 | + |
| 119 | + return os.path.join(base_dir, filename) |
| 120 | + |
| 121 | + @property |
| 122 | + def content_preview_length(self) -> int: |
| 123 | + """获取内容预览长度""" |
| 124 | + return self.config.get('content', {}).get('preview_length', 1000) |
| 125 | + |
| 126 | + @property |
| 127 | + def supported_sources(self) -> List[str]: |
| 128 | + """获取支持的输入源类型""" |
| 129 | + return self.config.get('input', {}).get('supported_sources', ['local']) |
| 130 | + |
| 131 | + @property |
| 132 | + def default_source(self) -> str: |
| 133 | + """获取默认输入源类型""" |
| 134 | + return self.config.get('input', {}).get('default_source', 'local') |
| 135 | + |
| 136 | + def validate_format(self, format_type: str) -> bool: |
| 137 | + """验证输出格式是否支持""" |
| 138 | + return format_type in self.supported_formats |
| 139 | + |
| 140 | + def validate_source(self, source_type: str) -> bool: |
| 141 | + """验证输入源类型是否支持""" |
| 142 | + return source_type in self.supported_sources |
0 commit comments