#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import os import datetime import shutil import re def read_modified_resumes(): """读取所有修改版简历文件""" modified_resumes = {} folder_path = "网页未导入数据/能源产业/能源修改版简历" 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_safely(): """安全地更新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部分 templates_match = re.search(r'const resumeTemplates = ({[\s\S]*?});', content) if not templates_match: print("❌ 未找到resumeTemplates") return templates_str = templates_match.group(1) # 安全地解析为Python对象 # 由于可能有已存在的modified字段,需要先清理 try: # 使用正则表达式提取各个岗位群 templates = {} # 匹配每个岗位群 group_pattern = r'"([^"]+)":\s*\[([\s\S]*?)\](?=,\s*"|\s*})' for match in re.finditer(group_pattern, templates_str): group_name = match.group(1) group_content = match.group(2) # 解析该岗位群的模板 templates[group_name] = [] # 匹配每个岗位模板 position_pattern = r'{\s*"position":\s*"([^"]+)"[^}]*?"content":\s*{[^}]*?"original":\s*"([^"\\]*(?:\\.[^"\\]*)*)"[^}]*?}[^}]*?}' for pos_match in re.finditer(position_pattern, group_content): position_name = pos_match.group(1) original_content = pos_match.group(2) template_obj = { "position": position_name, "content": { "original": original_content, "modified": "" } } # 查找修改版简历 if position_name in modified_resumes: template_obj["content"]["modified"] = modified_resumes[position_name] print(f" ✅ 匹配到: {position_name}") elif position_name.replace('PACK', 'pack') in modified_resumes: # 处理大小写问题 template_obj["content"]["modified"] = modified_resumes[position_name.replace('PACK', 'pack')] print(f" ✅ 匹配到: {position_name} (大小写转换)") elif position_name.replace('pack', 'PACK') in modified_resumes: template_obj["content"]["modified"] = modified_resumes[position_name.replace('pack', 'PACK')] print(f" ✅ 匹配到: {position_name} (大小写转换)") templates[group_name].append(template_obj) # 重新生成resumeTemplates new_templates_str = json.dumps(templates, ensure_ascii=False, indent=2) # 替换原来的resumeTemplates new_content = content[:templates_match.start(1)] + new_templates_str + content[templates_match.end(1):] # 写回文件 with open(mock_file, 'w', encoding='utf-8') as f: f.write(new_content) print("\n✅ 成功更新Mock文件") except Exception as e: print(f"❌ 更新失败: {e}") # 恢复备份 shutil.copy(backup_path, mock_file) print("已恢复备份") if __name__ == "__main__": update_mock_safely()