changeset 1:31acb0d0d691

Add method to parse Rabo description
author Cédric Krier <ced@b2ck.com>
date Fri, 01 Nov 2013 19:11:00 +0100
parents 3d6eb6c42455
children 17f892bb8c4f
files mt940/__init__.py mt940/test.py
diffstat 2 files changed, 57 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mt940/__init__.py	Tue Jul 30 19:50:26 2013 +0200
+++ b/mt940/__init__.py	Fri Nov 01 19:11:00 2013 +0100
@@ -29,7 +29,7 @@
 """a parser for MT940 files
 """
 __version__ = '0.1'
-__all__ = ['MT940']
+__all__ = ['MT940', 'rabo_description']
 
 from collections import namedtuple, defaultdict
 from decimal import Decimal
@@ -150,3 +150,38 @@
 Balance = namedtuple('Balance', ['date', 'amount', 'currency'])
 Transaction = namedtuple('Transaction', ['date', 'booking', 'amount', 'id',
         'reference', 'account', 'description'])
+
+
+RABO_TAGS = [
+    ('/MARF/', 'marf'),
+    ('/EREF/', 'eref'),
+    ('/PREF/', 'pref'),
+    ('/BENM/', 'benm'),
+    ('/ORDP/', 'ordp'),
+    ('/NAME/', 'name'),
+    ('/ID/', 'id'),
+    ('/ADDR/', 'addr'),
+    ('/REMI/', 'remi'),
+    ('/CDTRREFTP//CD/SCOR/ISSR/CUR/CDTRREF/', 'cdtrref'),
+    ('/CSID/', 'csid'),
+    ('/ISDT/', 'isdt'),
+    ('/RTRN/', 'rtrn'),
+    ]
+
+
+def rabo_description(description):
+    "Return dictionnary with Rabo 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
+    return values
--- a/mt940/test.py	Tue Jul 30 19:50:26 2013 +0200
+++ b/mt940/test.py	Fri Nov 01 19:11:00 2013 +0100
@@ -34,7 +34,7 @@
 from decimal import Decimal
 
 here = os.path.dirname(__file__)
-from mt940 import MT940
+from mt940 import MT940, rabo_description
 
 
 class TestMT940(unittest.TestCase):
@@ -82,5 +82,25 @@
 /NAME/QASD JGRED/REMI/Dit zijn de omschrijvingsregels/EREF/NOTPRO
 VIDED''')
 
+
+class TestRaboDescription(unittest.TestCase):
+
+    def test_one_tag(self):
+        self.assertEqual(rabo_description('/EREF/foo'), {'eref': 'foo'})
+
+    def test_empty_tags(self):
+        self.assertEqual(rabo_description('/BENM//NAME/Doe'),
+            {'benm': '', 'name': 'Doe'})
+
+    def test_long_tags(self):
+        self.assertEqual(rabo_description(
+                '/ORDP//NAME/Doe/REMI//CDTRREFTP//CD/SCOR/ISSR/CUR/CDTRREF/'
+                '12345'
+                )['cdtrref'], '12345')
+
+    def test_non_rabo(self):
+        self.assertEqual(rabo_description('foo'), {})
+        self.assertEqual(rabo_description('/FOO/BAR/NAME/'), {})
+
 if __name__ == '__main__':
     unittest.main()