135 lines
3.9 KiB
Python
135 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Скрипт для перемешивания вопросов в файле questions.js
|
|
"""
|
|
import re
|
|
import random
|
|
import sys
|
|
|
|
|
|
def find_matching_brace(content, start_pos):
|
|
"""Находит позицию закрывающей скобки для открывающей скобки в start_pos"""
|
|
depth = 0
|
|
in_string = False
|
|
escape_next = False
|
|
string_char = None
|
|
|
|
for i in range(start_pos, len(content)):
|
|
char = content[i]
|
|
|
|
if escape_next:
|
|
escape_next = False
|
|
continue
|
|
|
|
if char == '\\':
|
|
escape_next = True
|
|
continue
|
|
|
|
if (char == '"' or char == "'") and not escape_next:
|
|
if not in_string:
|
|
in_string = True
|
|
string_char = char
|
|
elif char == string_char:
|
|
in_string = False
|
|
string_char = None
|
|
continue
|
|
|
|
if in_string:
|
|
continue
|
|
|
|
if char == '{':
|
|
depth += 1
|
|
elif char == '}':
|
|
depth -= 1
|
|
if depth == 0:
|
|
return i + 1
|
|
|
|
return -1
|
|
|
|
|
|
def shuffle_questions(file_path):
|
|
"""Перемешивает вопросы в файле"""
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
|
|
# Находим начало массива вопросов
|
|
start_match = re.search(r'export const questions = \[', content)
|
|
if not start_match:
|
|
print("Ошибка: не найдено начало массива questions")
|
|
return False
|
|
|
|
start_pos = start_match.end()
|
|
|
|
# Находим все объекты вопросов
|
|
question_blocks = []
|
|
pos = start_pos
|
|
|
|
while pos < len(content):
|
|
# Пропускаем пробелы и переносы строк
|
|
while pos < len(content) and content[pos] in ' \n\r\t':
|
|
pos += 1
|
|
|
|
if pos >= len(content):
|
|
break
|
|
|
|
# Проверяем, не дошли ли до конца массива
|
|
if content[pos] == ']':
|
|
break
|
|
|
|
# Ищем начало объекта вопроса
|
|
if content[pos] != '{':
|
|
# Пропускаем запятую если есть
|
|
if content[pos] == ',':
|
|
pos += 1
|
|
continue
|
|
pos += 1
|
|
continue
|
|
|
|
obj_start = pos
|
|
obj_end = find_matching_brace(content, obj_start)
|
|
|
|
if obj_end == -1:
|
|
break
|
|
|
|
question_block = content[obj_start:obj_end].strip()
|
|
|
|
# Проверяем, что это действительно вопрос (содержит id)
|
|
if 'id:' in question_block:
|
|
question_blocks.append(question_block)
|
|
|
|
pos = obj_end
|
|
|
|
if not question_blocks:
|
|
print("Ошибка: не найдено ни одного вопроса")
|
|
return False
|
|
|
|
# Перемешиваем вопросы
|
|
random.shuffle(question_blocks)
|
|
|
|
# Собираем новый файл
|
|
new_content = content[:start_pos] + '\n'
|
|
|
|
for i, block in enumerate(question_blocks):
|
|
new_content += ' ' + block
|
|
if i < len(question_blocks) - 1:
|
|
new_content += ','
|
|
new_content += '\n'
|
|
|
|
# Добавляем закрывающую скобку массива
|
|
new_content += ']\n'
|
|
|
|
# Сохраняем файл
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
f.write(new_content)
|
|
|
|
print(f"Перемешано {len(question_blocks)} вопросов в файле {file_path}")
|
|
return True
|
|
|
|
|
|
if __name__ == '__main__':
|
|
file_path = 'src/data/questions.js'
|
|
if len(sys.argv) > 1:
|
|
file_path = sys.argv[1]
|
|
|
|
shuffle_questions(file_path)
|
|
|