Files
ALL-teach_sys/frontend_视觉设计/update_interview_questions.py

86 lines
3.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import json
import re
import os
# 读取视觉设计岗位简历.json文件
json_file = '网页未导入数据/视觉设计产业/视觉设计岗位简历.json'
mock_file = 'src/mocks/resumeInterviewMock.js'
print("正在读取面试题数据...")
with open(json_file, 'r', encoding='utf-8') as f:
positions_data = json.load(f)
# 创建岗位名称到面试题的映射
interview_questions_map = {}
for position in positions_data:
position_name = position.get("岗位名称", "")
interview_content = position.get("面试题内容", "")
if position_name and interview_content:
interview_questions_map[position_name] = interview_content
print(f"找到 {position_name} 的面试题")
print(f"\n总共找到 {len(interview_questions_map)} 个岗位的面试题")
# 读取resumeInterviewMock.js文件
print("\n正在读取 resumeInterviewMock.js...")
with open(mock_file, 'r', encoding='utf-8') as f:
content = f.read()
# 创建备份
backup_file = f"{mock_file}.backup_interview_{os.popen('date +%Y%m%d_%H%M%S').read().strip()}"
with open(backup_file, 'w', encoding='utf-8') as f:
f.write(content)
print(f"已创建备份文件: {backup_file}")
# 更新面试题
update_count = 0
not_found = []
for position_name, interview_content in interview_questions_map.items():
# 转义特殊字符
escaped_name = re.escape(position_name)
# 准备替换的面试题内容,需要转义反斜杠和反引号
escaped_content = interview_content.replace('\\', '\\\\').replace('`', '\\`')
# 首先尝试查找并更新已存在的interviewQuestions字段
# 注意:"position"字段也可能带引号
pattern_existing = rf'''("position":\s*["']{escaped_name}["'].*?)"interviewQuestions":\s*`[^`]*?`'''
if re.search(pattern_existing, content, re.DOTALL):
# 如果已存在interviewQuestions字段替换它
replacement = rf'\1"interviewQuestions": `{escaped_content}`'
content = re.sub(pattern_existing, replacement, content, flags=re.DOTALL)
update_count += 1
print(f"✓ 已更新 {position_name} 的面试题(替换已有)")
else:
# 如果不存在尝试在content字段后添加
pattern_add = rf'''(position:\s*["']{escaped_name}["'].*?content:\s*{{[^}}]*?}})(\s*}})'''
if re.search(pattern_add, content, re.DOTALL):
# 在content字段后添加interviewQuestions
replacement = rf'\1,\n interviewQuestions: `{escaped_content}`\2'
content = re.sub(pattern_add, replacement, content, flags=re.DOTALL)
update_count += 1
print(f"✓ 已更新 {position_name} 的面试题(新增字段)")
else:
not_found.append(position_name)
# 写回文件
print(f"\n正在写入更新后的内容...")
with open(mock_file, 'w', encoding='utf-8') as f:
f.write(content)
print(f"\n更新完成!")
print(f"成功更新: {update_count} 个岗位")
if not_found:
print(f"未找到以下岗位: {', '.join(not_found)}")
# 验证语法
print("\n验证文件语法...")
result = os.popen(f'node -c {mock_file} 2>&1').read()
if result:
print(f"❌ 语法错误: {result}")
else:
print("✓ 语法验证通过")