-
Notifications
You must be signed in to change notification settings - Fork 1
/
converter.py
executable file
·320 lines (240 loc) · 10.2 KB
/
converter.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/env python
'''
Created on 14 May 2015
@author: "Carmelo Velardo"
'''
__author__ = 'carmelo'
from optparse import OptionParser
import pymysql.cursors
import sys
import StringIO
import csv
import re
import fnmatch
import csv
import json
from openpyxl import load_workbook
import unicodedata
def convertOCType(text):
if text == 'radio': return 'radiobuttons'
elif text == 'single-select': return 'select'
elif text == 'multi-select': return 'checkboxes'
elif text == 'checkbox': return 'checkboxes'
else: return text
def combineOptions(opt, vals):
VALS = vals.split(',')
OPTS = opt.split(',')
OptionsElement = {}
for i in range(0, len(VALS)):
#if OPTS[i].strip() != '' and VALS[i].strip() != '':
OptionsElement[VALS[i].strip()] = OPTS[i].strip()
return OptionsElement
row = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA']
def get(sheet, c, r):
return sheet[row[c] + str(r)]
def getVal(sheet, c, r):
if get(sheet, c, r) != None:
return get(sheet, c, r).value
else :
return None
def isOK(data):
return data != None and data != ''
if __name__ == '__main__':
VERSION_BIG = 1
VERSION_SMALL = 1
usage = "usage: %prog [options] arg"
parser = OptionParser(usage)
parser.add_option("-i", "--input", dest="input", help="input CRF XLSX file")
parser.add_option("-o", "--output", dest="output", help="output elaborated html")
parser.add_option("-u", "--user_db", help="user of database. ")
parser.add_option("-p", "--pass_db", help="password of database. ")
parser.add_option("-n", "--name_db", help="the database name. ")
parser.add_option("-s", "--versionsmall", help="small version number. minor changes")
parser.add_option("-b", "--versionbig", help="big version number. major changes")
(options, args) = parser.parse_args(sys.argv)
connection = None
if options.user_db and options.pass_db :
connection = pymysql.connect(host='localhost', user=options.user_db, passwd=options.pass_db, port=8889, db=options.name_db, charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)
if options.versionsmall and options.versionbig :
VERSION_BIG = options.versionbig
VERSION_SMALL = options.versionsmall
input = None
output = None
if options.input:
input = open(options.input, 'rU')
if options.output:
output = open(options.output, 'w')
if input is None:
parser.error("You must provide at least a valid input files.")
# Load the Workbook
workbook = load_workbook(options.input)
sheet_groups = workbook['Groups']
sheet_sections = workbook['Sections']
sheet_items = workbook['Items']
starting_ind = 2
Dictionary_Groups = {}
while isOK(getVal(sheet_groups, 0, starting_ind)) :
Dictionary_Groups[getVal(sheet_groups, 0, starting_ind)] = getVal(sheet_groups, 2, starting_ind)
print('Added ' + getVal(sheet_groups, 0, starting_ind) + ' ' + Dictionary_Groups[getVal(sheet_groups, 0, starting_ind)])
starting_ind += 1
'''
This is the schema for the Items sheet
A 0 name ITEM_NAME
B 1 label_short DESCRIPTION_LABEL
C 2 caption LEFT_ITEM_TEXT
D 3 units UNITS
E 4 RIGHT_ITEM_TEXT
F 5 section SECTION_LABEL
G 6 group GROUP_LABEL
H 7 HEADER
I 8 SUBHEADER
J 9 PARENT_ITEM
K 10 COLUMN_NUMBER
L 11 PAGE_NUMBER
M 12 QUESTION_NUMBER
N 13 type RESPONSE_TYPE
O 14 ??? RESPONSE_LABEL
P 15 options RESPONSE_OPTIONS_TEXT
Q 16 options_values RESPONSE_VALUES_OR_CALCULATIONS
R 17 RESPONSE_LAYOUT
S 18 DEFAULT_VALUE
T 19 data_type DATA_TYPE
U 20 WIDTH_DECIMAL
V 21 validation_regex VALIDATION
W 22 validation_message VALIDATION_ERROR_MESSAGE
X 23 PHI
Y 24 required REQUIRED
Z 25 display_status ITEM_DISPLAY_STATUS
AA 26 display_condition SIMPLE_CONDITIONAL_DISPLAY
'''
clusterSection = ''
clusterGroup = ''
ALL_FORM_ELEMENTS = {}
DICTIONARY_ELEMENTS = {}
lineNumber=0
ind = 1
SI = sheet_items
while isOK(getVal(SI,0,ind)):
ind += 1
print('Elaborating row ' + str(ind))
# for row in reader:
# lineNumber+=1
# if len(row) < 1 or lineNumber == 1:
# continue
# if row[0] == "" or row[0] is None or row[13] == "" or row[13] is None:
# continue
if not isOK(getVal(SI,0,ind)) or not isOK(getVal(SI,13,ind)):
continue
section = ''
sectionFull = ''
if isOK(getVal(SI,5,ind)):
sectionFull = getVal(SI,5,ind)
section = "".join(sectionFull.split())
if section != '' and section is not None:
if section not in ALL_FORM_ELEMENTS:
ALL_FORM_ELEMENTS[section] = []
if section not in DICTIONARY_ELEMENTS:
DICTIONARY_ELEMENTS[section] = []
if clusterSection != getVal(SI,5,ind):
SINGLE_ELEMENT = {}
SINGLE_ELEMENT['type'] = 'p'
SINGLE_ELEMENT['class'] = 'section'
SINGLE_ELEMENT['html'] = getVal(SI,5,ind)
ALL_FORM_ELEMENTS[section].append(SINGLE_ELEMENT)
clusterSection = getVal(SI,5,ind)
if clusterGroup != getVal(SI,6,ind):
SINGLE_ELEMENT = {}
SINGLE_ELEMENT['type'] = 'p'
SINGLE_ELEMENT['class'] = 'group'
SINGLE_ELEMENT['html'] = Dictionary_Groups[getVal(SI,6,ind)]
ALL_FORM_ELEMENTS[section].append(SINGLE_ELEMENT)
clusterGroup = getVal(SI,6,ind)
SINGLE_ELEMENT = {}
SINGLE_ELEMENT['name'] = getVal(SI,0,ind)
SINGLE_ELEMENT['id'] = getVal(SI,0,ind)
SINGLE_ELEMENT['type'] = convertOCType(getVal(SI,13,ind).strip())
# In case of checkboxes transform the element
# into an array otherwise we will receive only
# the last selection at the server
if SINGLE_ELEMENT['type'] == 'checkboxes':
SINGLE_ELEMENT['name'] += '[]'
validate = {}
if isOK(getVal(SI,24,ind)) and (getVal(SI,24,ind) == '1' or getVal(SI,24,ind) == 1):
SINGLE_ELEMENT['validate'] = {'required': True, 'min': 0}
if isOK(getVal(SI,2,ind)):
SINGLE_ELEMENT['caption'] = getVal(SI,2,ind)
if isOK(getVal(SI,15,ind)) and isOK(getVal(SI,16,ind)):
SINGLE_ELEMENT['options'] = combineOptions(getVal(SI,15,ind), getVal(SI,16,ind))
# Activates the datepicker
if isOK(getVal(SI,19,ind)) and getVal(SI,19,ind) == 'DATE':
SINGLE_ELEMENT['datepicker'] = {}
# Substitute the type with number
if SINGLE_ELEMENT['type'] == 'text' and isOK(getVal(SI,19,ind)) and (getVal(SI,19,ind) == 'INT' or getVal(SI,19,ind) == 'REAL'):
SINGLE_ELEMENT['type'] = 'number'
if(getVal(SI,19,ind) == 'REAL'):
SINGLE_ELEMENT['step'] = 'any'
if isOK(getVal(SI,3,ind)):
SINGLE_ELEMENT['units'] = getVal(SI,3,ind)
if isOK(getVal(SI,5,ind)):
SINGLE_ELEMENT['section'] = getVal(SI,5,ind)
if isOK(getVal(SI,6,ind)):
SINGLE_ELEMENT['oc_group'] = getVal(SI,6,ind)
if isOK(getVal(SI,24,ind)) and getVal(SI,24,ind) == '1':
SINGLE_ELEMENT['required'] = True
ENCLOSING_ENVELOP = {}
ENCLOSING_ENVELOP['type'] = 'div'
ENCLOSING_ENVELOP['class'] = 'envelop'
ENCLOSING_ENVELOP['html'] = SINGLE_ELEMENT
ALL_FORM_ELEMENTS[section].append(ENCLOSING_ENVELOP)
if SINGLE_ELEMENT['type'] != 'p' and SINGLE_ELEMENT['type'] != 'div':
DICTIONARY_ELEMENTS[section].append(SINGLE_ELEMENT['name'])
# '<link rel="stylesheet" type="text/css" href="css/formalize.css"/>' \
# '<script src="js/jquery.formalize.min.js"></script>' \
html = '<link rel="stylesheet" type="text/css" href="css/dformStyle.css"/>' \
'<script src="js/jquery.min.js"></script>' \
'<script src="js/jquery-ui.min.js"></script>' \
'<script src="js/jquery.dform-1.1.0.min.js"></script>'\
'<script src="js/jquery.validate.min.js"></script>'\
'<script src="js/additional-methods.min.js"></script>'
# FINAL_ENVELOP = {}
# FINAL_ENVELOP['type'] = 'tabs'
# FINAL_ENVELOP['entries'] = []
#
# for Section, Elements in ALL_FORM_ELEMENTS.iteritems():
# F={}
# F['type'] = 'submit'
# F['value'] = 'Submit'
# Elements.append(F)
#
# ELEMENT_TAB = {}
# ELEMENT_TAB['caption'] = Section
# ELEMENT_TAB['html'] = Elements
#
# FINAL_ENVELOP['entries'].append(ELEMENT_TAB)
#
#
#
# html += '<form id="myform"></form><script type="text/javascript">$(function() {$("#myform").dform({ "action": "http://localhost:8888/supporthf2/plotFormData.php", "method": "post", "html": '
# html += json.dumps(FINAL_ENVELOP)
# html += '});});</script>'
for Section, Elements in ALL_FORM_ELEMENTS.iteritems():
if connection:
query = "INSERT INTO data_forms(name,versionbig,versionsmall,form_schema) VALUES (%s, %s, %s, %s)"
cur = connection.cursor()
cur.execute(query, (Section, VERSION_BIG, VERSION_SMALL, json.dumps(Elements)))
connection.commit()
html += '<form id="myform'+Section+'"></form><script type="text/javascript">$(function() {$("#myform'+Section+'").dform({ "action": "http://localhost:8888/supporthf2/plotFormData.php", "method": "post", "html": '
F={}
F['type'] = 'submit'
F['value'] = 'Submit'
Elements.append(F)
html += json.dumps(Elements)
html += '});});</script>'
D = {}
for Section, Elements in DICTIONARY_ELEMENTS.iteritems():
for i in Elements:
if i not in D:
D[i] = []
D[i] = Section
print json.dumps(D)
output.write(html)