Merge lp:~therp-nl/banking-addons/6.1_lp1117319_abnamro_sepa_line into lp:banking-addons/6.1

Proposed by Ronald Portier (Therp)
Status: Superseded
Proposed branch: lp:~therp-nl/banking-addons/6.1_lp1117319_abnamro_sepa_line
Merge into: lp:banking-addons/6.1
Diff against target: 78 lines (+46/-21)
1 file modified
account_banking_nl_abnamro/abnamro.py (+46/-21)
To merge this branch: bzr merge lp:~therp-nl/banking-addons/6.1_lp1117319_abnamro_sepa_line
Reviewer Review Type Date Requested Status
Stefan Rijnhart (Opener) Needs Fixing
Review via email: mp+146982@code.launchpad.net

This proposal supersedes a proposal from 2013-02-06.

This proposal has been superseded by a proposal from 2013-02-08.

Description of the change

Make it possible to import abnamro statement files with extended sepa attributes in line.

Taking into account the remarks by Stefan I streamlined the code , hopefully solving the errors.

To post a comment you must log in.
Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote : Posted in a previous version of this proposal

Thanks for fixing this!

l.8,28 Maybe combine in a single dictionary of possible keys?
l.62 Looks like the last key/value pair is not stored in sepa_dict. You could copy this line after the end of the 'while' loop.

review: Needs Fixing
Revision history for this message
Stefan Rijnhart (Opener) (stefan-opener) wrote :

I'm afraid your solution fails when the separator occurs in the field value as is allowed by the specs. E.g.

'/TRTP/SEPA OVERBOEKING/IBAN/CH999999999999F/BIC/UBSWCHZH80A/NAME/ORIG/INAL ART/REMI/CH9999999999/EREF/NOTPROVIDED/O\
RDP//ID/UBS AG'

Comes out as

{'ORIG//REMI': 'CH9999999999', 'NAME': '', 'BIC': 'UBSWCHZH80A', 'TRTP': 'SEPA OVERBOEKING', 'EREF': 'NOTPROVIDED', 'ORDP//ID': 'UBS AG', 'IBAN': 'CH999999999999F'}

when using your test script.

review: Needs Fixing
163. By Ronald Portier (Therp)

[FIX] Updated parsing of abnamro sepa string to handle even more unlikely cases.

164. By Ronald Portier (Therp)

[FIX] More meaningfull names for index variable names.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'account_banking_nl_abnamro/abnamro.py'
2--- account_banking_nl_abnamro/abnamro.py 2012-10-27 19:53:50 +0000
3+++ account_banking_nl_abnamro/abnamro.py 2013-02-08 09:36:20 +0000
4@@ -155,28 +155,53 @@
5 The string consists of slash separated KEY/VALUE pairs,
6 but the slash is allowed to and known to occur in VALUE as well!
7 """
8- items = field[1:].split('/') # skip leading slash
9+ def _sepa_message(field, reason):
10+ return _(
11+ 'unable to parse SEPA string: %s - %s' % (field, reason))
12+
13+ def _get_next_key(items, start):
14+ '''Find next key, starting from start, returns the key found,
15+ the start position in the array and the end position + 1'''
16+ known_keys = [
17+ 'TRTP', 'IBAN', 'BIC', 'NAME', 'RTRN', 'EREF', 'SWOC',
18+ 'REMI', 'ADDR', 'CPRP', 'CREF', 'CSID', 'ISDT', 'MARF',
19+ 'NRTX', 'NRTXR', 'PREF', 'PURP', 'REFOB', 'RREF', 'RTYP',
20+ 'SVCL', 'SWOD', 'BENM//ID', 'ORDP//ID', 'ORDP//RID',
21+ 'ORIG//CSID', 'ORIG//MARF', 'ULTD//NAME', 'ULTD//ID',
22+ 'ULTB//NAME', 'ULTB//ID'
23+ ]
24+ items_len = len(items)
25+ si = start
26+ # Search until start after end of items
27+ while si < items_len:
28+ ei = si + 1
29+ while ei < items_len:
30+ key = '/'.join(items[si:ei])
31+ if key in known_keys:
32+ return (key, si, ei)
33+ ei += 1
34+ si += 1
35+ return False
36+
37+ items = field[1:].split('/')
38+ assert len(items) > 1, _sepa_message(field, _('too few items'))
39 sepa_dict = {}
40- prev_key = False
41- known_keys = ['TRTP', 'IBAN', 'BIC', 'NAME', 'RTRN', 'EREF',
42- 'SWOC', 'REMI', ]
43- while items:
44- if len(items) == 1:
45- raise osv.except_osv(
46- _('Error !'),
47- _("unable to parse SEPA string: %s") % field)
48- key = items.pop(0)
49- if key not in known_keys:
50- # either an unknown key or a value containing a slash
51- if prev_key:
52- sepa_dict[prev_key] = sepa_dict[prev_key] + '/' + key
53- else:
54- raise osv.except_osv(
55- _('Error !'),
56- _("unable to parse SEPA string: %s") % field)
57- else:
58- sepa_dict[key] = items.pop(0).strip()
59- prev_key = key
60+ item_index = 0
61+ items_len = len(items)
62+ key_info = _get_next_key(items, item_index)
63+ assert key_info, _sepa_message(
64+ field, _('no key found for start %d') % item_index)
65+ assert key_info[1] == 0, _sepa_message(
66+ field, _('invalid data found before key %s') % key_info[0])
67+ while key_info:
68+ sepa_key = key_info[0]
69+ item_index = key_info[2]
70+ # Find where next key - if any - starts
71+ key_info = _get_next_key(items, item_index)
72+ ve = (key_info and key_info[1]) or items_len
73+ sepa_value = (
74+ (ve > item_index) and '/'.join(items[item_index:ve])) or ''
75+ sepa_dict[sepa_key] = sepa_value
76 return sepa_dict
77
78 def parse_type(field):

Subscribers

People subscribed via source and target branches