-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledger2beancount.py
39 lines (32 loc) · 1.15 KB
/
ledger2beancount.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/python
import ledger
import sys
import re
def account_name(post):
account = post.account.fullname().replace(" ","").replace("(","").replace(")","").replace("'","")
return re.sub(r'\:(\d)',r':X\1', account)
def get_symbol(amount):
symbol = amount.commodity.symbol.replace("-","").replace("\"","").upper()
if symbol == "$":
symbol = "USD"
return symbol
filename = sys.argv[1]
accounts = set()
for xact in ledger.read_journal(filename).xacts():
for post in xact.posts():
account = account_name(post)
if account not in accounts:
print "%s open %s" % (xact.date, account)
accounts.add(account)
print "%s * \"%s\"" % (xact.date, xact.payee)
for post in xact.posts():
account = account_name(post)
symbol = get_symbol(post.amount)
if post.amount.has_annotation():
price = post.amount.price()
if post.amount.number() != 0:
price = price / post.amount.number()
psym = get_symbol(price)
print " %-50s %s %s @ %s %s" % (account, post.amount.number(), symbol, price.number(), psym)
else:
print " %-50s %s %s" % (account, post.amount.number(), symbol)