-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
47 lines (35 loc) · 1.37 KB
/
main.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
import clize
import csv
from datetime import datetime
REVOLUT_ACCOUNT = 'Assets:Revolut:Euro'
def format_date(revolut_stamp):
in_fmt = '%d %b %Y'
out_fmt = '%Y/%m/%d'
return (datetime.strptime(revolut_stamp.strip(), in_fmt)
.strftime(out_fmt))
def to_hledger(is_incoming, date, ref, amount, replace_comma=False):
to_acc, from_acc = ((REVOLUT_ACCOUNT, f'Income:{ref}')
if is_incoming
else (f'Expenses:{ref}', REVOLUT_ACCOUNT))
if replace_comma:
amount = amount.replace(",", ".")
neg_amount = f"-{amount}"
return f"""{date} {ref.strip()}
{to_acc:34} {amount:>10}
{from_acc:34} {neg_amount:>10}\n"""
def parse(revolut_csv):
for date, ref, paid_out, paid_in, _exch_out, _exch_in, _bal, *opt in revolut_csv:
if len(opt) >= 2:
ref = f"{ref.strip()} ({opt[1].strip()})"
is_incoming = bool(paid_in)
stamp = format_date(date)
amount = paid_in.strip() if is_incoming else paid_out.strip()
yield is_incoming, stamp, ref, amount
def main(infile, *, replace_comma=False):
with open(infile, 'r') as csvfile:
lines = reversed(list(csvfile)[1:])
reader = csv.reader(lines, delimiter=';')
for entry in parse(reader):
print(to_hledger(*entry, replace_comma))
if __name__ == '__main__':
clize.run(main)