From 9e98d9fd0644f0d66db82127beda647856859ea3 Mon Sep 17 00:00:00 2001 From: Jorge Martinez Date: Sun, 15 May 2022 17:13:50 -0400 Subject: [PATCH] iconfig in csv categorizer --- beangulp/importers/csv.py | 10 ++++++---- beangulp/importers/csv_test.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/beangulp/importers/csv.py b/beangulp/importers/csv.py index a1b4842d..26781fe7 100644 --- a/beangulp/importers/csv.py +++ b/beangulp/importers/csv.py @@ -215,7 +215,7 @@ def __init__(self, config, account, currency, regexps: A list of regular expression strings. skip_lines: Skip first x (garbage) lines of file. last4_map: A dict that maps last 4 digits of the card to a friendly string. - categorizer: A callable with two arguments (transaction, row) that can attach + categorizer: A callable with 3 args (transaction, row, iconfig) that can attach the other posting (usually expenses) to a transaction with only single posting. institution: An optional name of an institution to rename the files to. debug: Whether or not to print debug information @@ -377,7 +377,7 @@ def get(row, ftype): data.Posting(account, units, None, None, None, None)) # Attach the other posting(s) to the transaction. - txn = self.call_categorizer(txn, row) + txn = self.call_categorizer(txn, row, iconfig) # Add the transaction to the output list entries.append(txn) @@ -411,7 +411,7 @@ def get(row, ftype): return entries - def call_categorizer(self, txn, row): + def call_categorizer(self, txn, row, iconfig): if not isinstance(self.categorizer, collections.abc.Callable): return txn @@ -420,7 +420,9 @@ def call_categorizer(self, txn, row): params = signature(self.categorizer).parameters if len(params) < 2: return self.categorizer(txn) - return self.categorizer(txn, row) + elif len(params) < 3: + return self.categorizer(txn, row) + return self.categorizer(txn, row, iconfig) def parse_amount(self, string): """The method used to create Decimal instances. You can override this.""" diff --git a/beangulp/importers/csv_test.py b/beangulp/importers/csv_test.py index 2814e999..1fa0f638 100644 --- a/beangulp/importers/csv_test.py +++ b/beangulp/importers/csv_test.py @@ -490,6 +490,39 @@ def categorizer(txn, row): Assets:Bank -25.00 EUR """, entries) + @test_utils.docfile + def test_categorizer_three_arguments(self, filename): + """\ + Date,Amount,Payee,Description + 6/2/2020,30.00,"Payee here","Description" + 7/2/2020,-25.00,"Supermarket","Groceries" + """ + def categorizer(txn, row, iconfig): + txn = txn._replace(payee=row[iconfig[Col.PAYEE]]) + txn.meta['source'] = pformat(row) + return txn + + importer = csv.CSVImporter({Col.DATE: 'Date', + Col.NARRATION: 'Description', + Col.PAYEE: 'Payee', + Col.AMOUNT: 'Amount'}, + 'Assets:Bank', + 'EUR', + ('Date,Amount,Payee,Description'), + categorizer=categorizer, + institution='foobar') + entries = importer.extract(filename) + self.assertEqualEntries(r""" + + 2020-06-02 * "Payee here" "Description" + source: "['6/2/2020', '30.00', 'Supermarket', 'Groceries']" + Assets:Bank 30.00 EUR + + 2020-07-02 * "Supermarket" "Groceries" + source: "['7/2/2020', '-25.00', 'Supermarket', 'Groceries']" + Assets:Bank -25.00 EUR + """, entries) + @test_utils.docfile def test_explict_encoding_utf8(self, filename): """\