-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfix-order.py
80 lines (57 loc) · 1.83 KB
/
fix-order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import gen
import sys
import argparse
import os
import json
def main():
parser = argparse.ArgumentParser(description="Rebuilds the Order.txt file fixing each questions order list.")
parser.add_argument('order', help='The original (screwed-up) Order.txt file.')
parser.add_argument('master', help='The original Master.txt file.')
parser.add_argument('src', help='The folder with all the Text-*.tex files.')
args = parser.parse_args()
fix(args)
def fix(args):
gen.parser(args)
questions = gen.questions_by_id
with open(args.order) as fp:
order = json.load(fp)
for f in os.listdir(args.src):
if f.startswith('Test-'):
try:
test_id = f[5:-4]
fix_test(args.src, f, test_id, questions, order)
except Exception as e:
sys.stderr.write("ERROR: %s \t (%s)\n" % (test_id, repr(e)))
s = json.dumps(order)
print(s)
def fix_test(src, f, test_id, questions, order):
test_order = order[test_id]
tex_file = open(os.path.join(src, f))
lines = tex_file.readlines()
test_questions = list(test_order['questions'])
while lines:
l = lines.pop(0).strip()
if l.startswith('\\item'):
question_meta = test_questions.pop(0)
question = questions[question_meta['id']]
order = []
while lines:
li = lines.pop(0).strip()
if li.startswith('\\item'):
li = li.decode('utf8')[5:].strip()
for i, (_, _, opt) in enumerate(question.options):
opt = opt.split('\n')[0].strip()
if li == opt.strip():
order.append(i)
break
elif li.startswith('\\end{enumerate}'):
if len(order) != len(question.options) or len(set(order)) != len(question.options):
print(order)
question_meta['order'] = order
answers = []
for i in question_meta['visual_answers']:
answers.append(order[i - 1])
question_meta['answers'] = answers
break
if __name__ == '__main__':
main()