forked from kedder/ofxstatement-sample
/
ingbe.py
70 lines (55 loc) · 2.07 KB
/
ingbe.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import csv
from ofxstatement import statement
from ofxstatement.plugin import Plugin
from ofxstatement.parser import CsvStatementParser
from ofxstatement.parser import StatementParser
from ofxstatement.statement import StatementLine
class IngBePlugin(Plugin):
"""ING Belgium Plugin
"""
def get_parser(self, filename):
f = open(filename, 'r', encoding=self.settings.get("charset", "ISO-8859-1"))
parser = IngBeParser(f)
return parser
class IngBeParser(CsvStatementParser):
date_format = "%d/%m/%Y"
mappings = {
'check_no': 3,
'date': 4,
'payee': 2,
'memo': 9,
'amount': 6
}
def parse(self):
"""Main entry point for parsers
super() implementation will call to split_records and parse_record to
process the file.
"""
stmt = super(IngBeParser, self).parse()
statement.recalculate_balance(stmt)
return stmt
def split_records(self):
"""Return iterable object consisting of a line per transaction
"""
reader = csv.reader(self.fin)
next(reader, None)
return reader
def parse_record(self, line):
"""Parse given transaction line and return StatementLine object
"""
# Remove non CSV cr*p and zero-value notifications
if(line[5] and not (line[6]=="0" and line[7]=="0")):
transaction_id = line[3]
date = line[4]
date_value = line[5]
account_to = line[2]
currency = line[8]
# fix amount - Well done ING, mixing the comma as decimal separator and as CSV delimiter. Way to go...
line[6] = line[6]+"."+line[7]
amount = line[6]
# Pack info in description
line[9] = line[9]+"-"+line[10]+"-"+line[11]
description = line[9]
stmtline = super(IngBeParser, self).parse_record(line)
stmtline.trntype = 'DEBIT' if stmtline.amount < 0 else 'CREDIT'
return stmtline