Changeset - 3e21157f205b
[Not reviewed]
0 4 0
Brett Smith - 5 years ago 2019-07-29 16:37:42
brettcsmith@brettcsmith.org
ledger_entry: Support string tests in amount expressions.

This is useful for setting amounts based on imported strings like country,
Eventbrite ticket type, etc.
4 files changed with 46 insertions and 7 deletions:
0 comments (0 inline, 0 general)
README.rst
Show inline comments
...
 
@@ -44,3 +44,9 @@ 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), conditional expressions in the format ``TRUE_EXPR if CONDITION else FALSE_EXPR``, 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 and quoted strings
 
* imported data referred to as ``{variable_name}``
 
* basic arithmetic operators (including parentheses for grouping)
 
* the operators ``==``, ``!=``, ``<``, ``<=``, ``>``, ``>=``, ``and``, ``or``, ``not``, and ``in``
 
* conditional expressions in the format ``TRUE_EXPR if CONDITION else FALSE_EXPR``
 

	
import2ledger/hooks/ledger_entry.py
Show inline comments
...
 
@@ -56,3 +56,10 @@ class TokenTransformer:
 
class AmountTokenTransformer(TokenTransformer):
 
    SUPPORTED_NAMES = frozenset(['if', 'else'])
 
    SUPPORTED_NAMES = frozenset([
 
        'if',
 
        'else',
 
        'and',
 
        'or',
 
        'not',
 
        'in',
 
    ])
 
    SUPPORTED_OPS = frozenset([
...
 
@@ -112,4 +119,9 @@ class AmountTokenTransformer(TokenTransformer):
 

	
 
    transform_STRING = TokenTransformer._noop_transformer
 

	
 

	
 
class AccountSplitter:
 
    EVAL_GLOBALS = {
 
        'Decimal': decimal.Decimal,
 
    }
 
    TARGET_LINE_LEN = 78
...
 
@@ -168,9 +180,8 @@ class AccountSplitter:
 
    def _build_amounts(self, template_vars):
 
        amount_vars = {k: v for k, v in template_vars.items() if isinstance(v, decimal.Decimal)}
 
        amount_vars['Decimal'] = decimal.Decimal
 
        try:
 
            amounts = [
 
                (account, self._currency_decimal(eval(amount_expr, amount_vars),
 
                                                 template_vars['currency']))
 
                for account, amount_expr in self.splits
 
                (account,
 
                 self._currency_decimal(eval(amount_expr, self.EVAL_GLOBALS, template_vars),
 
                                        template_vars['currency']),
 
                ) for account, amount_expr in self.splits
 
            ]
tests/data/templates.ini
Show inline comments
...
 
@@ -55,2 +55,9 @@ template =
 

	
 
[StringConditional]
 
template =
 
 Income:Sales  {true} and -1
 
 Income:Sales  {false} or -2
 
 Income:Sales  -3 if 'x' not in {false} else -{amount}
 
 Assets:Cash   {amount}
 

	
 
[NondecimalWord]
tests/test_hook_ledger_entry.py
Show inline comments
...
 
@@ -201,2 +201,17 @@ def test_conditional(amount, expect_fee):
 

	
 
def test_string_conditionals():
 
    render_vars = template_vars('MM', '6', other_vars={
 
        'false': '',
 
        'true': 'true',
 
    })
 
    lines = render_lines(render_vars, 'StringConditional')
 
    assert lines == [
 
        "",
 
        "2015/03/14 MM",
 
        "  Income:Sales  -1.00 USD",
 
        "  Income:Sales  -2.00 USD",
 
        "  Income:Sales  -3.00 USD",
 
        "  Assets:Cash  6.00 USD",
 
    ]
 

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