Skip to content

Commit 526b2b6

Browse files
committed
[REF] refactor the way to generate the regex and improve error message
1 parent e2a7832 commit 526b2b6

File tree

1 file changed

+108
-89
lines changed

1 file changed

+108
-89
lines changed

cfonb/parser/common.py

Lines changed: 108 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,13 @@ def __init__(self):
7171
self._regex
7272
regex = r''
7373
keys = []
74-
for key, value in self._regex:
75-
regex += value
74+
self.size = 0
75+
for key, value, size in self._regex:
76+
self.size += size
77+
if '%d' in value:
78+
regex += value % size
79+
else:
80+
regex += value
7681
keys += [key]
7782
self.re = re.compile(regex + r'$')
7883
self.keys = keys
@@ -88,10 +93,24 @@ def _post(self, res):
8893
def parse(self, line):
8994
if line[-1] == "\n":
9095
line = line[:-1]
96+
if len(line) != self.size:
97+
raise ParsingError("Invalid line: %s. the len should be %s"
98+
"instead of %s"%self.size, len(line))
9199
match = self.re.match(line)
92100
# re check
93101
if match is None:
94-
raise ParsingError('line is invalid: "%s"' % line)
102+
message= ''
103+
index = 0
104+
for key, value, size in self._regex:
105+
pattern = '%d' in value and value % size or value
106+
if not re.match(pattern, line[:size]):
107+
message += ("The key %s in position %s with the pattern %s"
108+
"do not match with %s\n"
109+
%(key, index, pattern, line[:size]))
110+
index += size
111+
line = line[size:]
112+
raise ParsingError("Invalid line: %s. Please read detail"
113+
"message\n %s" %(line, message))
95114
else:
96115
res = dict(zip(self.keys, list(match.groups())))
97116
return self._post(res)
@@ -101,19 +120,19 @@ def parse(self, line):
101120
class ParserContent01(Parser):
102121
_code = '01'
103122
_regex = [
104-
('record_code', '(01)' ),
105-
('bank_code', G_N % 5 ),
106-
('_1', G__ % 4 ),
107-
('desk_code', G_N % 5 ),
108-
('currency_code', G_A_ % 3 ),
109-
('nb_of_dec', G_N_ % 1 ),
110-
('_2', G__ % 1 ),
111-
('account_nb', G_AN % 11),
112-
('_3', G__ % 2 ),
113-
('prev_date', G_N % 6 ),
114-
('_4', G__ % 50),
115-
('prev_amount', G_AMT ),
116-
('_5', G__ % 16),
123+
('record_code', '(01)', 2),
124+
('bank_code', G_N, 5),
125+
('_1', G__, 4),
126+
('desk_code', G_N, 5),
127+
('currency_code', G_A_, 3),
128+
('nb_of_dec', G_N_, 1),
129+
('_2', G__, 1),
130+
('account_nb', G_AN, 11),
131+
('_3', G__, 2),
132+
('prev_date', G_N, 6),
133+
('_4', G__, 50),
134+
('prev_amount', G_AMT, 14),
135+
('_5', G__, 16),
117136
]
118137

119138
def _post(self, res):
@@ -127,25 +146,25 @@ def _post(self, res):
127146
class ParserContent04(Parser):
128147
_code = '04'
129148
_regex = [
130-
('record_code', '(04)' ),
131-
('bank_code', G_N % 5 ),
132-
('internal_code', G_AN % 4 ),
133-
('desk_code', G_N % 5 ),
134-
('currency_code', G_A_ % 3 ),
135-
('nb_of_dec', G_N_ % 1 ),
136-
('_1', G_ALL % 1 ),
137-
('account_nb', G_AN % 11),
138-
('operation_code', G_AN % 2 ),
139-
('operation_date', G_N % 6 ),
140-
('reject_code', G_N_ % 2 ),
141-
('value_date', G_N % 6 ),
142-
('label', G_ALL % 31),
143-
('_2', G_ALL % 2 ),
144-
('reference', G_ALL % 7 ),
145-
('exempt_code', G_ALL % 1 ),
146-
('_3', G_ALL % 1 ),
147-
('amount', G_AMT ),
148-
('_4:', G_ALL % 16),
149+
('record_code', '(04)', 2),
150+
('bank_code', G_N, 5),
151+
('internal_code', G_AN, 4),
152+
('desk_code', G_N, 5),
153+
('currency_code', G_A_, 3),
154+
('nb_of_dec', G_N_, 1),
155+
('_1', G_ALL, 1),
156+
('account_nb', G_AN, 11),
157+
('operation_code', G_AN, 2),
158+
('operation_date', G_N, 6),
159+
('reject_code', G_N_, 2),
160+
('value_date', G_N, 6),
161+
('label', G_ALL, 31),
162+
('_2', G_ALL, 2),
163+
('reference', G_ALL, 7),
164+
('exempt_code', G_ALL, 1),
165+
('_3', G_ALL, 1),
166+
('amount', G_AMT, 14),
167+
('_4:', G_ALL, 16),
149168
]
150169

151170
def _post(self, res):
@@ -162,20 +181,20 @@ def _post(self, res):
162181
class ParserContent05(Parser):
163182
_code = '05'
164183
_regex = [
165-
('record_code', '(05)'),
166-
('bank_code', G_N % 5 ),
167-
('internal_code', G_AN % 4 ),
168-
('desk_code', G_N % 5 ),
169-
('currency_code', G_A_ % 3 ),
170-
('nb_of_dec', G_N_ % 1 ),
171-
('_1', G_AN_ % 1 ),
172-
('account_nb', G_AN % 11),
173-
('operation_code', G_AN % 2 ),
174-
('operation_date', G_N % 6 ),
175-
('_2', G__ % 5 ),
176-
('qualifier', G_AN % 3 ),
177-
('additional_info', G_ALL % 70),
178-
('_3', G__ % 2 ),
184+
('record_code', '(05)', 2),
185+
('bank_code', G_N, 5),
186+
('internal_code', G_AN, 4),
187+
('desk_code', G_N, 5),
188+
('currency_code', G_A_, 3),
189+
('nb_of_dec', G_N_, 1),
190+
('_1', G_AN_, 1),
191+
('account_nb', G_AN, 11),
192+
('operation_code', G_AN, 2),
193+
('operation_date', G_N, 6),
194+
('_2', G__, 5),
195+
('qualifier', G_AN, 3),
196+
('additional_info', G_ALL, 70),
197+
('_3', G__, 2),
179198
]
180199

181200
def parse(self, line):
@@ -188,19 +207,19 @@ def parse(self, line):
188207
class ParserContent07(Parser):
189208
_code = '07'
190209
_regex = [
191-
('record_code', '(07)' ),
192-
('bank_code', G_N % 5 ),
193-
('_1', G__ % 4 ),
194-
('desk_code', G_N % 5 ),
195-
('currency_code', G_A_ % 3 ),
196-
('nb_of_dec', G_N_ % 1 ),
197-
('_2', G__ % 1 ),
198-
('account_nb', G_AN % 11),
199-
('_3', G__ % 2 ),
200-
('next_date', G_N % 6 ),
201-
('_4', G__ % 50),
202-
('next_amount', G_AMT ),
203-
('_5', G__ % 16),
210+
('record_code', '(07)', 2),
211+
('bank_code', G_N, 5),
212+
('_1', G__, 4),
213+
('desk_code', G_N, 5),
214+
('currency_code', G_A_, 3),
215+
('nb_of_dec', G_N_, 1),
216+
('_2', G__, 1),
217+
('account_nb', G_AN, 11),
218+
('_3', G__, 2),
219+
('next_date', G_N, 6),
220+
('_4', G__, 50),
221+
('next_amount', G_AMT, 14),
222+
('_5', G__, 16),
204223
]
205224

206225
def _post(self, res):
@@ -215,117 +234,117 @@ def _post(self, res):
215234
class ParserLIB(Parser):
216235
_code = "LIB"
217236
_regex = [
218-
('label', G_ALL %70),
237+
('label', G_ALL, 70),
219238
]
220239

221240

222241
class ParserNPY(Parser):
223242
_code = "NPY"
224243
_regex = [
225-
('debtor_name', G_ALL %70),
244+
('debtor_name', G_ALL, 70),
226245
]
227246

228247

229248
class ParserIPY(Parser):
230249
_code = 'IPY'
231250
_regex = [
232-
('debtor_id', G_ALL %35),
233-
('debtor_id_type', G_ALL %35),
251+
('debtor_id', G_ALL, 35),
252+
('debtor_id_type', G_ALL, 35),
234253
]
235254

236255

237256
class ParserNBE(Parser):
238257
_code = 'NBE'
239258
_regex = [
240-
('creditor_name', G_ALL %70),
259+
('creditor_name', G_ALL, 70),
241260
]
242261

243262

244263
class ParserIBE(Parser):
245264
_code = 'IBE'
246265
_regex = [
247-
('creditor_id', G_ALL %35),
248-
('creditor_id_type', G_ALL %35),
266+
('creditor_id', G_ALL, 35),
267+
('creditor_id_type', G_ALL, 35),
249268
]
250269

251270

252271
class ParserNPO(Parser):
253272
_code = 'NPO'
254273
_regex = [
255-
('ultimate_debtor_name', G_ALL %70),
274+
('ultimate_debtor_name', G_ALL, 70),
256275
]
257276

258277

259278
class ParserIPO(Parser):
260279
_code = 'IPO'
261280
_regex = [
262-
('ultimate_debtor_id', G_ALL %35),
263-
('ultimate_debtor_type', G_ALL %35),
281+
('ultimate_debtor_id', G_ALL, 35),
282+
('ultimate_debtor_type', G_ALL, 35),
264283
]
265284

266285

267286
class ParserNBU(Parser):
268287
_code = 'NBU'
269288
_regex = [
270-
('ultimate_creditor_name', G_ALL %70),
289+
('ultimate_creditor_name', G_ALL, 70),
271290
]
272291

273292

274293
class ParserIBU(Parser):
275294
_code = 'IBU'
276295
_regex = [
277-
('ultimate_creditor_id', G_ALL %35),
278-
('ultimate_creditor_type', G_ALL %35),
296+
('ultimate_creditor_id', G_ALL, 35),
297+
('ultimate_creditor_type', G_ALL, 35),
279298
]
280299

281300

282301
class ParserLCC(Parser):
283302
_code = 'LCC'
284303
_regex = [
285-
('remittance_information_1', G_ALL %70),
304+
('remittance_information_1', G_ALL, 70),
286305
]
287306

288307

289308
class ParserLC2(Parser):
290309
_code = 'LC2'
291310
_regex = [
292-
('remittance_information_2', G_ALL %70),
311+
('remittance_information_2', G_ALL, 70),
293312
]
294313

295314

296315
class ParserLCS(Parser):
297316
_code = 'LCS'
298317
_regex = [
299-
('creditor_ref_information', G_ALL %70),
318+
('creditor_ref_information', G_ALL, 70),
300319
]
301320

302321

303322
class ParserRCN(Parser):
304323
_code = 'RCN'
305324
_regex = [
306-
('end2end_identification', G_ALL %35),
307-
('purpose', G_ALL %35),
325+
('end2end_identification', G_ALL, 35),
326+
('purpose', G_ALL, 35),
308327
]
309328

310329

311330
class ParserRCN(Parser):
312331
_code = 'RCN'
313332
_regex = [
314-
('payment_infor_id', G_ALL %35),
315-
('instruction_id', G_ALL %35),
333+
('payment_infor_id', G_ALL, 35),
334+
('instruction_id', G_ALL, 35),
316335
]
317336

318337

319338
#Specific to bank transfers
320339
class ParserMMO(Parser):
321340
_code = 'MMO'
322341
_regex = [
323-
('currency_code', G_AN %3),
324-
('nb_of_dec_amount', G_N %1),
325-
('equivalent_amount', G_N_ %14),
326-
('nb_of_dec_exchange_rate', G_N_ %2),
327-
('exchange_rate', G_N_ %11),
328-
('_', G_AN_ %39)
342+
('currency_code', G_AN, 3),
343+
('nb_of_dec_amount', G_N, 1),
344+
('equivalent_amount', G_N_, 14),
345+
('nb_of_dec_exchange_rate', G_N_, 2),
346+
('exchange_rate', G_N_, 11),
347+
('_', G_AN_, 39)
329348
]
330349

331350
def _post(self, res):
@@ -342,7 +361,7 @@ def _post(self, res):
342361
class ParserCBE(Parser):
343362
_code = 'CBE'
344363
_regex = [
345-
('creditor_account', G_ALL %70),
364+
('creditor_account', G_ALL, 70),
346365
]
347366

348367
#specific to withdrawal

0 commit comments

Comments
 (0)