Changeset - 093834dd15db
[Not reviewed]
0 3 0
Brett Smith - 7 years ago 2017-11-09 21:18:03
brettcsmith@brettcsmith.org
template: Support variables in account names.
3 files changed with 9 insertions and 6 deletions:
0 comments (0 inline, 0 general)
import2ledger/template.py
Show inline comments
...
 
@@ -127,26 +127,28 @@ class AccountSplitter:
 
        )
 
        self._balance_amounts(amounts, template_vars['amount'])
 
        self._balance_amounts(amounts, -template_vars['amount'])
 
        return amounts
 

	
 
    def _iter_splits(self, template_vars):
 
        amounts = self._build_amounts(template_vars)
 
        if template_vars['currency'] in self.signed_currencies:
 
            amt_fmt = self.signed_currency_fmt
 
        else:
 
            amt_fmt = self.unsigned_currency_fmt
 
        for account, amount in amounts.items():
 
            amt_s = babel.numbers.format_currency(amount, template_vars['currency'], amt_fmt)
 
            yield '    {:45}  {:>19}\n'.format(account, amt_s)
 
            yield '    {:45}  {:>19}\n'.format(
 
                account.format_map(template_vars),
 
                babel.numbers.format_currency(amount, template_vars['currency'], amt_fmt),
 
            )
 

	
 
    def render_next(self, template_vars):
 
        if template_vars is not self._last_template_vars:
 
            self._split_iter = self._iter_splits(template_vars)
 
            self._last_template_vars = template_vars
 
        return next(self._split_iter)
 

	
 

	
 
class Template:
 
    ACCOUNT_SPLIT_RE = re.compile(r'(?:\t|  )\s*')
 
    DATE_FMT = '%Y/%m/%d'
 
    SIGNED_CURRENCY_FMT = '¤#,##0.###;¤-#,##0.###'
tests/data/templates.ini
Show inline comments
...
 
@@ -6,24 +6,24 @@ template =
 
[FiftyFifty]
 
template =
 
 Accrued:Accounts Receivable  {amount}
 
 Income:Donations  -.5 * {amount}
 
 Income:Sales  -.5*{amount}
 

	
 
[Complex]
 
template =
 
 ;Tag: Value
 
 ;TransactionID: {txid}
 
 Accrued:Accounts Receivable  {amount}
 
 ;Entity: Supplier
 
 Income:Donations:Specific    -.955*  {amount}
 
 ;Program: Specific
 
 Income:Donations:{program}    -.955*  {amount}
 
 ;Program: {program}
 
 ;Entity: {entity}
 
 Income:Donations:General     -.045  * {amount}
 
 ;Entity: {entity}
 

	
 
[Multivalue]
 
template =
 
 Expenses:Taxes  {tax}
 
 Accrued:Accounts Receivable  {amount} - {tax}
 
 Income:RBI         -.1*{amount}
 
 Income:Donations   -.9*{amount}
tests/test_templates.py
Show inline comments
...
 
@@ -33,38 +33,39 @@ def test_easy_template():
 

	
 
def test_date_formatting():
 
    tmpl = template_from('Simplest', date_fmt='%Y-%m-%d')
 
    assert_easy_render(tmpl, 'KK', '6.99', 'CAD', '2015-03-14', '6.99 CAD')
 

	
 
def test_currency_formatting():
 
    tmpl = template_from('Simplest', signed_currencies=['USD'])
 
    assert_easy_render(tmpl, 'CC', '7.99', 'USD', '2015/03/14', '$7.99')
 

	
 
def test_complex_template():
 
    template_vars = {
 
        'entity': 'T-T',
 
        'program': 'Spectrum Defense',
 
        'txid': 'ABCDEF',
 
    }
 
    tmpl = template_from('Complex', date_fmt='%Y-%m-%d', signed_currencies=['USD'])
 
    rendered = tmpl.render('TT', decimal.Decimal('125.50'), 'USD', DATE, **template_vars)
 
    lines = [normalize_whitespace(s) for s in rendered.splitlines()]
 
    assert lines == [
 
        "",
 
        "2015-03-14 TT",
 
        "  ;Tag: Value",
 
        "  ;TransactionID: ABCDEF",
 
        "  Accrued:Accounts Receivable  $125.50",
 
        "  ;Entity: Supplier",
 
        "  Income:Donations:Specific  $-119.85",
 
        "  ;Program: Specific",
 
        "  Income:Donations:Spectrum Defense  $-119.85",
 
        "  ;Program: Spectrum Defense",
 
        "  ;Entity: T-T",
 
        "  Income:Donations:General  $-5.65",
 
        "  ;Entity: T-T",
 
    ]
 

	
 
def test_balancing():
 
    tmpl = template_from('FiftyFifty')
 
    rendered = tmpl.render('FF', decimal.Decimal('1.01'), 'USD', DATE)
 
    lines = [normalize_whitespace(s) for s in rendered.splitlines()]
 
    assert lines == [
 
        "",
 
        "2015/03/14 FF",
0 comments (0 inline, 0 general)