41 lines
1.3 KiB
Python
41 lines
1.3 KiB
Python
#!/usr/bin/env python3
|
|
|
|
from beancount.core import data
|
|
from beancount.core import account
|
|
from beancount.core import getters
|
|
from beancount.core import amount
|
|
from beancount.core import account_types
|
|
from beancount.core import realization
|
|
from beancount.core.number import D
|
|
from beancount.parser import options
|
|
|
|
import re
|
|
|
|
__plugins__ = ('optional_balance_plugin',)
|
|
|
|
def optional_balance_plugin(entries, options_map, config):
|
|
try:
|
|
match_enabled = re.compile(config)
|
|
except Exception as e:
|
|
print(f"exception {e}")
|
|
return entries, [e]
|
|
|
|
errors = []
|
|
new_entries = []
|
|
for entry in entries:
|
|
if isinstance(entry, data.Custom):
|
|
if entry.type == "optional-balance":
|
|
try:
|
|
assert(len(entry.values) == 4)
|
|
type, acc, units, curr = entry.values
|
|
if not match_enabled.match(type.value):
|
|
continue
|
|
v = amount.Amount(number=D(units.value), currency=curr.value)
|
|
entry = data.Balance(meta=entry.meta, date=entry.date, account=acc.value, amount=v, tolerance=None, diff_amount=None)
|
|
except Exception as e:
|
|
print(f"exception {e}")
|
|
errors.append(e)
|
|
|
|
new_entries.append(entry)
|
|
|
|
return new_entries, errors |