Skip to content

Instantly share code, notes, and snippets.

@mbafford
Created November 30, 2021 12:23
Show Gist options
  • Save mbafford/48159573504837855ce9bc08e4bba348 to your computer and use it in GitHub Desktop.
Save mbafford/48159573504837855ce9bc08e4bba348 to your computer and use it in GitHub Desktop.
Beancount - Fix payee/narration behavior to always have payee first and narration optional
#!python3
"""
Beancount has inconsistent behavior with payee/narrations.
If you have a line like this:
2020-01-01 * "Text1"
Then "Text1" is the narration
If you have a line like this:
2020-01-01 * "Text1" "Text2"
Then "Text1" is the payee, and "Text2" is the narration
-----
This plugin takes the cases where the text was entered with only a single string
and assumes the "narration" was actually meant to be the "payee". For my data, this
is always true.
"""
from beancount.core import data
__plugins__ = ('fix_payee_narration',)
def fix(entry):
if not isinstance(entry, data.Transaction):
return entry
# don't run if the entry is auto-added by beancount
if 'Padding inserted for Balance' in entry.narration:
return entry
if entry.narration and not entry.payee:
return entry._replace(payee=entry.narration, narration="")
return entry
def fix_payee_narration(entries, options_map=None, config=None):
ret = []
for entry in entries:
ret.append( fix(entry) )
return ret, []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment