changeset 2:17f892bb8c4f

Add ABN AMRO description parser
author Cédric Krier <ced@b2ck.com>
date Fri, 09 May 2014 23:48:21 +0200
parents 31acb0d0d691
children 5130a7997e09
files mt940/__init__.py mt940/test.py
diffstat 2 files changed, 73 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/mt940/__init__.py	Fri Nov 01 19:11:00 2013 +0100
+++ b/mt940/__init__.py	Fri May 09 23:48:21 2014 +0200
@@ -29,7 +29,7 @@
 """a parser for MT940 files
 """
 __version__ = '0.1'
-__all__ = ['MT940', 'rabo_description']
+__all__ = ['MT940', 'rabo_description', 'abn_amro_description']
 
 from collections import namedtuple, defaultdict
 from decimal import Decimal
@@ -152,6 +152,21 @@
         'reference', 'account', 'description'])
 
 
+def _find_swift_tags(tags, description):
+    values = {}
+    for tag, name in tags:
+        if description.startswith(tag):
+            description = description[len(tag):]
+            try:
+                i = description.index('/')
+            except ValueError:
+                i = len(description)
+            values[name] = description[:i]
+            description = description[i:]
+        if not description:
+            break
+    return values
+
 RABO_TAGS = [
     ('/MARF/', 'marf'),
     ('/EREF/', 'eref'),
@@ -172,16 +187,35 @@
 def rabo_description(description):
     "Return dictionnary with Rabo informations"
     description = ''.join(description.splitlines())
+    return _find_swift_tags(RABO_TAGS, description)
+
+
+ABN_AMRO_ACCOUNT = re.compile(r"""
+    ^([0-9]{1,3}\.[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{1,3})""", re.VERBOSE)
+ABN_AMRO_GIRO = re.compile(r"""
+    ^GIRO\ +([0-9]+)""", re.VERBOSE)
+ABN_AMRO_TAGS = [
+    ('/TRTP/', 'trtp'),
+    ('/IBAN/', 'iban'),
+    ('/BIC/', 'bic'),
+    ('/CSID', 'csid'),
+    ('/NAME/', 'name'),
+    ('/REMI/', 'remi'),
+    ('/EREF/', 'eref'),
+    ('/ORDP//ID/', 'ordp'),
+    ('/BENM//ID/', 'benm'),
+    ]
+
+
+def abn_amro_description(description):
+    "Retrun dictionnary with ABN AMRO informations"
+    description = ''.join(description.splitlines())
     values = {}
-    for tag, name in RABO_TAGS:
-        if description.startswith(tag):
-            description = description[len(tag):]
-            try:
-                i = description.index('/')
-            except ValueError:
-                i = len(description)
-            values[name] = description[:i]
-            description = description[i:]
-        if not description:
-            break
+    m = ABN_AMRO_ACCOUNT.match(description)
+    if m:
+        values['account'] = m.group(1).replace('.', '')
+    m = ABN_AMRO_GIRO.match(description)
+    if m:
+        values['account'] = m.group(1)
+    values.update(_find_swift_tags(ABN_AMRO_TAGS, description))
     return values
--- a/mt940/test.py	Fri Nov 01 19:11:00 2013 +0100
+++ b/mt940/test.py	Fri May 09 23:48:21 2014 +0200
@@ -34,7 +34,7 @@
 from decimal import Decimal
 
 here = os.path.dirname(__file__)
-from mt940 import MT940, rabo_description
+from mt940 import MT940, rabo_description, abn_amro_description
 
 
 class TestMT940(unittest.TestCase):
@@ -102,5 +102,31 @@
         self.assertEqual(rabo_description('foo'), {})
         self.assertEqual(rabo_description('/FOO/BAR/NAME/'), {})
 
+
+class TestABNAMRODescription(unittest.TestCase):
+
+    def test_account(self):
+        self.assertEqual(abn_amro_description('12.34.56.789 John Doe'),
+            {'account': '123456789'})
+
+    def test_giro(self):
+        self.assertEqual(abn_amro_description('GIRO 4090309'),
+            {'account': '4090309'})
+
+    def test_tag(self):
+        self.assertEqual(abn_amro_description(
+                '''/TRTP/SEPA OVERBOEKING/IBAN/FR001234567890/BIC/GEF
+RADAM/NAME/ENERGIE BEDRIJF/EREF/NOTPROVIDED'''), {
+                'trtp': 'SEPA OVERBOEKING',
+                'iban': 'FR001234567890',
+                'bic': 'GEFRADAM',
+                'name': 'ENERGIE BEDRIJF',
+                'eref': 'NOTPROVIDED',
+                })
+
+    def test_non_abn_amro(self):
+        self.assertEqual(abn_amro_description('foo'), {})
+        self.assertEqual(rabo_description('/FOO/BAR/NAME/'), {})
+
 if __name__ == '__main__':
     unittest.main()