Skip to content

Instantly share code, notes, and snippets.

@asciipip
Created November 2, 2021 13:24
Show Gist options
  • Save asciipip/a579dc866a303471c2fa002cce8406ab to your computer and use it in GitHub Desktop.
Save asciipip/a579dc866a303471c2fa002cce8406ab to your computer and use it in GitHub Desktop.
beancount-import example
#!/usr/bin/env python3
import glob
import os
import beancount_import.webserver
## I keep my scripts in a subdirectory relative to the main beancount
## file, so the main directory is this script's directory's parent.
JOURNAL_DIR = os.path.dirname(os.path.dirname(__file__))
DATA_DIR = os.path.join(JOURNAL_DIR, 'data')
CACHE_DIR = os.path.join(JOURNAL_DIR, 'cache')
if __name__ == '__main__':
data_sources = [
## OFX data is in directories as "data/<institution>/<account-type>/<month>.ofx"
## e.g. "data/Bank-A/Checking/2021-10.ofx"
## The exception is one brokerage that gives me combined files for
## all of my accounts with them, plus they only do quarterly
## statements, so it has stuff like "data/Broker-B/2021-Q3.ofx"
dict(
module='beancount_import.source.ofx',
ofx_filenames=(
glob.glob(os.path.join(DATA_DIR, 'Bank-A/Checking/*.ofx')) \
+ glob.glob(os.path.join(DATA_DIR, 'Broker-B/*.ofx')) \
+ glob.glob(os.path.join(DATA_DIR, 'Bank-C/Credit-Card/*.ofx')) \
+ glob.glob(os.path.join(DATA_DIR, 'Bank-D/Savings/*.ofx')) \
+ glob.glob(os.path.join(DATA_DIR, 'Bank-D/Checking/*.ofx')) \
+ glob.glob(os.path.join(DATA_DIR, 'Bank-D/Credit-Card/*.ofx'))
),
cache_filename=os.path.join(CACHE_DIR, 'ofx_cache.pickle')
),
## Amazon data is in "data/Amazon/<order-number>.html". I download
## the invoice files from Amazon and then have a script that
## renames the files according to the order number. (The script
## also sets the file modification date to the order date.)
dict(
module='beancount_import.source.amazon',
directory=os.path.join(DATA_DIR, 'Amazon'),
amazon_account='[email protected]',
posttax_adjustment_accounts={
'Gift Card Amount': 'Assets:Cash:Amazon:Gift-Cards',
'Rewards Points': 'Assets:Cash:Chase:Rewards',
},
),
]
beancount_import.webserver.main(
[],
## My main beancount file is "main.beancount", which basically only
## has include directives to pull in all the other files I use.
journal_input=os.path.join(JOURNAL_DIR, 'main.beancount'),
## The ignored.beancount file is not included by main.beancount, so
## I can drop transactions here if I don't want them in my records
## for some reason.
ignored_journal=os.path.join(JOURNAL_DIR, 'ignored.beancount'),
## While I have fairly granular files for my hand-maintained data,
## I just have a single "journal.beancount" file for most imported
## data. I basically trat that file as write-only; I look at the
## transactions once, in the importer, and then never again.
default_output=os.path.join(JOURNAL_DIR, 'journal.beancount'),
## Account definitions are in the "chart-of-accounts.beancount"
## file. The importer adds new entries at the bottom and I
## periodically go in and sort the account definitions into
## appropriate sections earlier in the file.
open_account_output_map=[
('.*', os.path.join(JOURNAL_DIR, 'chart-of-accounts.beancount')),
],
## I don't actually use imported balance assertions; I enter them
## by hand from the bank statements. This setting is leftover
## from when I was initially setting up the importer.
balance_account_output_map=[
('Bank-A', os.path.join(JOURNAL_DIR, 'bank-a.beancount')),
('.*', os.path.join(JOURNAL_DIR, 'journal.beancount')),
],
data_sources=data_sources,
verbose=True,
dev=True,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment