Skip to content

Commit dec6eb3

Browse files
committed
Cell source map test with headings from the source
1 parent 7222de7 commit dec6eb3

File tree

2 files changed

+598
-4
lines changed

2 files changed

+598
-4
lines changed

flattentool/input.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ def get_sub_sheets_lines(self):
169169
def get_sheet_lines(self, sheet_name):
170170
raise NotImplementedError
171171

172+
def get_sheet_headings(self, sheet_name):
173+
raise NotImplementedError
174+
172175
def read_sheets(self):
173176
raise NotImplementedError
174177

@@ -190,6 +193,11 @@ def do_unflatten(self):
190193
sheets = [(self.main_sheet_name, self.get_main_sheet_lines())] + list(self.get_sub_sheets_lines())
191194
for i, sheet in enumerate(sheets):
192195
sheet_name, lines = sheet
196+
try:
197+
actual_headings = self.get_sheet_headings(sheet_name)
198+
except NotImplementedError:
199+
# The ListInput type used in the tests doesn't support getting headings.
200+
actual_headings = None
193201
for j, line in enumerate(lines):
194202
if all(x is None or x == '' for x in line.values()):
195203
#if all(x == '' for x in line.values()):
@@ -198,7 +206,10 @@ def do_unflatten(self):
198206
if WITH_CELLS:
199207
cells = OrderedDict()
200208
for k, header in enumerate(line):
201-
cells[header] = Cell(line[header], (sheet_name, _get_column_letter(k+1), j+2, header))
209+
if actual_headings:
210+
cells[header] = Cell(line[header], (sheet_name, _get_column_letter(k+1), j+2, actual_headings[k]))
211+
else:
212+
cells[header] = Cell(line[header], (sheet_name, _get_column_letter(k+1), j+2, header))
202213
unflattened = unflatten_main_with_parser(self.parser, cells, self.timezone)
203214
else:
204215
unflattened = unflatten_main_with_parser(self.parser, line, self.timezone)
@@ -241,10 +252,11 @@ def fancy_unflatten(self):
241252
cell_tree = self.do_unflatten()
242253
result = extract_list_to_value(cell_tree)
243254
cell_source_map = extract_list_to_error_path([self.main_sheet_name.lower()], cell_tree)
244-
ordered_cell_source_map = OrderedDict(( '/'.join(str(x) for x in path), location) for path, location in sorted(cell_source_map.items()))
255+
ordered_items = sorted(cell_source_map.items())
256+
ordered_cell_source_map = OrderedDict(( '/'.join(str(x) for x in path), location) for path, location in ordered_items)
245257
row_source_map = OrderedDict()
246258
heading_source_map = {}
247-
for path in cell_source_map:
259+
for path, _ in ordered_items:
248260
cells = cell_source_map[path]
249261
# Prepare row_source_map key
250262
key = '/'.join(str(x) for x in path[:-1])
@@ -327,6 +339,20 @@ def extract_dict_to_value(input):
327339
class CSVInput(SpreadsheetInput):
328340
encoding = 'utf-8'
329341

342+
def get_sheet_headings(self, sheet_name):
343+
if sys.version > '3': # If Python 3 or greater
344+
with open(os.path.join(self.input_name, sheet_name+'.csv'), encoding=self.encoding) as main_sheet_file:
345+
r = csvreader(main_sheet_file)
346+
for row in enumerate(r):
347+
# Just return the first row
348+
return row[1]
349+
else: # If Python 2
350+
with open(os.path.join(self.input_name, sheet_name+'.csv')) as main_sheet_file:
351+
r = csvreader(main_sheet_file, encoding=self.encoding)
352+
for row in enumerate(r):
353+
# Just return the first row
354+
return row[1]
355+
330356
def read_sheets(self):
331357
sheet_file_names = os.listdir(self.input_name)
332358
if self.main_sheet_name+'.csv' not in sheet_file_names:
@@ -367,6 +393,10 @@ def read_sheets(self):
367393
sheet_names.remove(self.main_sheet_name)
368394
self.sub_sheet_names = sheet_names
369395

396+
def get_sheet_headings(self, sheet_name):
397+
worksheet = self.workbook[self.sheet_names_map[sheet_name]]
398+
return [cell.value for cell in worksheet.rows[0]]
399+
370400
def get_sheet_lines(self, sheet_name):
371401
worksheet = self.workbook[self.sheet_names_map[sheet_name]]
372402
header_row = worksheet.rows[0]

0 commit comments

Comments
 (0)