Changeset - 8755d6af6700
[Not reviewed]
0 4 0
Brett Smith - 5 years ago 2019-07-04 14:15:09
brettcsmith@brettcsmith.org
ledger_entry: Support conditional expressions in amounts.
4 files changed with 45 insertions and 2 deletions:
0 comments (0 inline, 0 general)
README.rst
Show inline comments
...
 
@@ -44,3 +44,3 @@ The first line of the template is a Ledger tag.  The program will leave all kind
 

	
 
The next two lines split the money across accounts.  They follow almost the same format as they do in Ledger: there's an account named, followed by a tab or two or more spaces, and then an expression.  Each time import2ledger generates an entry, it will evaluate this expression using the data it imported to calculate the actual currency amount to write.  Your expression can use numbers, basic arithmetic operators (including parentheses for grouping), and imported data referred to as ``{variable_name}``.
 
The next two lines split the money across accounts.  They follow almost the same format as they do in Ledger: there's an account named, followed by a tab or two or more spaces, and then an expression.  Each time import2ledger generates an entry, it will evaluate this expression using the data it imported to calculate the actual currency amount to write.  Your expression can use numbers, basic arithmetic operators (including parentheses for grouping), conditional expressions in the format ``TRUE_EXPR if CONDITION else FALSE_EXPR``, and imported data referred to as ``{variable_name}``.
 

	
import2ledger/hooks/ledger_entry.py
Show inline comments
...
 
@@ -56,3 +56,17 @@ class TokenTransformer:
 
class AmountTokenTransformer(TokenTransformer):
 
    SUPPORTED_OPS = frozenset('+-*/()')
 
    SUPPORTED_NAMES = frozenset(['if', 'else'])
 
    SUPPORTED_OPS = frozenset([
 
        '(',
 
        ')',
 
        '+',
 
        '-',
 
        '*',
 
        '/',
 
        '==',
 
        '!=',
 
        '<',
 
        '<=',
 
        '>',
 
        '>=',
 
    ])
 

	
...
 
@@ -68,2 +82,8 @@ class AmountTokenTransformer(TokenTransformer):
 

	
 
    def transform_NAME(self, ttype, tvalue):
 
        if tvalue in self.SUPPORTED_NAMES:
 
            yield from self._noop_transformer(ttype, tvalue)
 
        else:
 
            raise ValueError("unsupported bare word {!r}".format(tvalue))
 

	
 
    def transform_NUMBER(self, ttype, tvalue):
tests/data/templates.ini
Show inline comments
...
 
@@ -49,2 +49,8 @@ template =
 

	
 
[Conditional]
 
template =
 
 Assets:Cash  {amount} - (6 if {amount} > 50 else 3)
 
 Expenses:Banking Fees  (6 if {amount} > 50 else 3)
 
 Income:Sales  -{amount}
 

	
 
[Empty]
tests/test_hook_ledger_entry.py
Show inline comments
...
 
@@ -184,2 +184,19 @@ def test_line1_not_custom_payee():
 

	
 
@pytest.mark.parametrize('amount,expect_fee', [
 
    (40, 3),
 
    (80, 6),
 
])
 
def test_conditional(amount, expect_fee):
 
    expect_cash = amount - expect_fee
 
    amount_s = '{:.02f}'.format(amount)
 
    render_vars = template_vars('Buyer', amount_s)
 
    lines = render_lines(render_vars, 'Conditional')
 
    assert lines == [
 
        "",
 
        "2015/03/14 Buyer",
 
        "  Assets:Cash  {:.02f} USD".format(expect_cash),
 
        "  Expenses:Banking Fees  {:.02f} USD".format(expect_fee),
 
        "  Income:Sales  -{} USD".format(amount_s),
 
    ]
 

	
 
@pytest.mark.parametrize('amount_expr', [
0 comments (0 inline, 0 general)