from typing import Dict, Any import json import argparse def load_config(config_path: str) -> Dict[str, Any]: """从JSON配置文件加载配置""" with open(config_path, 'r', encoding='utf-8') as f: return json.load(f) def parse_arguments(): parser = argparse.ArgumentParser(description='报告生成工具') # 添加配置文件参数 parser.add_argument('--config', '-c', dest='config_file', default='config.json', help='配置文件路径,默认为当前目录下的config.json') # 保留一些关键参数作为命令行可覆盖选项 parser.add_argument('--turbine_id', '--id', dest='turbine_id', help='覆盖配置文件中的风机ID') parser.add_argument('--output_dir', '--out', dest='output_dir', help='覆盖配置文件中的输出目录') parser.add_argument('--if_waibu', type=lambda x: x.lower() in ('true', '1', 'yes'), help='是否包含外部作业章节') parser.add_argument('--if_neibu', type=lambda x: x.lower() in ('true', '1', 'yes'), help='是否包含内部作业章节') parser.add_argument('--if_fanglei', type=lambda x: x.lower() in ('true', '1', 'yes'), help='是否包含防雷作业章节') parser.add_argument('--userName', dest='userName', help='报告编制人') parser.add_argument('--baogaoCheck', dest='baogaoCheck', help='报告审核状态') parser.add_argument('--key_words', dest='key_words', help='关键词,英文逗号分隔') parser.add_argument('--data_processor', dest='data_processor', help='数据处理人') parser.add_argument('--jiancha_renyuan', dest='jiancha_renyuan', help='检查人员') parser.add_argument('--check_date', dest='check_date', help='检查日期,格式 YYYY-MM-DD') parser.add_argument('--coverurl', dest='coverurl', help='封面图片 URL') parser.add_argument('--conclusion', dest='conclusion', help='报告总结') parser.add_argument('--choose_template', '--ct', dest='choose_template', help='选择模板,默认为迪特模板,目前支持枚举参数:DT,JF') return parser.parse_args() def merge_configs(default_cfg: Dict[str, Any], file_cfg: Dict[str, Any], cli: argparse.Namespace) -> Dict[str, Dict[str, Any]]: """合并默认配置、文件配置和命令行参数""" merged = { 'json1': {**default_cfg['json1'], **file_cfg.get('json1', {})}, 'json2': {**default_cfg['json2'], **file_cfg.get('json2', {})} } # 高频覆盖项 if cli.turbine_id: merged['json1']['turbine_id'] = cli.turbine_id if cli.output_dir: merged['json2']['shengcheng_dir'] = cli.output_dir for key in ('if_waibu', 'if_neibu', 'if_fanglei', 'userName', 'baogaoCheck', 'key_words', 'data_processor', 'jiancha_renyuan', 'check_date', 'coverurl', 'conclusion' 'choose_template'): val = getattr(cli, key, None) if val is not None: merged['json2'][key] = val return merged def get_default_config() -> Dict[str, Dict[str, Any]]: """获取默认配置""" return { 'json1': { "turbine_id": "183463dbf40d9278549a76b82b175dd9", }, 'json2': { 'shengcheng_dir': "", "dianxing_enum": "TYPICAL", "quexian_enum": "DEFECT", "other_enum": "OTHER", "waibu_enum": "out-work", "neibu_enum": "in-work", "fanglei_enum": "lightning-protection-work", "if_waibu": True, "if_neibu": True, "if_fanglei": True, "userName": "admin", "baogaoCheck": "未审核", 'key_words': '缺,损,裂,脱,污', "data_processor": "未获取", "shigong_fangan": None, 'jiancha_renyuan': '未获取', "check_date": None, "coverurl": None, "conclusion": "未填写总结", "if_docx_fengmian": True, "if_docx_project_overview": True, "if_docx_inspection_method": True, "if_docx_inspection_info": True, "if_docx_chengguo_sub": True, "if_docx_inspection_text": True, "if_docx_inspection_picture": True, "if_docx_defect_picture": True, "if_docx_conclusion": True, "choose_template" : "DT" } }