""" 任务二配置管理模块 复用任务一的ConfigManager,读取任务二专用配置文件 """ import sys from pathlib import Path # 将项目根目录添加到 Python 路径,以便导入 src 模块 project_root = Path(__file__).parent.parent sys.path.insert(0, str(project_root)) from src.config import ConfigManager as BaseConfigManager from typing import List class Task2ConfigManager(BaseConfigManager): """任务二配置管理器,继承自任务一的ConfigManager""" def __init__(self, config_path=None): """ 初始化任务二配置管理器 Args: config_path: 配置文件路径,如果为None则使用任务二默认路径 """ if config_path is None: # 默认路径:项目根目录/config/config_task2.ini config_path = project_root / "config" / "config_task2.ini" super().__init__(config_path) def get_tapd_config(self): """ 获取TAPD配置(任务二版本,不需要reporter) Returns: dict: 包含workspace_id的字典 """ if not self.config.has_section('TAPD'): raise ValueError("配置文件缺少[TAPD]节") if not self.config.has_option('TAPD', 'workspace_id'): raise ValueError("配置文件[TAPD]节缺少workspace_id配置项") workspace_id = self.config.get('TAPD', 'workspace_id').strip() if not workspace_id: raise ValueError("workspace_id配置项不能为空") return { 'workspace_id': workspace_id } def get_smartsheet_config(self): """ 获取智能表格配置(任务二版本,支持多个docid) Returns: dict: 包含docid_list的字典 Raises: ValueError: 配置项缺失时抛出 """ if not self.config.has_section('SmartSheet'): raise ValueError("配置文件缺少[SmartSheet]节") if not self.config.has_option('SmartSheet', 'docid'): raise ValueError("配置文件[SmartSheet]节缺少docid配置项") docid_raw = self.config.get('SmartSheet', 'docid').strip() if not docid_raw: raise ValueError("docid配置项不能为空") # 解析逗号分隔的多个docid docid_list = [d.strip() for d in docid_raw.split(',') if d.strip()] if not docid_list: raise ValueError("docid配置项解析后为空") return { 'docid': docid_list[0], # 保持向后兼容,返回第一个docid 'docid_list': docid_list # 新增:返回所有docid列表 } def get_docid_list(self) -> List[str]: """ 获取所有配置的docid列表 Returns: List[str]: docid列表 """ return self.get_smartsheet_config()['docid_list'] def get_schedule_config(self): """ 获取调度配置(任务二版本,只需要sync_interval) Returns: dict: 包含sync_interval的字典 """ default_sync_interval = 15 if not self.config.has_section('Schedule'): return {'sync_interval': default_sync_interval} if not self.config.has_option('Schedule', 'sync_interval'): return {'sync_interval': default_sync_interval} sync_interval_str = self.config.get('Schedule', 'sync_interval').strip() try: sync_interval = int(sync_interval_str) except ValueError: raise ValueError(f"sync_interval必须为整数,当前值: {sync_interval_str}") if sync_interval <= 0: raise ValueError(f"sync_interval必须为正整数,当前值: {sync_interval}") return {'sync_interval': sync_interval} def get_wework_config(self): """ 获取企业微信推送配置 Returns: dict: 包含agentid和receivers的字典,如果配置不存在则返回None """ if not self.config.has_section('wework'): return None agentid = self.config.get('wework', 'agentid', fallback='').strip() receivers = self.config.get('wework', 'receivers', fallback='').strip() # 如果配置不完整,返回None if not agentid or not receivers: return None return { 'agentid': agentid, 'receivers': receivers } def get_all_config(self): """获取所有配置""" return { 'tapd': self.get_tapd_config(), 'smartsheet': self.get_smartsheet_config(), 'schedule': self.get_schedule_config(), 'wework': self.get_wework_config() } def print_config(self): """打印当前配置信息""" print("\n=== 任务二配置信息 ===") try: tapd_config = self.get_tapd_config() print(f"[TAPD]") print(f" workspace_id: {tapd_config['workspace_id']}") except ValueError as e: print(f"[TAPD] 配置错误: {e}") try: smartsheet_config = self.get_smartsheet_config() print(f"[SmartSheet]") docid_list = smartsheet_config['docid_list'] print(f" docid数量: {len(docid_list)}") for i, docid in enumerate(docid_list, 1): # 显示docid的前20个字符,便于识别 display_id = docid[:20] + "..." if len(docid) > 20 else docid print(f" docid_{i}: {display_id}") except ValueError as e: print(f"[SmartSheet] 配置错误: {e}") try: schedule_config = self.get_schedule_config() print(f"[Schedule]") print(f" sync_interval: {schedule_config['sync_interval']} 分钟") except ValueError as e: print(f"[Schedule] 配置错误: {e}") wework_config = self.get_wework_config() if wework_config: print(f"[wework]") print(f" agentid: {wework_config['agentid']}") print(f" receivers: {wework_config['receivers']}") else: print(f"[wework] 未配置(推送功能将被禁用)") print("======================\n") if __name__ == "__main__": try: config = Task2ConfigManager() config.print_config() except Exception as e: print(f"错误: {e}")