#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json from datetime import datetime def create_backup(file_path): """创建备份文件""" timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") backup_path = f"{file_path}.backup_direct_{timestamp}" with open(file_path, 'r', encoding='utf-8') as f: content = f.read() with open(backup_path, 'w', encoding='utf-8') as f: f.write(content) print(f"已创建备份: {backup_path}") return backup_path def get_chemical_questions(): """获取化工行业的面试题数据""" # 加载化工数据 with open('网页未导入数据/化工产业/化工岗位简历.json', 'r', encoding='utf-8') as f: data = json.load(f) # 创建岗位群到面试题的映射 questions_map = {} for item in data: group = item.get('简历岗位群', '') if group and group not in questions_map: # 为每个岗位群创建示例面试题 questions_map[group] = [ { "id": "q1", "question": f"请介绍一下你对{group}岗位的理解", "answer": "需要掌握相关专业知识和技能,确保工作安全高效。" }, { "id": "q2", "question": f"在{group}工作中,你认为最重要的是什么?", "answer": "安全第一,质量优先,持续改进。" }, { "id": "q3", "question": "遇到紧急情况你会如何处理?", "answer": "立即启动应急预案,确保人员安全,及时上报并处理。" } ] # 特定岗位群的专业问题 if "化工安全" in questions_map: questions_map["化工安全"] = [ { "id": "q1", "question": "发现储罐泄漏应如何处理?", "answer": "立即隔离危险区,启动应急预案,关闭泄漏源,通报相关部门。" }, { "id": "q2", "question": "如何进行危险源辨识?", "answer": "通过HAZOP分析、JSA作业安全分析等方法系统识别风险。" }, { "id": "q3", "question": "化工安全管理的核心是什么?", "answer": "预防为主,综合治理,从源头控制风险。" } ] if "精细化工" in questions_map: questions_map["精细化工"] = [ { "id": "q1", "question": "精细化工与传统化工的区别?", "answer": "精细化工产品附加值高、批量小、品种多、技术含量高。" }, { "id": "q2", "question": "如何控制产品质量?", "answer": "严格控制原料质量、工艺参数、过程监控和产品检验。" } ] return questions_map def update_mock_file(): """直接更新mock文件的subQuestions""" mock_file = 'src/mocks/resumeInterviewMock.js' # 创建备份 backup_path = create_backup(mock_file) # 获取面试题数据 questions_map = get_chemical_questions() # 读取文件内容 with open(mock_file, 'r', encoding='utf-8') as f: content = f.read() # 对每个岗位群进行替换 for group_name, questions in questions_map.items(): # 查找该岗位群的questions部分 # 模式:从 "name": "岗位群名" 到 "subQuestions": [] import re pattern = rf'("name":\s*"{re.escape(group_name)}".*?"subQuestions":\s*)\[\]' # 构建替换内容 questions_json = json.dumps(questions, ensure_ascii=False, indent=10) # 调整缩进 lines = questions_json.split('\n') formatted_lines = [] for line in lines[1:-1]: # 跳过首尾的[] formatted_lines.append(' ' + line) formatted_json = '[\n' + '\n'.join(formatted_lines) + '\n ]' # 执行替换 replacement = rf'\1{formatted_json}' new_content = re.sub(pattern, replacement, content, count=1, flags=re.DOTALL) if new_content != content: content = new_content print(f"✓ 已更新 {group_name} 的面试题") else: print(f"⚠ 未找到 {group_name} 的空subQuestions") # 保存文件 with open(mock_file, 'w', encoding='utf-8') as f: f.write(content) # 验证语法 import subprocess result = subprocess.run(['node', '-c', mock_file], capture_output=True, text=True) if result.returncode == 0: print("\n✓ 语法检查通过") print(f"✓ 成功更新了 {len(questions_map)} 个岗位群的面试题") return True else: print("\n✗ 语法检查失败:") print(result.stderr[:500]) # 恢复备份 with open(backup_path, 'r', encoding='utf-8') as f: backup_content = f.read() with open(mock_file, 'w', encoding='utf-8') as f: f.write(backup_content) print("已恢复备份文件") return False def main(): """主函数""" print("开始直接更新面试题数据...\n") if update_mock_file(): print("\n✅ 面试题更新成功完成!") else: print("\n❌ 面试题更新失败") if __name__ == "__main__": main()