-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_questions.py
252 lines (217 loc) · 7.43 KB
/
import_questions.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import copy
import json
import math
import sys
import uuid
import csv
QUESTIONS_BASE_PATH = "kysymykset"
if len(sys.argv) < 2:
questions_version = input("Enter new version number: ")
else:
questions_version = sys.argv[1]
question_page = {
"children": [],
"type": "page"
}
single_choise_grid_description = {
"description": {
"fi": "1: Ei lainkaan, 2: Huonosti, " +
"3: Melko huonosti, 4: Jossain määrin, 5: Melko hyvin, " +
"6: Hyvin, 7: Erittäin hyvin",
"en": "1: Not at all, 2: Poorly, " +
"3: Somewhat poorly, 4: To some extent, 5: Pretty well, " +
"6: Well, 7: Very well"
},
"idname": "section-separator",
"title": {
"fi": "Arvioi, miten hyvin seuraavat väitteet pitävät paikkansa työssäsi.",
"en": "Rate how well the following statements are reflected in your job."
},
"type": "question"
}
single_choise_grid = {
"idname": "single-choice-grid",
"title": {
"fi": "",
"en": ""
},
"children": [
{
"required": True,
"type": "single"
}
],
"choices": [
{
"label": "1"
},
{
"label": "2"
},
{
"label": "3"
},
{
"label": "4"
},
{
"label": "5"
},
{
"label": "6"
},
{
"label": "7"
}
]
}
text_response = {
"idname": "text-response",
"title": {
"fi": ""
},
"children": [
{
"required": False,
"type": "string"
}
]
}
master_json_data = {
"title": {
"fi": "IMQ Kysymyspatteristo {}".format(questions_version),
"en": "IMQ Questions {}".format(questions_version)
},
"version": questions_version,
"form": [],
"languages": [
{
"code": "fi",
"isDefault": True,
"name": "Finnish"
},
{
"code": "en",
"isDefault": False,
"name": "English"
}
]
}
master_json_data_flattened = {
"title": {
"fi": "IMQ Kysymyspatteristo {}".format(questions_version),
"en": "IMQ Questions {}".format(questions_version)
}, "version": questions_version,
"form": [{
"children": [],
"type": "page"
}],
"languages": [
{
"code": "fi",
"isDefault": True,
"name": "Finnish"
},
{
"code": "en",
"isDefault": False,
"name": "English"
}
]
}
reverse_question_ids = [
line.rstrip("\n") for line in open("./{0}/{1}/reverse_question_ids_{1}.txt"
.format(QUESTIONS_BASE_PATH,
questions_version))]
def add_spss_formula_info(question):
with open(("./{0}/{1}/spss_formulas_{1}.json")
.format(QUESTIONS_BASE_PATH, questions_version)
) as spss_formulas_file:
spss_formulas_json_data = json.load(spss_formulas_file)
for formulas_data in spss_formulas_json_data:
if "children" in formulas_data:
if question["id"] in formulas_data["children"]:
append_formulas_to_question(question, formulas_data)
def append_formulas_to_question(question, formulas_data):
if "SPSS_FORMULAS" not in question:
question["SPSS_FORMULAS"] = []
for formula_name in formulas_data["formulas"]:
formula_entry_found = False
for question_formula_entry in question["SPSS_FORMULAS"]:
if question_formula_entry["name"] == formula_name:
question_formula_entry["ids"].append(formulas_data["id"])
formula_entry_found = True
break
if not formula_entry_found:
question["SPSS_FORMULAS"].append(
{"name": formula_name, "ids": [formulas_data["id"]]})
def add_spss_sum_formula_info():
with open(("./{0}/{1}/spss_sum_formulas_{1}.json")
.format(QUESTIONS_BASE_PATH, questions_version)
) as spss_sum_formulas_file:
spss_sum_formulas_json_data = json.load(
spss_sum_formulas_file)
master_json_data["SPSS_SUM_FORMULAS"] = spss_sum_formulas_json_data
master_json_data_flattened[
"SPSS_SUM_FORMULAS"] = spss_sum_formulas_json_data
def add_spss_formulas_order_info():
with open(("./{0}/{1}/spss_formulas_order_{1}.json")
.format(QUESTIONS_BASE_PATH, questions_version)
) as spss_formulas_order_file:
spss_formulas_order_json_data = json.load(
spss_formulas_order_file)
master_json_data["SPSS_FORMULAS_ORDER"] = spss_formulas_order_json_data
master_json_data_flattened[
"SPSS_FORMULAS_ORDER"] = spss_formulas_order_json_data
with open("./{0}/{1}/questions_{1}.csv"
.format(QUESTIONS_BASE_PATH, questions_version)) as questions_file:
csv_file = csv.reader(questions_file, delimiter=';')
for index, row in enumerate(csv_file):
question_id = row[0]
title_fi = row[1]
title_en = row[2]
if "avoin" in question_id:
question = copy.deepcopy(text_response)
else:
question = copy.deepcopy(single_choise_grid)
question["title"]["fi"] = title_fi
question["title"]["en"] = title_en
question["id"] = question_id
if question_id in reverse_question_ids:
question["REVERSE_VALUE"] = True
add_spss_formula_info(question)
place = math.floor((index) / 9)
if len(master_json_data["form"]) <= place:
question_page_copy = copy.deepcopy(question_page)
master_json_data["form"].append(question_page_copy)
single_choise_grid_description_copy = copy.deepcopy(
single_choise_grid_description)
single_choise_grid_description_copy["id"] = str(uuid.uuid1())
master_json_data["form"][place]["children"].append(
single_choise_grid_description_copy)
master_json_data["form"][place]["children"].append(question)
master_json_data_flattened["form"][0]["children"].append(question)
# http://stackoverflow.com/q/845058
print("Added {0} questions".format(index + 1))
# Uncomment for debugging.
# print(json.dumps(master_json_data["form"][0]["children"],
# indent=4, sort_keys=True))
# Add survey frame
with open("./{0}/{1}/master_frame_{1}.json"
.format(QUESTIONS_BASE_PATH, questions_version)
) as master_frame_json_file:
master_frame_json_data = json.load(master_frame_json_file)
# Last question go to the end of the survey.
PREPENDING_QUESTIONS_IN_FRAME = len(master_frame_json_data) - 1
master_json_data["form"] = master_frame_json_data[
:PREPENDING_QUESTIONS_IN_FRAME] + master_json_data[
"form"] + master_frame_json_data[PREPENDING_QUESTIONS_IN_FRAME:]
add_spss_sum_formula_info()
add_spss_formulas_order_info()
with open("./{0}/{1}/master_{1}.json"
.format(QUESTIONS_BASE_PATH, questions_version), "w") as outfile:
json.dump(master_json_data, outfile, indent=2, ensure_ascii=False)
with open("./{0}/{1}/master_flattened_{1}.json"
.format(QUESTIONS_BASE_PATH, questions_version), "w") as outfile:
json.dump(master_json_data_flattened, outfile, indent=2,
ensure_ascii=False)