This repository was archived by the owner on Apr 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxls2jsonYGREFrame.py
283 lines (257 loc) · 8.57 KB
/
xls2jsonYGREFrame.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
# -*- coding: utf-8 -*-
import os
import sys
import xlrd
import datetime
import hashlib
import base64
import json
from basic.common import getrootpath, makeDirsForFile, existFile
from basic.tools import date2dateStr, countDays
from basic.data import WEEKDAY2STR
reload(sys)
sys.setdefaultencoding('utf-8');
def main():
rootpath = getrootpath()
inputpath = os.path.join(rootpath, 'Data')
outputpath = os.path.join(rootpath, 'Results')
# name = '田哲予'
name = raw_input('Please input a name (e.g.: 张三): ')
print '--'
YGREFrameFile = os.path.join(inputpath, 'Y-GRE-Frame-%s.xls' % name)
YGREFrameFileX = os.path.join(inputpath, 'Y-GRE-Frame-%s.xlsx' % name)
YGREFrameData = {
'general': {
'basics': {},
'scores': {
'gre': {},
'toefl': {},
'gaokao': {},
},
'application': {},
'framework': {},
'assessment': {},
},
'schedule': [],
'remarks': [],
}
# Read Y-GRE Frame data from Y-GRE-Frame-Name.xls(x)
try:
excelFile = YGREFrameFile
workbook = xlrd.open_workbook(excelFile)
except Exception, e:
excelFile = YGREFrameFileX
workbook = xlrd.open_workbook(excelFile)
print 'Import Y-GRE Frame data: %s' % excelFile
# Read Y-GRE Frame data: General
sheet = workbook.sheet_by_index(0)
values = []
for row in range(sheet.nrows):
ctype = sheet.cell(row, 1).ctype
value = sheet.cell(row, 1).value
if ctype == 0:
# empty
value = 'N/A'
elif ctype == 1:
# string
pass
elif ctype == 2:
# number
if row == 45:
value = str(value * 100)
else:
value = str(value)
if value[-2:] == '.0':
value = value[:-2]
if row == 45:
value = value + '%'
elif ctype == 3:
# date
value = datetime.date(*xlrd.xldate_as_tuple(value, workbook.datemode)[:3])
value = date2dateStr(value)
else:
print 'Warning: invalid ctype:', ctype
value = 'Invalid'
values.append(value)
YGREFrameData['general'] = {
'basics': {
'name': values[0],
'vb_class': values[1],
'y_gre_class': values[2],
'university': values[3],
'department': values[4],
'enrollment_year': values[5],
'degree': values[6],
'gpa': values[7],
'gpa_full_marks': values[8],
},
'scores': {
'gre': {
'v_initial': values[9],
'q_initial': values[10],
'aw_initial': values[11],
'v_admission': values[12],
'q_admission': values[13],
'aw_admission': values[14],
'v_ppii_1': values[15],
'q_ppii_1': values[16],
'v_ppii_2': values[17],
'q_ppii_2': values[18],
'v_aim': values[19],
'q_aim': values[20],
'aw_aim': values[21],
},
'toefl': {
'total': values[22],
'reading': values[23],
'listening': values[24],
'speaking': values[25],
'writing': values[26],
'aim': values[27],
},
'gaokao': {
'total': values[28],
'full_marks': values[29],
'math': values[30],
'english': values[31],
},
},
'application': {
'country': values[32],
'major': values[33],
'degree': values[34],
'aim': values[35],
'agency': values[36],
},
'framework': {
'g0_start': values[37],
'g0_end': values[38],
'g0_days': str(countDays(values[37], values[38])),
'g1_start': values[39],
'g1_end': values[40],
'g1_days': str(countDays(values[39], values[40])),
'g2_start': values[41],
'g2_end': values[42],
'g2_days': str(countDays(values[41], values[42])),
'deadline': values[43],
'required_time': values[44],
},
'assessment': {
'applicability': values[45],
'assessor': values[46],
'first_executive_supervisor': values[47],
'second_executive_supervisor': values[48],
},
}
# Read Y-GRE Frame data: Schedule
sheet = workbook.sheet_by_index(1)
weekCount = 0
weekDict = {
'monday': {
'tasks': [],
},
'tuesday': {
'tasks': [],
},
'wednesday': {
'tasks': [],
},
'thursday': {
'tasks': [],
},
'friday': {
'tasks': [],
},
'saturday': {
'tasks': [],
},
'sunday': {
'tasks': [],
},
'nota_bene': [],
}
YGREFrameData['schedule'].append(weekDict)
# Week date prefix
firstDay = datetime.date(*xlrd.xldate_as_tuple(sheet.cell(0,0).value, workbook.datemode)[:3])
for delta in range(firstDay.weekday(), 0, -1):
date = firstDay - datetime.timedelta(delta)
weekday = date.weekday()
dateStr = date2dateStr(date)
YGREFrameData['schedule'][weekCount][WEEKDAY2STR[weekday]]['date'] = dateStr
YGREFrameData['schedule'][weekCount][WEEKDAY2STR[weekday]]['display'] = False
# Week date contents
for row in range(sheet.nrows):
values = sheet.row_values(row)
date = datetime.date(*xlrd.xldate_as_tuple(values[0], workbook.datemode)[:3])
weekday = date.weekday()
dateStr = date2dateStr(date)
YGREFrameData['schedule'][weekCount][WEEKDAY2STR[weekday]]['date'] = dateStr
YGREFrameData['schedule'][weekCount][WEEKDAY2STR[weekday]]['display'] = True
for value in values[1:]:
if value != '':
YGREFrameData['schedule'][weekCount][WEEKDAY2STR[weekday]]['tasks'].append(value)
if weekday == 6:
weekCount += 1
weekDict = {
'monday': {
'tasks': [],
},
'tuesday': {
'tasks': [],
},
'wednesday': {
'tasks': [],
},
'thursday': {
'tasks': [],
},
'friday': {
'tasks': [],
},
'saturday': {
'tasks': [],
},
'sunday': {
'tasks': [],
},
'nota_bene': [],
}
YGREFrameData['schedule'].append(weekDict)
# Week date suffix
if weekday == 6:
YGREFrameData['schedule'].pop(-1)
else:
lastDay = date
for delta in range(1, 7 - lastDay.weekday()):
date = lastDay + datetime.timedelta(delta)
weekday = date.weekday()
dateStr = date2dateStr(date)
YGREFrameData['schedule'][weekCount][WEEKDAY2STR[weekday]]['date'] = dateStr
YGREFrameData['schedule'][weekCount][WEEKDAY2STR[weekday]]['display'] = False
# Read Y-GRE Frame data: N.B.
sheet = workbook.sheet_by_index(2)
for row in range(sheet.nrows):
values = sheet.row_values(row)
for value in values[1:]:
if value != '':
YGREFrameData['schedule'][row]['nota_bene'].append(value)
# Read Y-GRE Frame data: Remarks
sheet = workbook.sheet_by_index(3)
values = sheet.col_values(0)
for value in values:
YGREFrameData['remarks'].append(value)
# Generate passkey
password = YGREFrameData['general']['basics']['name']
salt = YGREFrameData['general']['basics']['vb_class'] + YGREFrameData['general']['basics']['y_gre_class'] + YGREFrameData['general']['basics']['university']
passkey = base64.urlsafe_b64encode(hashlib.pbkdf2_hmac('sha256', password, salt, 100000, dklen=24))
print 'Passkey:', passkey
# Write to passkey.js file
jsonFilename = '%s.js' % passkey
print 'Write to file: %s' % jsonFilename
jsonFile = os.path.join(outputpath, jsonFilename)
makeDirsForFile(jsonFile)
templateContent = 'frame_data = %s;'
with open(jsonFile, 'w') as f:
f.write(templateContent % json.dumps(YGREFrameData))
if __name__ == '__main__':
main()