From f52b1b75aa1819fa5eec3e16206bc44e326d89e0 Mon Sep 17 00:00:00 2001 From: Red S Date: Fri, 14 Apr 2023 22:44:48 -0700 Subject: [PATCH] fix(opengroup): gracefully continue if a rule doesn't exist --- beancount_reds_plugins/opengroup/opengroup.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/beancount_reds_plugins/opengroup/opengroup.py b/beancount_reds_plugins/opengroup/opengroup.py index cfb1fa2..5dc825f 100644 --- a/beancount_reds_plugins/opengroup/opengroup.py +++ b/beancount_reds_plugins/opengroup/opengroup.py @@ -2,6 +2,7 @@ # flake8: noqa import re +import sys import time from ast import literal_eval from beancount.core import data @@ -46,8 +47,13 @@ } # type: ignore -def run_rule(rules, rulename, f_acct, f_ticker, f_opcurr): - rule, inserts = rules[rulename] +def run_rule(rules, rulename, f_acct, f_ticker, f_opcurr, entry): + try: + rule, inserts = rules[rulename] + except KeyError: + print(f"WARNING (opengroup): {rulename} not found in rules. " + f"{entry.meta['filename']}:{entry.meta['lineno']}", file=sys.stderr) + return [] components = re.search(rule, f_acct).groupdict() components.update(locals()) @@ -89,7 +95,7 @@ def opengroup(entries, options_map, config): oc, rule = m.split('_', 1) # Insert open entries for leaf in entry.meta[m].split(","): - for acc_params in run_rule(rules, rule, entry.account, leaf, op_currency): + for acc_params in run_rule(rules, rule, entry.account, leaf, op_currency, entry): meta = data.new_metadata(entry.meta["filename"], entry.meta["lineno"]) if oc == 'opengroup': new_entries.append(data.Open(meta, entry.date, *acc_params))