Skip to content

Commit 1dd01ce

Browse files
committed
[IMP] improve parsing introduce _post method for cleaning the data and manage correctly the date and amount
1 parent f78f22a commit 1dd01ce

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

cfonb/parser/common.py

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# python import
2+
from datetime import datetime
23
import math
34
import re
45

@@ -77,6 +78,14 @@ def __init__(self):
7778
self.re = re.compile(regex + r'$')
7879
self.keys = keys
7980

81+
def _post(self, res):
82+
for key in res:
83+
if key not in ['qualifier', 'additional_info']:
84+
res[key] = res[key].strip()
85+
return res
86+
87+
88+
8089
def parse(self, line):
8190
if line[-1] == "\n":
8291
line = line[:-1]
@@ -85,8 +94,8 @@ def parse(self, line):
8594
if match is None:
8695
raise ParsingError('line is invalid: "%s"' % line)
8796
else:
88-
return dict(zip(self.keys, list(match.groups())))
89-
97+
res = dict(zip(self.keys, list(match.groups())))
98+
return self._post(res)
9099

91100
# http://www.cfonb.org/Web/cfonb/cfonbmain.nsf/DocumentsByIDWeb/7JSHS5/$File/7-8%20Evolution%20Releve%20Comptes%20120%20caracteres%20operations%20virement%20et%20prelevement%20sepa%20V2_0_2010_03.pdf
92101

@@ -108,13 +117,20 @@ class ParserContent01(Parser):
108117
('_5', G__ % 16),
109118
]
110119

120+
def _post(self, res):
121+
res = super(ParserContent01, self)._post(res)
122+
res.update({
123+
'prev_amount': parse_amount(res['prev_amount'], res['nb_of_dec']),
124+
'prev_date': parse_date(res['prev_date']),
125+
})
126+
return res
111127

112128
class ParserContent04(Parser):
113129
_code = '04'
114130
_regex = [
115131
('record_code', '(04)' ),
116132
('bank_code', G_N % 5 ),
117-
('inernal_code', G_AN % 4 ),
133+
('internal_code', G_AN % 4 ),
118134
('desk_code', G_N % 5 ),
119135
('currency_code', G_A_ % 3 ),
120136
('nb_of_dec', G_N_ % 1 ),
@@ -133,13 +149,23 @@ class ParserContent04(Parser):
133149
('_4:', G_AN_ % 16),
134150
]
135151

152+
def _post(self, res):
153+
res = super(ParserContent04, self)._post(res)
154+
res.update({
155+
'amount': parse_amount(res['amount'], res['nb_of_dec']),
156+
'value_date': parse_date(res['value_date']),
157+
'operation_date': parse_date(res['operation_date']),
158+
})
159+
return res
160+
161+
136162

137163
class ParserContent05(Parser):
138164
_code = '05'
139165
_regex = [
140166
('record_code', '(05)'),
141167
('bank_code', G_N % 5 ),
142-
('inernal_code', G_AN % 4 ),
168+
('internal_code', G_AN % 4 ),
143169
('desk_code', G_N % 5 ),
144170
('currency_code', G_A_ % 3 ),
145171
('nb_of_dec', G_N_ % 1 ),
@@ -178,6 +204,14 @@ class ParserContent07(Parser):
178204
('_5', G__ % 16),
179205
]
180206

207+
def _post(self, res):
208+
res = super(ParserContent07, self)._post(res)
209+
res.update({
210+
'next_amount': parse_amount(res['next_amount'], res['nb_of_dec']),
211+
'next_date': parse_date(res['next_date']),
212+
})
213+
return res
214+
181215

182216
class ParserLIB(Parser):
183217
_code = "LIB"
@@ -295,6 +329,16 @@ class ParserMMO(Parser):
295329
('_', G_AN_ %39)
296330
]
297331

332+
def _post(self, res):
333+
res = super(ParserMMO, self)._post(res)
334+
res['equivalent_amount'] = float(res['equivalent_amount'])\
335+
/float(res['nb_of_dec_amount'])
336+
if res['exchange_rate']:
337+
res['exchange_rate']= float(res['exchange_rate'])\
338+
/float(res['nb_of_dec_exchange_rate'])
339+
return res
340+
341+
298342

299343
class ParserCBE(Parser):
300344
_code = 'CBE'
@@ -348,6 +392,8 @@ def parse_amount(amount_str, nb_of_dec):
348392
raise Exception('Bad amount string')
349393
return amount_num
350394

395+
def parse_date(date):
396+
return datetime.strptime(date, '%d%m%y').strftime('%Y-%m-%d')
351397

352398
def write_amount(amount, nb_of_dec):
353399
"""Returns a cfonb string for a numerical amount.

0 commit comments

Comments
 (0)