Skip to content

Commit 948f6f0

Browse files
authored
Merge pull request #175 from OpenDataServices/174-hashcomments
[#174] Add hashcomments command to xlsx
2 parents 3dbfc74 + 261ebfd commit 948f6f0

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

flattentool/input.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,8 @@ def get_sheet_headings(self, sheet_name):
574574
sheet_configuration = {}
575575

576576
skip_rows = sheet_configuration.get("skipRows", 0)
577-
if sheet_configuration.get("ignore"):
577+
if (sheet_configuration.get("ignore") or
578+
(sheet_configuration.get("hashcomments") and sheet_name.startswith('#'))):
578579
# returning empty headers is a proxy for no data in the sheet.
579580
return []
580581

@@ -617,7 +618,13 @@ def get_sheet_lines(self, sheet_name):
617618
header_row = worksheet.rows[skip_rows + configuration_line]
618619
remaining_rows = worksheet.rows[skip_rows + configuration_line + header_rows:]
619620

620-
coli_to_header = ({i: x.value for i, x in enumerate(header_row) if x.value is not None})
621+
coli_to_header = {}
622+
for i, header in enumerate(header_row):
623+
if header.value is None:
624+
continue
625+
if sheet_configuration.get("hashcomments") and str(header.value).startswith('#'):
626+
continue
627+
coli_to_header[i] = header.value
621628
for row in remaining_rows:
622629
yield OrderedDict((coli_to_header[i], x.value) for i, x in enumerate(row) if i in coli_to_header)
623630

flattentool/lib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ def parse_sheet_configuration(configuration_list):
1515
configuration['headerRows'] = max(int(parts[1]), 1)
1616
if (len(parts) == 1 and parts[0].lower() == "ignore"):
1717
configuration['ignore'] = True
18+
if (len(parts) == 1 and parts[0].lower() in ("hashcomments", "hashcomment")):
19+
configuration['hashcomments'] = True
1820
return configuration
8.24 KB
Binary file not shown.

flattentool/tests/test_init.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,3 +1240,20 @@ def test_commands_ignore(tmpdir):
12401240
unflattened = json.load(tmpdir.join('command_single_unflattened.json'))
12411241

12421242
assert unflattened == {'main': [{'actual': 'actual', 'headings': 'data', 'some': 'some'}]}
1243+
1244+
def test_commands_hashcomments(tmpdir):
1245+
1246+
unflatten(
1247+
'flattentool/tests/fixtures/xlsx/commands_hashcomments.xlsx',
1248+
input_format='xlsx',
1249+
output_name=tmpdir.join('commands_hashcomments_unflattened.json').strpath,
1250+
cell_source_map=tmpdir.join('commands_hashcomments_source_map.json').strpath,
1251+
heading_source_map=tmpdir.join('commands_hashcomments_heading_source_map.json').strpath,
1252+
metatab_name='Meta',
1253+
metatab_vertical_orientation=True
1254+
)
1255+
1256+
unflattened = json.load(tmpdir.join('commands_hashcomments_unflattened.json'))
1257+
1258+
assert unflattened == {'main': [{'actual': 'actual', 'headings': 'data', 'some': 'some'}, {'actual': 'actual', 'headings': 'Other data', 'some': 'some'}],
1259+
'some': 'data'}

0 commit comments

Comments
 (0)