#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import os import datetime import shutil def read_modified_resumes(): """读取所有修改版简历文件""" modified_resumes = {} folder_path = "网页未导入数据/能源产业/能源修改版简历" # 获取所有.md文件 for filename in os.listdir(folder_path): if filename.endswith('.md'): position_name = filename.replace('.md', '') file_path = os.path.join(folder_path, filename) with open(file_path, 'r', encoding='utf-8') as f: content = f.read() modified_resumes[position_name] = content return modified_resumes def update_mock_file_with_modified(): """更新Mock文件,添加修改版简历内容""" mock_file = "src/mocks/resumeInterviewMock.js" # 备份文件 backup_path = f"{mock_file}.backup_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}" shutil.copy(mock_file, backup_path) print(f"✅ 已备份文件到:{backup_path}") # 读取修改版简历 modified_resumes = read_modified_resumes() print(f"📚 找到 {len(modified_resumes)} 个修改版简历文件") # 读取当前mock文件 with open(mock_file, 'r', encoding='utf-8') as f: content = f.read() # 更新resumeTemplates中对应岗位的modified字段 import re updated_count = 0 for position_name, resume_content in modified_resumes.items(): print(f" 处理岗位: {position_name}") # 转义特殊字符用于正则表达式 escaped_position = re.escape(position_name) # 查找该岗位在resumeTemplates中的位置 # 模式:查找 "position": "岗位名称" 后的 content 对象 pattern = rf'("position":\s*"{escaped_position}"[^{{]*?"content":\s*{{[^}}]*?"original":[^,]*?,)(\s*"modified":\s*)"[^"]*"' # 准备替换的内容(转义JSON字符串) escaped_content = json.dumps(resume_content, ensure_ascii=False) # 执行替换 new_content, count = re.subn( pattern, rf'\1\2{escaped_content}', content ) if count > 0: content = new_content updated_count += 1 print(f" ✅ 已更新") else: # 如果第一种模式失败,尝试另一种模式(没有modified字段的情况) pattern2 = rf'("position":\s*"{escaped_position}"[^{{]*?"content":\s*{{[^}}]*?"original":[^}}]*?)(}})' # 添加modified字段 replacement = rf'\1,\n "modified": {escaped_content}\2' new_content, count = re.subn(pattern2, replacement, content) if count > 0: content = new_content updated_count += 1 print(f" ✅ 已添加modified字段") else: print(f" ⚠️ 未找到匹配位置") # 写回文件 with open(mock_file, 'w', encoding='utf-8') as f: f.write(content) print(f"\n✅ 成功更新 {updated_count}/{len(modified_resumes)} 个岗位的修改版简历") # 验证更新 verify_updates(mock_file, modified_resumes.keys()) def verify_updates(mock_file, position_names): """验证更新是否成功""" with open(mock_file, 'r', encoding='utf-8') as f: content = f.read() print("\n📊 验证结果:") for position_name in position_names: # 检查是否包含该岗位的modified内容 pattern = f'"position": "{position_name}"' if pattern in content: # 进一步检查是否有modified字段 # 查找该岗位后面的modified字段 start_idx = content.find(pattern) if start_idx > 0: # 查找该岗位content对象的范围(简化检查) end_idx = content.find('"position":', start_idx + len(pattern)) if end_idx < 0: end_idx = start_idx + 2000 # 如果是最后一个,取后面2000字符 section = content[start_idx:end_idx] if '"modified":' in section and position_name in section: print(f" ✅ {position_name}: 已包含修改版") else: print(f" ❌ {position_name}: 未包含修改版") else: print(f" ⚠️ {position_name}: 未找到该岗位") if __name__ == "__main__": update_mock_file_with_modified()