-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathofxwriter.py
executable file
·51 lines (43 loc) · 1.6 KB
/
ofxwriter.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
import itertools as it
from meza.io import IterStringIO
from csv2ofx import utils
from csv2ofx.ofx import OFX
from csv2ofx.mappings.default import mapping
from operator import itemgetter
from decimal import Decimal
import re
def ofx_export(file, transactions_list):
def convert_decimal_to_string(data):
if isinstance(data, dict):
for key, value in data.items():
data[key] = convert_decimal_to_string(value)
elif isinstance(data, list):
for i in range(len(data)):
data[i] = convert_decimal_to_string(data[i])
elif isinstance(data, Decimal):
data = str(data)
return data
convert_decimal_to_string(transactions_list)
mapping = {
'account': itemgetter('account'),
'date': itemgetter('date'),
'amount': itemgetter('amount'),
'payee': itemgetter('desc'),
'notes': itemgetter('memo'),
'class': itemgetter('transfer_account'),
'type': itemgetter('type')
}
ofx = OFX(mapping)
groups = ofx.gen_groups(transactions_list)
trxns = ofx.gen_trxns(groups)
cleaned_trxns = ofx.clean_trxns(trxns)
data = utils.gen_data(cleaned_trxns)
content = it.chain([ofx.header(), ofx.gen_body(data), ofx.footer()])
for ofxline in IterStringIO(content):
line = ofxline.decode("utf-8")
if transactions_list[0]["account"] == "Amex":
line = re.sub(r'<BANKID>.*?</BANKID>', '<BANKID>084000026</BANKID>', line)
print(line)
file.write(line)
if __name__ == "__main__":
print("Not to be used as stand-alone program.")