Skip to content

Commit

Permalink
fix(opengroup): gracefully continue if a rule doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
redstreet committed Apr 15, 2023
1 parent cac32e0 commit f52b1b7
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions beancount_reds_plugins/opengroup/opengroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# flake8: noqa
import re
import sys
import time
from ast import literal_eval
from beancount.core import data
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit f52b1b7

Please sign in to comment.