Skip to content

Commit

Permalink
iconfig in csv categorizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Martinez committed May 15, 2022
1 parent 84a3486 commit 9e98d9f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
10 changes: 6 additions & 4 deletions beangulp/importers/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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."""
Expand Down
33 changes: 33 additions & 0 deletions beangulp/importers/csv_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""\
Expand Down

0 comments on commit 9e98d9f

Please sign in to comment.