diff --git a/README.rst b/README.rst index 5d8fe53..9c01058 100644 --- a/README.rst +++ b/README.rst @@ -42,7 +42,13 @@ Every setting in your configuration file has to be in a section. ``[DEFAULT]`` The first line of the template is a Ledger tag. The program will leave all kinds of tags and Ledger comments alone, except to indent them nicely. -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 uses decimal math to calculate each amount, and rounds to the number of digits appropriate for that currency. If the amount of currency being imported doesn't split evenly, spare change will be allocated to the last split to keep the entry balanced on both sides. diff --git a/import2ledger/hooks/ledger_entry.py b/import2ledger/hooks/ledger_entry.py index 17ff7f2..f67a811 100644 --- a/import2ledger/hooks/ledger_entry.py +++ b/import2ledger/hooks/ledger_entry.py @@ -54,7 +54,14 @@ class TokenTransformer: class AmountTokenTransformer(TokenTransformer): - SUPPORTED_NAMES = frozenset(['if', 'else']) + SUPPORTED_NAMES = frozenset([ + 'if', + 'else', + 'and', + 'or', + 'not', + 'in', + ]) SUPPORTED_OPS = frozenset([ '(', ')', @@ -110,8 +117,13 @@ class AmountTokenTransformer(TokenTransformer): else: raise ValueError("unsupported operator {!r}".format(tvalue)) + transform_STRING = TokenTransformer._noop_transformer + class AccountSplitter: + EVAL_GLOBALS = { + 'Decimal': decimal.Decimal, + } TARGET_LINE_LEN = 78 # -4 because that's how many spaces prefix an account line. TARGET_ACCTLINE_LEN = TARGET_LINE_LEN - 4 @@ -166,13 +178,12 @@ class AccountSplitter: amounts[balance_index] = (account_name, start_amount + remainder) 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 ] except (ArithmeticError, NameError, TypeError, ValueError) as error: raise errors.UserInputConfigurationError( diff --git a/tests/data/templates.ini b/tests/data/templates.ini index 5b8ce60..e7b7b71 100644 --- a/tests/data/templates.ini +++ b/tests/data/templates.ini @@ -53,6 +53,13 @@ template = Expenses:Banking Fees (6 if {amount} > 50 else 3) Income:Sales -{amount} +[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] template = Income:Sales -5 diff --git a/tests/test_hook_ledger_entry.py b/tests/test_hook_ledger_entry.py index ffa7235..d1c5213 100644 --- a/tests/test_hook_ledger_entry.py +++ b/tests/test_hook_ledger_entry.py @@ -199,6 +199,21 @@ def test_conditional(amount, expect_fee): " Income:Sales -{} USD".format(amount_s), ] +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', [ '', 'name',