|
|
|
@ -14,6 +14,9 @@ EXPECTED_TABLE_HEADERS = [
@@ -14,6 +14,9 @@ EXPECTED_TABLE_HEADERS = [
|
|
|
|
|
"Rest", |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ExcelRow = namedtuple("ExcelRow", ["row", "data"]) |
|
|
|
|
|
|
|
|
|
BudgetData = namedtuple( |
|
|
|
|
"BudgetData", |
|
|
|
|
[ |
|
|
|
@ -29,31 +32,31 @@ BudgetData = namedtuple(
@@ -29,31 +32,31 @@ BudgetData = namedtuple(
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _check_table_header(row): |
|
|
|
|
fields = [c.strip() for c in row[:7]] |
|
|
|
|
def _check_table_header(xl_row): |
|
|
|
|
fields = [c.strip() for c in xl_row.data[:7]] |
|
|
|
|
print(fields) |
|
|
|
|
print(EXPECTED_TABLE_HEADERS) |
|
|
|
|
if fields != EXPECTED_TABLE_HEADERS: |
|
|
|
|
raise BudgetParserError(f"unexpected headers: '{row}'") |
|
|
|
|
raise BudgetParserError(f"unexpected headers: '{xl_row.data}'") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _skip_empty_lines(rows, start=0): |
|
|
|
|
for i, row in enumerate(rows, start=start): |
|
|
|
|
first_cell = row[0] |
|
|
|
|
def _skip_empty_lines(rows): |
|
|
|
|
for xl_row in rows: |
|
|
|
|
first_cell = xl_row.data[0] |
|
|
|
|
if first_cell is None: |
|
|
|
|
continue |
|
|
|
|
if isinstance(first_cell, str) and first_cell.strip() == "": |
|
|
|
|
continue |
|
|
|
|
yield i, row |
|
|
|
|
yield xl_row |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_data_table(rows, start=2): |
|
|
|
|
for i, data in _skip_empty_lines(rows, start): |
|
|
|
|
yield BudgetData(i, *data[:7]) |
|
|
|
|
def _parse_data_table(rows): |
|
|
|
|
for xl_row in _skip_empty_lines(rows): |
|
|
|
|
yield BudgetData(xl_row.row, *xl_row.data[:7]) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def parse_budget_data(xls_sheet): |
|
|
|
|
""" parses the budget data """ |
|
|
|
|
rows = xls_sheet.values |
|
|
|
|
rows = (ExcelRow(i, v) for i, v in enumerate(xls_sheet.values, start=1)) |
|
|
|
|
_check_table_header(next(rows)) |
|
|
|
|
return list(_parse_data_table(rows, start=2)) |
|
|
|
|
return list(_parse_data_table(rows)) |
|
|
|
|