#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 直接更新resumeInterviewMock.js中的空questions数组 """ import json from datetime import datetime def create_questions_for_group(group_name, questions_list): """为岗位群创建格式化的面试题数据""" if not questions_list: return [] # 限制题目数量,避免太长 max_questions = min(10, len(questions_list)) return [{ "id": f"group_{group_name}_q1", "question": f"{group_name}面试题", "subQuestions": questions_list[:max_questions] }] def update_mock_file(): # 读取面试题数据 with open('interview_questions_data.json', 'r', encoding='utf-8') as f: interview_data = json.load(f) # 读取mock文件 with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: content = f.read() # 备份 backup_name = f'src/mocks/resumeInterviewMock.js.backup_{datetime.now().strftime("%Y%m%d_%H%M%S")}_questions_update' with open(backup_name, 'w', encoding='utf-8') as f: f.write(content) print(f"已创建备份: {backup_name}") # 逐行处理,查找空的questions数组并替换 lines = content.split('\n') updated_lines = [] i = 0 updates = 0 while i < len(lines): line = lines[i] # 检查是否是空的questions数组 if '"questions": []' in line: # 向前查找最近的name字段 group_name = None for j in range(i-1, max(0, i-50), -1): if '"name":' in lines[j]: # 提取岗位群名称 import re match = re.search(r'"name":\s*"([^"]+)"', lines[j]) if match: group_name = match.group(1) break if group_name and group_name in interview_data: questions = create_questions_for_group(group_name, interview_data[group_name]) if questions: # 格式化questions数组 questions_json = json.dumps(questions, ensure_ascii=False, indent=4) # 调整缩进 indent = len(line) - len(line.lstrip()) questions_lines = questions_json.split('\n') formatted_questions = [] for idx, q_line in enumerate(questions_lines): if idx == 0: formatted_questions.append(' ' * indent + '"questions": ' + q_line) else: formatted_questions.append(' ' * indent + q_line) # 替换当前行 updated_lines.extend(formatted_questions[:-1]) # 不包括最后的']' updated_lines.append(' ' * indent + ']') updates += 1 print(f"已更新: {group_name} ({len(interview_data[group_name])} 道题)") i += 1 continue updated_lines.append(line) i += 1 # 写回文件 with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write('\n'.join(updated_lines)) print(f"\n总共更新了 {updates} 个岗位群的面试题") return updates if __name__ == "__main__": update_mock_file()