hooks.ledger_entry: Improve date handling.

Check that we have the date field used in the payee line, and not just
'date'.
Allow other date fields to be None since they may not be used by the
template.
This commit is contained in:
Brett Smith 2019-11-11 10:18:21 -05:00
parent 7156529ceb
commit 8516134687
3 changed files with 24 additions and 9 deletions

View file

@ -224,7 +224,7 @@ class AccountSplitter:
class Template: class Template:
ACCOUNT_SPLIT_RE = re.compile(r'(?:\t| )\s*') ACCOUNT_SPLIT_RE = re.compile(r'(?:\t| )\s*')
DATE_FMT = '%Y/%m/%d' DATE_FMT = '%Y/%m/%d'
PAYEE_LINE_RE = re.compile(r'\{(\w*_)*date\}') PAYEE_LINE_RE = re.compile(r'^\{(\w*_)*date\}\s')
SIGNED_CURRENCY_FMT = '¤#,##0.###;¤-#,##0.###' SIGNED_CURRENCY_FMT = '¤#,##0.###;¤-#,##0.###'
UNSIGNED_CURRENCY_FMT = '#,##0.### ¤¤' UNSIGNED_CURRENCY_FMT = '#,##0.### ¤¤'
@ -234,6 +234,7 @@ class Template:
unsigned_currency_fmt=UNSIGNED_CURRENCY_FMT, unsigned_currency_fmt=UNSIGNED_CURRENCY_FMT,
template_name='<template>'): template_name='<template>'):
self.date_fmt = date_fmt self.date_fmt = date_fmt
self.date_field = 'date'
self.splitter = AccountSplitter( self.splitter = AccountSplitter(
signed_currencies, signed_currency_fmt, unsigned_currency_fmt, template_name) signed_currencies, signed_currency_fmt, unsigned_currency_fmt, template_name)
@ -273,7 +274,9 @@ class Template:
line1 = next(lines) line1 = next(lines)
except StopIteration: except StopIteration:
return return
if self.PAYEE_LINE_RE.match(line1): match = self.PAYEE_LINE_RE.match(line1)
if match:
self.date_field = match.group(0)[1:-2]
yield '\n' + line1 yield '\n' + line1
else: else:
yield '\n{date} {payee}' yield '\n{date} {payee}'
@ -293,16 +296,16 @@ class Template:
# template_vars must have these keys. Raise a KeyError if not. # template_vars must have these keys. Raise a KeyError if not.
template_vars['currency'] template_vars['currency']
template_vars['payee'] template_vars['payee']
try: if template_vars.get(self.date_field) is None:
date = template_vars['date'] raise errors.UserInputConfigurationError(
except KeyError: "entry needs {} field but that's not set by the importer".format(
date = datetime.date.today() self.date_field,
), self.splitter.template_name)
render_vars = { render_vars = {
'amount': strparse.currency_decimal(template_vars['amount']), 'amount': strparse.currency_decimal(template_vars['amount']),
'date': date.strftime(self.date_fmt),
} }
for key, value in template_vars.items(): for key, value in template_vars.items():
if key.endswith('_date'): if value is not None and (key == 'date' or key.endswith('_date')):
render_vars[key] = value.strftime(self.date_fmt) render_vars[key] = value.strftime(self.date_fmt)
all_vars = collections.ChainMap(render_vars, template_vars) all_vars = collections.ChainMap(render_vars, template_vars)
return ''.join(f(all_vars) for f in self.format_funcs) return ''.join(f(all_vars) for f in self.format_funcs)

View file

@ -30,7 +30,7 @@ REQUIREMENTS['tests_require'] = [
setup( setup(
name='import2ledger', name='import2ledger',
description="Import different sources of financial data to Ledger", description="Import different sources of financial data to Ledger",
version='0.10.0', version='0.10.1',
author='Brett Smith', author='Brett Smith',
author_email='brettcsmith@brettcsmith.org', author_email='brettcsmith@brettcsmith.org',
license='GNU AGPLv3+', license='GNU AGPLv3+',

View file

@ -182,6 +182,18 @@ def test_line1_not_custom_payee():
" Income:Donations -15.00 USD", " Income:Donations -15.00 USD",
] ]
def test_custom_date_missing():
render_vars = template_vars('YY', '20.00')
with pytest.raises(errors.UserInputConfigurationError):
render_lines(render_vars, 'Custom Payee')
def test_custom_date_is_none():
render_vars = template_vars('YZ', '25.00', other_vars={
'custom_date': None,
})
with pytest.raises(errors.UserInputConfigurationError):
render_lines(render_vars, 'Custom Payee')
@pytest.mark.parametrize('amount,expect_fee', [ @pytest.mark.parametrize('amount,expect_fee', [
(40, 3), (40, 3),
(80, 6), (80, 6),