-
Notifications
You must be signed in to change notification settings - Fork 4
/
ether_validator.py
324 lines (272 loc) · 13.2 KB
/
ether_validator.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
321
322
323
324
from validator import Validator
from collections import defaultdict
import re
class EtherValidator(Validator):
def __init__(self, rule):
self.corpus = rule[0]
self.doc = rule[1]
self.domain = rule[2]
self.name = rule[3]
self.operator = rule[4]
self.argument = rule[5]
def _apply_exists(self, parsed_ether):
report = ''
tooltip = ''
cells = []
colmap = parsed_ether['__colmap__'] # name -> list of col letters
col_letters = colmap[self.name] # list of letters with col name
if len(col_letters) == 0:
report += "Column named '" + self.name + "' not found<br/>"
return report, tooltip, cells
def _apply_doesntexist(self, parsed_ether):
report = ''
tooltip = ''
cells = []
colmap = parsed_ether['__colmap__'] # name -> list of col letters
col_letters = colmap[self.name] # list of letters with col name
if len(col_letters) > 0:
report += "Columns named '" + self.name + "' are not allowed<br/>"
cells += [letter + "1" for letter in col_letters]
return report, tooltip, cells
def _apply_span_equals_number(self, parsed_ether):
report = ''
tooltip = ''
cells = []
colmap = parsed_ether['__colmap__'] # name -> list of col letters
col_letters = colmap[self.name] # list of letters with col name
if len(col_letters) == 0:
report += "Column named " + self.name + " not found<br/>"
return report, tooltip, cells
for letter in col_letters:
for cell in parsed_ether[letter]:
if cell.row == "1":
continue
if self.argument == "1":
if cell.span != "1":
report += "Cell " + cell.col + cell.row + ": span is not 1<br/>"
cells.append(cell.col + cell.row)
else:
if cell.span != "" and cell.span != self.argument:
report += "Cell " + cell.col + cell.row + ": span is not " + self.argument + "<br/>"
cells.append(cell.col + cell.row)
return report, tooltip, cells
def _apply_regex(self, parsed_ether):
report = ''
tooltip = ''
cells = []
colmap = parsed_ether['__colmap__'] # name -> list of col letters
col_letters = colmap[self.name] # list of letters with col name
for letter in col_letters:
for cell in parsed_ether[letter]:
if cell.row == "1":
continue
match = re.search(self.argument, cell.content)
if match is None:
report += ("Cell " + cell.col + cell.row
+ ": content does not match pattern " + self.argument + "<br/>")
tooltip += ("Cell " + cell.col + cell.row + ":<br/>"
+ "Content: " + cell.content + "<br/>"
+ "Pattern: " + self.argument + "<br/>")
cells.append(cell.col + cell.row)
return report, tooltip, cells
def _binary_op_check_cols_exist(self, colmap):
name_letters = colmap[self.name]
arg_letters = colmap[self.argument]
if len(name_letters) == 0:
if self.operator != "==":
return "Column named " + self.name + " not found<br/>"
if len(arg_letters) == 0:
if self.operator != "==":
return "Column named " + self.argument + " not found<br/>"
return ""
def _binary_op_setup(self, parsed_ether):
colmap = parsed_ether['__colmap__'] # name -> list of col letters
name_letters = colmap[self.name]
arg_letters = colmap[self.argument]
name_tuples = defaultdict(list)
arg_tuples = defaultdict(list)
start_rows = defaultdict(list)
all_rows = []
for letter in name_letters:
for cell in parsed_ether[letter]:
start_rows[letter].append(cell.row)
# "de-merge" cell so we have an entry for every row in its span with its letter and content
for i in range(int(cell.span) or 1):
row = str(int(cell.row) + i)
name_tuples[row].append((letter, cell.content))
all_rows.append(row)
# same as above with arg_letters
for letter in arg_letters:
for cell in parsed_ether[letter]:
start_rows[letter].append(cell.row)
for i in range(int(cell.span) or 1):
row = str(int(cell.row) + i)
arg_tuples[row].append((letter, cell.content))
if row not in all_rows:
all_rows.append(row)
name_start_cells = []
name_start_rows = set() # for O(1) lookup
for letter in name_letters:
name_start_cells += [(letter, row) for row in start_rows[letter]]
name_start_rows = name_start_rows.union(set(row for row in start_rows[letter]))
arg_start_cells = []
arg_start_rows = set()
for letter in arg_letters:
arg_start_cells += [(letter, row) for row in start_rows[letter]]
arg_start_rows = arg_start_rows.union(set(row for row in start_rows[letter]))
return name_letters, arg_letters, name_tuples, arg_tuples, start_rows, all_rows, \
name_start_cells, name_start_rows, arg_start_cells, arg_start_rows
def _apply_subspan(self, parsed_ether):
report = ''
tooltip = ''
cells = []
colmap = parsed_ether['__colmap__'] # name -> list of col letters
col_letters = colmap[self.name] # list of letters with col name
err = self._binary_op_check_cols_exist(colmap)
if err:
report += err
return report, tooltip, cells
name_letters, arg_letters, name_tuples, \
arg_tuples, start_rows, all_rows, \
name_start_cells, name_start_rows, \
arg_start_cells, arg_start_rows = self._binary_op_setup(parsed_ether)
for row in all_rows:
# check to see if all cells in rhs are contained within cells on lhs
if row in arg_tuples and row not in name_tuples:
for letter, _ in arg_tuples[row]:
cells.append(letter + row)
report += ("Cell " + letter + row
+ " must appear in the span of a cell in one of these columns: "
+ ", ".join(name_letters) + "<br/>")
return report, tooltip, cells
def _apply_equal_span_length(self, parsed_ether):
report = ''
tooltip = ''
cells = []
colmap = parsed_ether['__colmap__'] # name -> list of col letters
col_letters = colmap[self.name] # list of letters with col name
err = self._binary_op_check_cols_exist(colmap)
if err:
report += err
return report, tooltip, cells
name_letters, arg_letters, name_tuples, \
arg_tuples, start_rows, all_rows, \
name_start_cells, name_start_rows, \
arg_start_cells, arg_start_rows = self._binary_op_setup(parsed_ether)
for row in all_rows:
if row == "1":
continue
name_len = len(name_tuples[row])
arg_len = len(arg_tuples[row])
if name_len > arg_len:
for letter, _ in name_tuples[row][arg_len:]:
if row not in name_start_rows:
cells.append(letter + row)
report += ("Cell " + letter + row
+ " has no corresponding value in one of these columns: "
+ ", ".join(arg_letters) + "<br/>")
elif arg_len > name_len:
for letter, _ in arg_tuples[row][name_len:]:
if row not in arg_start_rows:
cells.append(letter + row)
report += ("Cell " + letter + row
+ " has no corresponding value in one of these columns: "
+ ", ".join(name_letters) + "<br/>")
for letter, row in name_start_cells:
if row not in arg_start_rows:
cells.append(letter + row)
report += ("Cell " + letter + row
+ " needs a span of equal length beginning in one of these columns: "
+ ", ".join(arg_letters) + "<br/>")
for letter, row in arg_start_cells:
if row not in name_start_rows:
cells.append(letter + row)
report += ("Cell " + letter + row
+ " needs a span of equal length beginning in one of these columns: "
+ ", ".join(name_letters) + "<br/>")
return report, tooltip, cells
def _apply_equal_span_length_and_content(self, parsed_ether):
report = ''
tooltip = ''
cells = []
colmap = parsed_ether['__colmap__'] # name -> list of col letters
col_letters = colmap[self.name] # list of letters with col name
err = self._binary_op_check_cols_exist(colmap)
if err:
report += err
return report, tooltip, cells
name_letters, arg_letters, name_tuples, \
arg_tuples, start_rows, all_rows, \
name_start_cells, name_start_rows, \
arg_start_cells, arg_start_rows = self._binary_op_setup(parsed_ether)
for row in all_rows:
if row == "1":
continue
name_len = len(name_tuples[row])
arg_len = len(arg_tuples[row])
if name_len > arg_len:
for letter, _ in name_tuples[row][arg_len:]:
if row not in name_start_rows:
cells.append(letter + row)
report += ("Cell " + letter + row
+ " has no corresponding value in one of these columns: "
+ ", ".join(arg_letters) + "<br/>")
elif arg_len > name_len:
for letter, _ in arg_tuples[row][name_len:]:
if row not in arg_start_rows:
cells.append(letter + row)
report += ("Cell " + letter + row
+ " has no corresponding value in one of these columns: "
+ ", ".join(name_letters) + "<br/>")
for i in range(min(len(name_tuples[row]), len(arg_tuples[row]))):
name_letter, name_content = name_tuples[row][i]
arg_letter, arg_content = arg_tuples[row][i]
if arg_content != name_content and (row in start_rows[arg_letter] or row in start_rows[name_letter]):
cells.append(name_letter + row)
cells.append(arg_letter + row)
report += ("Cells " + name_letter + row
+ " and " + arg_letter + row
+ " must have equivalent content.<br/>")
for letter, row in name_start_cells:
if row not in arg_start_rows:
cells.append(letter + row)
report += ("Cell " + letter + row
+ " needs a span of equal length beginning in one of these columns: "
+ ", ".join(arg_letters) + "<br/>")
for letter, row in arg_start_cells:
if row not in name_start_rows:
cells.append(letter + row)
report += ("Cell " + letter + row
+ " needs a span of equal length beginning in one of these columns: "
+ ", ".join(name_letters) + "<br/>")
return report, tooltip, cells
def _apply_rule(self, parsed_ether):
if self.name is None:
return "", "", []
if self.operator == "exists":
return self._apply_exists(parsed_ether)
if self.operator == "doesntexist":
return self._apply_doesntexist(parsed_ether)
elif self.operator == "|":
return self._apply_span_equals_number(parsed_ether)
elif self.operator == "~":
return self._apply_regex(parsed_ether)
elif self.operator == ">":
return self._apply_subspan(parsed_ether)
elif self.operator == "=":
return self._apply_equal_span_length(parsed_ether)
elif self.operator == "==":
return self._apply_equal_span_length_and_content(parsed_ether)
else:
raise Exception("Unknown EtherCalc validation operator: '" + str(self.operator) + "'")
def applies(self, doc_name, doc_corpus):
if self.corpus is not None and re.search(self.corpus, doc_corpus) is None:
return False
if self.doc is not None and re.search(self.doc, doc_name) is None:
return False
return True
def validate(self, parsed_ether):
report, tooltip, cells = self._apply_rule(parsed_ether)
return {"report": report,
"tooltip": tooltip,
"cells": cells}