def parse_csv(self, csv_file): trans = [] assertion_desc = 'expecting row with account,date,description,debit,credit,commodity. got: %s' for line in csv_file.readlines(): line = line.decode('utf8') fields = self.parse_record(line) assert len(fields) == 6, assertion_desc % fields fields = self.clean_record(fields) raw_account = fields[0] raw_date = fields[1] raw_desc = fields[2] debit = parserutil.parse_float(fields[3]) credit = parserutil.parse_float(fields[4]) commodity = fields[5] date_parts = parserutil.parse_date(raw_date) t = transaction.LedgerTransaction() t.add_description(bidi.fix_rtl(raw_desc)) t.add_date(date_parts[2], date_parts[1], date_parts[0]) t.add_commodity(commodity) ambiguous_assert = 'either debit or credit have value: row %s' % line ambiguous_assert = str(ambiguous_assert.encode('utf8')) assert debit == 0 or credit == 0, ambiguous_assert assert debit > 0 or credit > 0, ambiguous_assert if debit > 0: t.add_debit_account(raw_account) t.add_credited_account(u'') t.add_amount(debit) if credit > 0: t.add_credited_account(raw_account) t.add_debit_account(u'') t.add_amount(credit) trans.append(t) return trans
def _parse_xml(self): trans = [] for row in self._get_rows(): row_as_string = xml.etree.ElementTree.tostring(row, encoding="utf8") assertion_desc = "expecting row with account,date,description,debit,credit,commodity. got: %s" assertion_desc = assertion_desc % row_as_string assert len(list(row)) == 6, assertion_desc clean_row = [el[0].text.encode("utf-8").decode("utf-8", "ignore") for el in row] raw_account = clean_row[0] raw_date = clean_row[1] raw_desc = clean_row[2] debit = parserutil.parse_float(clean_row[3]) credit = parserutil.parse_float(clean_row[4]) commodity = clean_row[5] date_parts = parserutil.parse_date(raw_date) t = transaction.LedgerTransaction() t.add_description(bidi.fix_rtl(raw_desc)) t.add_date(date_parts[2], date_parts[1], date_parts[0]) t.add_commodity(commodity) ambiguous_assert = "either debit or credit have value: row %s" % row_as_string ambiguous_assert = str(ambiguous_assert.encode("utf8")) assert debit == 0 or credit == 0, row_as_string assert debit > 0 or credit > 0, row_as_string if debit > 0: t.add_debit_account(raw_account) t.add_credited_account(u"") t.add_amount(debit) if credit > 0: t.add_credited_account(raw_account) t.add_debit_account(u"") t.add_amount(credit) trans.append(t) return trans