""" Budget Parser """ from collections import namedtuple from .exceptions import BudgetParserError EXPECTED_TABLE_HEADERS = [ "Nr.", "Projekt", "Laufzeit", "BA", "Budget", "Ausgaben", "Rest", ] BudgetData = namedtuple( "BudgetData", [ "row", "number", "project_name", "period", "project", "budget", "expenses", "rest", ], ) def _check_table_header(row): fields = [c.strip() for c in row[:7]] print(fields) print(EXPECTED_TABLE_HEADERS) if fields != EXPECTED_TABLE_HEADERS: raise BudgetParserError(f"unexpected headers: '{row}'") def _skip_empty_lines(rows, start=0): for i, row in enumerate(rows, start=start): first_cell = row[0] if first_cell is None: continue if isinstance(first_cell, str) and first_cell.strip() == "": continue yield i, row def _parse_data_table(rows, start=2): for i, data in _skip_empty_lines(rows, start): yield BudgetData(i, *data[:7]) def parse_budget_data(xls_sheet): """ parses the budget data """ rows = xls_sheet.values _check_table_header(next(rows)) return list(_parse_data_table(rows, start=2))