-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsfin2ledger.py
56 lines (50 loc) · 1.77 KB
/
sfin2ledger.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import sys
import json
import argparse
from decimal import Decimal
from datetime import datetime
def simplefin2Ledger(intext):
data = json.loads(intext)
err = ''
all_trans = []
for account in data['accounts']:
for transaction in account['transactions']:
transaction['account'] = account
all_trans.append(transaction)
all_trans = sorted(all_trans, key=lambda x:x['posted'])
lines = []
for trans in all_trans:
posted = datetime.fromtimestamp(trans['posted']).strftime('%Y/%m/%d')
lines.append('{0} {1}'.format(posted, trans['description']))
amount = Decimal(trans['amount'])
approx_width = 40
if amount > 0:
# income
name = 'Assets:{0}'.format(trans['account']['name'])
amount = '${0}'.format(abs(amount))
space = ' '*(approx_width-len(name)-len(amount))
lines.append(' {name}{space}{amount}'.format(
name=name,
space=space,
amount=amount))
lines.append(' Income:UNKNOWN')
else:
# expense
name = 'Expenses:UNKNOWN'
amount = '${0}'.format(abs(amount))
space = ' '*(approx_width-len(name)-len(amount))
lines.append(' {name}{space}{amount}'.format(
name=name,
space=space,
amount=amount))
lines.append(' Assets:{0}'.format(trans['account']['name']))
lines.append('')
lines.append('')
return '\n'.join(lines), err
ap = argparse.ArgumentParser('ledger-pull')
def run(args=None):
args = ap.parse_args(args)
stdin = sys.stdin.read()
out, err = simplefin2Ledger(stdin)
sys.stdout.write(out)
sys.stderr.write(err)