-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_parser.py
More file actions
325 lines (278 loc) · 11.3 KB
/
python_parser.py
File metadata and controls
325 lines (278 loc) · 11.3 KB
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
321
322
323
324
325
import json, time, sys, collections
from google_sheet import GoogleSheet
def get_file(spreadsheetId):
result = service.spreadsheets().values().get(spreadsheetId=spreadsheetId, range='sheet2!A1:ZZ').execute()
return result.get('values', [])
class MatrixAccessor:
matrix = [[]]
(start_row, start_col, len_row, len_col) = (0, 0, 0, 0)
def __init__(self, matrixOrAccessor, start_row, start_col, len_row = None, len_col = None):
if not isinstance(matrixOrAccessor, MatrixAccessor):
self.matrix = matrixOrAccessor
(self.start_row, self.start_col, self.len_row, self.len_col) = (start_row, start_col, len_row, len_col)
else:
self.matrix = matrixOrAccessor.matrix
self.start_row += matrixOrAccessor.start_row + start_row
self.start_col += matrixOrAccessor.start_col + start_col
(self.len_row, self.len_col) = (len_row, len_col)
def getRow(self, row):
if self.len_col:
return self.matrix[row + self.start_row][self.start_col:self.start_col + self.len_col]
else:
return self.matrix[row + self.start_row][self.start_col:]
def getMatrix(self):
return self.getMatrixWithBound(0, self.getHeight(), 0, self.getWidth())
def getMatrixWithBound(self, up, down, left, right):
ret = []
for i in range(up, down):
row = []
for j in range(left, right):
row.append(self.getCell(i, j))
ret.append(row)
return ret
def getCell(self, row, col):
return self.matrix[row + self.start_row][col + self.start_col]
def setCell(self, row, col, value):
self.matrix[row + self.start_row][col + self.start_col] = value
def getWidth(self):
return self.len_col if self.len_col else len(self.matrix[0]) - self.start_col
def getHeight(self):
return self.len_row if self.len_row else len(self.matrix) - self.start_row
class MlgbNoContent:
def __init__(self):
return
def mlgb(accessor, rangeGetter):
if not isinstance(accessor.getCell(0,0), basestring):
return accessor.getCell(0,0)
(height, width) = (accessor.getHeight(), accessor.getWidth())
#print height,width
if height == 0 or width == 0:
return MlgbNoContent()
elif (height == 1 or not accessor.getCell(1,0)) and (width == 1 or not accessor.getCell(0,1)):
return parseJsonString(accessor.getCell(0,0), rangeGetter) if accessor.getCell(0,0) != "" else MlgbNoContent()
elif accessor.getCell(0,0) == "-":
return mlgbList(accessor, rangeGetter)
elif accessor.getCell(0,0) == "#":
return mlgbSharp(accessor, rangeGetter)
elif accessor.getCell(0,0) == '...':
# has ..., - -> List
allKeys = set([accessor.getCell(i,0) for i in range(height) if len(accessor.getCell(i,0))])
if allKeys == set(['-', '...']):
return mlgbList(accessor, rangeGetter)
else:
return mlgbObject(accessor, rangeGetter)
else:
return mlgbObject(accessor, rangeGetter)
def parseJsonString(string, rangeGetter):
if string == "TRUE": return True
if string == "FALSE": return False
try: return json.loads(string)
except ValueError:
if string.startswith("->"): return mlgb(MatrixAccessor(rangeGetter(string[2:]), 0, 0), rangeGetter)
return string
def mlgbList(accessor, rangeGetter):
indexRowsNumbers = []
for i in range(accessor.getHeight()):
cell = accessor.getCell(i,0)
if cell == '-' or cell == '...':
indexRowsNumbers.append(i)
elif cell:
return []
elems = []
lenIndexRowNumbers = len(indexRowsNumbers)
for i, indexRowsNumber in enumerate(indexRowsNumbers):
accessorHeight = indexRowsNumbers[i+1] - indexRowsNumber if i != lenIndexRowNumbers - 1 else accessor.getHeight() - indexRowsNumber
mlgbResult = mlgb(MatrixAccessor(accessor, indexRowsNumber, 1, accessorHeight, accessor.getWidth() - 1), rangeGetter)
if accessor.getCell(indexRowsNumber, 0) == '...':
if isinstance(mlgbResult, list):
elems += mlgbResult
else:
elems.append(mlgbResult)
return filter(lambda x: not isinstance(x, MlgbNoContent), elems)
def mlgbObject(accessor, rangeGetter):
if accessor.getWidth() == 1:
return None
rowKeyList = []
for i in range(accessor.getHeight()):
cell = accessor.getCell(i,0)
if cell: rowKeyList.append({"row": i, "key": cell})
elems = []
lenRowKeyList = len(rowKeyList)
for i, rowKey in enumerate(rowKeyList):
accessorHeight = rowKeyList[i+1]["row"] - rowKey["row"] if i != lenRowKeyList - 1 else accessor.getHeight() - rowKey["row"]
elems.append({
"keys": rowKey["key"] if rowKey["key"]=="..." else rowKey["key"].split("."),
"value": mlgb(MatrixAccessor(accessor, rowKey["row"], 1, accessorHeight, accessor.getWidth() - 1), rangeGetter)
})
# Expand ...
if len(elems) == 1 and elems[0]["keys"]=="...":
return elems[0]["value"]
# if all keys are ... and values are all list then return list
if set([elem['keys'] for elem in elems if not isinstance(elem['keys'], list)]) == set(['...']) and set([isinstance(elem['value'], list) for elem in elems]) == set([True]):
ret = []
for sublist in [elem['value'] for elem in elems]:
for item in sublist:
ret.append(item)
return ret
# Concat result
result = collections.OrderedDict()
for elem in elems:
if elem["keys"] == "...":
if isinstance(elem["value"], dict):
result.update(elem["value"])
elif not isinstance(elem["value"], MlgbNoContent):
curObject = result
# set keys en route
for key in elem["keys"][0:-1]:
if key not in curObject:
curObject[key] = collections.OrderedDict()
curObject = curObject[key]
curObject[elem["keys"][-1]] = elem["value"]
return result if len(result.keys()) else MlgbNoContent()
def mlgbSharp(accessor, rangeGetter):
def findSquareSharps(accessor):
sharpRows = 0
for i in range(accessor.getHeight()):
if accessor.getCell(i,0) == "#": sharpRows = i + 1
else: break
sharpCols = 0
for j in range(accessor.getWidth()):
if accessor.getCell(0,j) == "#": sharpCols = j + 1
else: break
for i in range(sharpRows):
if i == 0: continue
for j in range(sharpCols):
if j == 0: continue
if accessor.getCell(i, j) != "#": return (0,0)
return (sharpRows,sharpCols)
def findRowsWithKeys(accessor):
ret = []
for i in range(accessor.getHeight()):
for j in range(accessor.getWidth()):
if accessor.getCell(i, j) != "":
ret.append(i)
break
return ret
def findColsWithKeys(accessor):
#find folded edge
cellWithSharp = []
# when multiple sharps appear, only the highest one will be used
colsWithSharp = []
cellFoldedEdgeMapping = {}
for i in range(accessor.getHeight()):
for j in range(accessor.getWidth()):
#Folded edge (applying starts from second row)
if accessor.getCell(i, j) == "#" and i > 0 and j not in colsWithSharp:
cellWithSharp.append((i, j))
colsWithSharp.append(j)
#Folded edge
for xy in cellWithSharp:
#find next element of row above:
up = xy[0]
left = xy[1]
down = accessor.getHeight()
right = accessor.getWidth()
for j in range(xy[1] + 1, accessor.getWidth()):
if accessor.getCell(up - 1, j) != "":
right = j
break
#copy this range to a new matrix
cellFoldedEdgeMapping[xy] = accessor.getMatrixWithBound(up, down, left, right)
#generate a new col
cellToErase = []
for cell, matrix in cellFoldedEdgeMapping.items():
for i in range(len(matrix)):
for j in range(len(matrix[0])):
cellToErase.append((cell[0]+i, cell[1]+j))
matrixWithoutFoldedEdge = []
for i in range(accessor.getHeight()):
row = []
for j in range(accessor.getWidth()):
row.append(accessor.getCell(i,j) if (i,j) not in cellToErase else "")
matrixWithoutFoldedEdge.append(row)
matrixWithoutFoldedEdge = MatrixAccessor(matrixWithoutFoldedEdge, 0, 0)
colsWithKeys = []
for i in range(matrixWithoutFoldedEdge.getHeight()):
for j in range(matrixWithoutFoldedEdge.getWidth()):
#columns with key
if matrixWithoutFoldedEdge.getCell(i, j) != "" and j not in colsWithKeys:
colsWithKeys.append(j)
colsWithKeys.sort()
return (colsWithKeys, cellFoldedEdgeMapping, matrixWithoutFoldedEdge)
def diffLens(lenList, allLen):
list1 = [0] + lenList
list2 = lenList + [allLen]
return [list2_i - list1_i for list1_i, list2_i in zip(list1, list2)][1:]
def transposeMatrixAccessor(accessor):
matrix = []
for j in range(accessor.getWidth()):
row = []
for i in range(accessor.getHeight()):
row.append(accessor.getCell(i,j))
matrix.append(row)
return MatrixAccessor(matrix, 0, 0)
def insertContentWithRowAxis(contents, accessor, isAxisEmpty):
if isAxisEmpty: return contents[0]
contentIndex = 0
matrix = []
for i in range(accessor.getHeight()):
row = accessor.getRow(i) + [""]
for j in reversed(range(-len(row), 0)):
if(row[j] != ""):
row[j+1] = contents[contentIndex]
contentIndex += 1
break
matrix.append(row)
return mlgb(MatrixAccessor(matrix, 0, 0), rangeGetter)
#delete empty last rows
def clearLastEmptyRows(matrix):
downRow = len(matrix)
for i in range(len(matrix)):
if matrix[i][0] == "":
downRow = i
break
return matrix[0:downRow]
# find square sharps in left-top corner
(sharpRows, sharpCols) = findSquareSharps(accessor)
if not sharpRows or not sharpCols: return MlgbNoContent()
rowAxisMatrixAccessor = MatrixAccessor(accessor, sharpRows, 0, accessor.getHeight() - sharpRows, sharpCols)
colAxisMatrixAccessor = MatrixAccessor(accessor, 0, sharpCols, sharpRows, accessor.getWidth() - sharpCols)
rowsWithKeys = [sharpRows + e for e in findRowsWithKeys(rowAxisMatrixAccessor)]
#columns with keys, and folded edge will be found
(colsWithKeys, cellFoldedEdgeMapping, matrixWithoutFoldedEdge) = findColsWithKeys(colAxisMatrixAccessor)
colsWithKeys = [sharpCols + e for e in colsWithKeys]
#print matrixWithoutFoldedEdge.getMatrix()
colAxisTransposeMatrixAccessor = transposeMatrixAccessor(matrixWithoutFoldedEdge)
colFoldedEdgeMapping = {k[1]+sharpCols:clearLastEmptyRows(v) for (k,v) in cellFoldedEdgeMapping.items()}
# empty row keys or col keys
(isRowWithKeysEmpty, isColWithKeysEmpty) = (not len(rowsWithKeys), not len(colsWithKeys))
if isRowWithKeysEmpty: rowsWithKeys = [sharpRows]
if isColWithKeysEmpty: colsWithKeys = [sharpCols]
rowsLens = diffLens(rowsWithKeys, accessor.getHeight())
colsLens = diffLens(colsWithKeys, accessor.getWidth())
#print colFoldedEdgeMapping, colsWithKeys
contentsMatrix = []
# get contents of cells
for i in range(len(rowsWithKeys)):
contentsRow = []
for j in range(len(colsWithKeys)):
if colsWithKeys[j] not in colFoldedEdgeMapping:
matrixAccessor = MatrixAccessor(accessor, rowsWithKeys[i], colsWithKeys[j], rowsLens[i], colsLens[j])
else:
upper = colFoldedEdgeMapping[colsWithKeys[j]] #folded edge
#print "hehe",rowsWithKeys[i], colsWithKeys[j], rowsLens[i], colsLens[j]
lower = accessor.getMatrixWithBound(rowsWithKeys[i], rowsWithKeys[i]+rowsLens[i], colsWithKeys[j], colsWithKeys[j]+colsLens[j])
#print upper+lower
matrixAccessor = MatrixAccessor(upper+lower, 0, 0)
#print matrixAccessor.getMatrix()
contentsRow.append(mlgb(matrixAccessor, rangeGetter))
contentsMatrix.append(contentsRow)
contentsRow = [insertContentWithRowAxis(row, colAxisTransposeMatrixAccessor, isColWithKeysEmpty) for row in contentsMatrix]
return insertContentWithRowAxis(contentsRow, rowAxisMatrixAccessor, isRowWithKeysEmpty)
def parse(spreadsheetId):
googleSheet = GoogleSheet('AIzaSyDk90mbxnE2U3nl0xf00djXH0LfmGYQfZ4')
contents = googleSheet.getSpreadsheet(spreadsheetId)
res = mlgb(MatrixAccessor(contents, 0, 0), googleSheet.getRange)
return json.dumps(res, ensure_ascii=False).encode("utf8")
if __name__ == "__main__":
print(parse(sys.argv[1]))