-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstatement.py
More file actions
64 lines (51 loc) · 2.03 KB
/
statement.py
File metadata and controls
64 lines (51 loc) · 2.03 KB
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
# cfonb import
from .common import Row, ParsingError
class StatementReader(object):
"""Statement file parser. Parse file object to corresponding
row objects for further use. Offers useful method for reading, writing and
comparing issues.
"""
def parse(self, file_obj):
""" Parser a file object and return the list of bank statement
extracted from the file"""
file_lines = file_obj.readlines()
# content
result = {}
for index, line in enumerate(file_lines):
#print(f"at line number {index+1}")
if line[0:2] == '01':
row = Row(line)
#If a statement already exist for the same account
#we updated it else we create a new own
if result.get(row.account_nb):
statement = result[row.account_nb]
else:
statement = Statement()
result[row.account_nb] = statement
statement.header = row
statement.account_nb = row.account_nb
elif line[0:2] == '04':
row = Row(line)
statement.lines.append(row)
elif line[0:2] == '05':
new_row = Row(line)
if new_row.get('label') and row.get('label'):
index = 0
while True:
index += 1
if not row.get('label_%s'%index):
row['label_%s'%index] = new_row.label
break
else:
row.update(new_row)
elif line[0:2] == '07':
statement.footer = Row(line)
else:
raise ParsingError('line %s is invalid: "%s"' % (index, line))
return [result[key] for key in result]
class Statement(object):
def __init__(self):
self.header = None
self.footer = None
self.lines = list()
self.account_nb = None