Commit 172d3f64 authored by Pierre Dittgen's avatar Pierre Dittgen

Better catetegorize empty-header errors

parent 9bef58fd
......@@ -193,7 +193,8 @@ def repair_core(dataset: tablib.Dataset, schema_field_names):
content_dataset = tablib.Dataset()
rejected_cols_dataset = tablib.Dataset()
column_names_dict = dict()
last_nonempty_header_col = -1
last_nonempty_header_col = None
first_nonempty_header_col = None
empty_header_cols = []
for i, head in enumerate(dataset.headers):
......@@ -203,11 +204,17 @@ def repair_core(dataset: tablib.Dataset, schema_field_names):
empty_header_cols.append(i)
continue
# Remember first non-empty header
if first_nonempty_header_col is None:
first_nonempty_header_col = i
# Remember last non-empty header
last_nonempty_header_col = i
# Move unknown columns in a special dataset
if head not in schema_field_names:
report.append(goodtables.Error(code='extra-header',
message_substitutions={'column-name': head}))
append_col(rejected_cols_dataset, dataset.get_col(i), head)
continue
......@@ -223,10 +230,26 @@ def repair_core(dataset: tablib.Dataset, schema_field_names):
column_names_dict[head] = i
# add blank-header errors
if last_nonempty_header_col != -1:
report.extend([goodtables.Error(code='blank-header',
message_substitutions={'column-number': col_id + 1})
for col_id in empty_header_cols])
def create_blank_header_error(col_id, pos_type, addon={}):
return goodtables.Error(code='blank-header', message_substitutions={
'column-number': col_id + 1,
'position': pos_type,
**addon
})
# With context to ease repairing
for col_id in empty_header_cols:
if col_id < first_nonempty_header_col:
report.append(create_blank_header_error(col_id, 'leading'))
elif col_id > last_nonempty_header_col:
report.append(create_blank_header_error(col_id, 'trailing'))
else:
before_header = list(filter(lambda elt: elt != EMPTY_HEADER, dataset.headers[:col_id][::-1]))[0]
after_header = list(filter(lambda elt: elt != EMPTY_HEADER, dataset.headers[col_id+1:]))[0]
position_addon = {
'before-header-name': before_header,
'after-header-name': after_header,
}
report.append(create_blank_header_error(col_id, 'in', addon=position_addon))
# Compare ordering
if content_dataset.headers:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment