Skip to content

Commit

Permalink
importers.csvbase: Allow finalize() to drop the current transaction
Browse files Browse the repository at this point in the history
Returning None from finalize() results in the current transaction
being discarded and in the source row to do not participate in the
determination of the balances.
  • Loading branch information
suqin-haha authored and dnicolodi committed Jan 12, 2025
1 parent 07a4073 commit 30286ac
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
8 changes: 7 additions & 1 deletion beangulp/importers/csvbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ def extract(self, filepath, existing):

# Apply user processing to the transaction.
txn = self.finalize(txn, row)
if txn is None:
continue

# Add the transaction to the output list.
entries.append(txn)
Expand Down Expand Up @@ -366,12 +368,16 @@ def metadata(self, filepath, lineno, row):
def finalize(self, txn, row):
"""Post process the transaction.
Returning None results in the transaction being discarded and
in source row to do not contribute to the determination of the
balances.
Args:
txn: The just build Transaction object.
row: The data row being processed.
Returns:
A potentially extended or modified Transaction object.
A potentially extended or modified Transaction object or None.
"""
return txn
21 changes: 21 additions & 0 deletions beangulp/importers/csvbase_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,24 @@ def finalize(self, txn, row):
Assets:CSV -1.00 EUR
Expenses:Testing 1.00 EUR
""")

@docfile
def test_extract_finalize_remove(self, filename):
"""\
2021-05-17, Test, -1.00, Testing
2021-05-18, Drop, -2.00, Testing
"""
class CSVImporter(Base):
date = Date(0)
narration = Column(1)
amount = Amount(2)
names = False

def finalize(self, txn, row):
if txn.narration == 'Drop':
return None
return txn

importer = CSVImporter('Assets:CSV', 'EUR')
entries = importer.extract(filename, [])
self.assertEqual(len(entries), 1)

0 comments on commit 30286ac

Please sign in to comment.