Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iconfig in CSV Importer categorizer call #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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