File: __init__.py

package info (click to toggle)
python-mt940 0.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 224 kB
  • sloc: python: 474; sh: 11; makefile: 6
file content (294 lines) | stat: -rw-r--r-- 9,685 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# This file is part of mt940.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
"""a parser for MT940 files
"""
__version__ = '0.6.0'
__all__ = ['MT940', 'rabo_description', 'abn_amro_description',
    'ing_description', 'regiobank_description']

from collections import namedtuple, defaultdict
from decimal import Decimal
import datetime
import re
import io


SECTIONS = {
    'begin': [':940:'],
    'statement': [':20:'],
    'account': [':25:'],
    'information': [':28:', ':28C:'],
    'start_balance': [':60F:'],
    'transaction': [':61:'],
    'description': [':86:'],
    'end_balance': [':62F:'],
    }


def _parse_date(date):
    return datetime.datetime.strptime(date, '%y%m%d').date()


def _parse_amount(amount, sign='C'):
    amount = Decimal(amount.replace(',', '.'))
    if sign in ('D', 'RC'):
        return -amount
    return amount


TRANSACTION_RE = re.compile(r"""
    (?P<date>\d{6})
    (?P<booking>\d{4})?
    (?P<sign>D|C|RC|RD)
    (?P<code>\w)??  # ING skips this mandatory field
    (?P<amount>(\d|,){1,15})
    (?P<id>\w{4})
    (?P<reference>.{0,34})""", re.VERBOSE)


class MT940(object):

    def __init__(self, name, encoding=None):
        self.statements = []

        if isinstance(name, (bytes, str)):
            with io.open(name, encoding=encoding, mode='r') as f:
                self._parse(f)
        else:
            self._parse(name)

    def _parse(self, f):
        values = defaultdict(str)
        transactions = []
        for line in self._readline(f):
            for name, sections in SECTIONS.items():
                if name == 'begin':
                    continue
                for section in sections:
                    if line.startswith(section):
                        if name in values and name == 'statement':
                            self._set_statement(values, transactions)
                        if name.endswith('_balance'):
                            values[name] = self._get_balance(
                                line[len(section):])
                        elif name == 'transaction':
                            transactions.append(
                                self._get_transaction(line[len(section):]))
                        elif name == 'description':
                            description = line[len(section):]
                            if 'end_balance' in values:
                                values['description'] += description
                            else:
                                transactions[-1] = (transactions[-1][:-1]
                                    + (description,))
                        else:
                            values[name] += line[len(section):]
        if values:
            self._set_statement(values, transactions)

    @staticmethod
    def _readline(f):
        buf = []
        for line in f:
            line = line.strip('\n')
            if buf:
                if (line.startswith(':')
                        or line.startswith('-')):
                    yield '\n'.join(buf)
                    del buf[:]
            buf.append(line)
        if buf:
            yield '\n'.join(buf)

    @staticmethod
    def _get_balance(balance):
        date = _parse_date(balance[1:7])
        amount = _parse_amount(balance[10:], balance[0])
        return Balance(date=date, amount=amount, currency=balance[7:10])

    @staticmethod
    def _get_transaction(transaction):
        lines = transaction.splitlines()
        if len(lines) == 1:
            transaction, = lines
            additional_data = None
        else:
            transaction, additional_data = lines
        transaction = TRANSACTION_RE.match(transaction)
        date = _parse_date(transaction.group('date'))
        if transaction.group('booking'):
            booking = _parse_date(
                transaction.group('date')[:2]
                + transaction.group('booking'))
        else:
            booking = None
        amount = _parse_amount(transaction.group('amount'),
            transaction.group('sign'))
        id_ = transaction.group('id')
        reference = transaction.group('reference')
        reference, _, institution_reference = reference.partition('//')
        return (date, booking, amount, id_, reference,
            institution_reference, additional_data, '')

    def _set_statement(self, values, transactions):
        # Set optional values
        values.setdefault('start_balance')
        values.setdefault('end_balance')
        values.setdefault('description')
        self.statements.append(
            Statement(
                transactions=[Transaction(*t) for t in transactions],
                **values))
        values.clear()
        del transactions[:]


Statement = namedtuple('Statement', ['statement', 'account', 'information',
        'start_balance', 'transactions', 'end_balance', 'description'])
Balance = namedtuple('Balance', ['date', 'amount', 'currency'])
Transaction = namedtuple('Transaction', ['date', 'booking', 'amount', 'id',
        'reference', 'institution_reference', 'additional_data',
        'description'])


def _find_swift_tags(tags, description):
    values = {}
    for tag, name in tags:
        if description.startswith(tag):
            description = description[len(tag):]
            cursor = len(description)
            for next_tag, _ in tags:
                if next_tag in values or next_tag == tag:
                    continue
                index = description.find(next_tag)
                if index == -1:
                    continue
                cursor = min(cursor, index)
            next_tag_index = cursor
            values[name] = description[:next_tag_index]
            description = description[next_tag_index:]
        if not description:
            break
    return values


RABO_TAGS = [
    ('/MARF/', 'marf'),
    ('/EREF/', 'eref'),
    ('/PREF/', 'pref'),
    ('/TRCD/', 'trcd'),
    ('/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 dictionary 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):
    "Return dictionary with ABN AMRO informations"
    description = ''.join(description.splitlines())
    values = {}
    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


ING_TAGS = re.compile(r'/(RTRN|EREF|PREF|MARF|CSID|CNTP|REMI|PURP|ULT[CD])/')
ING_TAGS_DEFINITION = {
    'RTRN': ('rtrn', []),
    'EREF': ('eref', []),
    'PREF': ('pref', []),
    'MARF': ('marf', []),
    'CSID': ('csid', []),
    'CNTP': ('cntp', ['account_number', 'bic', 'name', 'city']),
    'REMI': ('remi', ['code', 'issuer', 'remittance_info']),
    'PURP': ('purp', []),
    'ULTC': ('ultc', ['name', 'id']),
    'ULTD': ('ultd', ['name', 'id']),
    }


def ing_description(description):
    "Return dictionary with ING informations"
    description = ''.join(description.splitlines())
    values = {}
    ing_tags = iter(ING_TAGS.split(description)[1:])
    for tag, tag_value in zip(ing_tags, ing_tags):
        tag_value = tag_value[:-1]
        name, subfields = ING_TAGS_DEFINITION[tag]

        if not subfields:
            values[name] = tag_value
            continue

        values[name] = {}
        if 'name' in subfields or 'remittance_info' in subfields:
            special_tag = 'name' if 'name' in subfields else 'remittance_info'
            tag_idx = subfields.index(special_tag)
            subtags = tag_value.split('/', tag_idx)
            for sf_name, sf_value in zip(subfields[:tag_idx], subtags[:-1]):
                values[name][sf_name] = sf_value
            subtags = subtags[-1].rsplit('/', len(subfields) - tag_idx - 1)
            for sf_name, sf_value in zip(subfields[tag_idx:], subtags):
                values[name][sf_name] = sf_value
        else:
            subtags = tag_value.split('/')
            for sf_name, sf_value in zip(subfields, subtags):
                values[name][sf_name] = sf_value

    return values


def regiobank_description(description):
    "Return dictionary with RegioBank informations"
    lines = description.splitlines()
    values = {}
    try:
        first, second, third = lines[0], lines[1], ''.join(lines[2:])
    except (ValueError, IndexError):
        return {}
    try:
        values['account_number'], values['name'] = first.split(' ', 1)
    except ValueError:
        return {}
    values['address'] = second  # XXX Not clear how to split it
    if third.startswith('aan %s' % values['name']):
        _, values['iban'], values['remittance_info'], values['description'] = \
                third.split(',')
    else:
        values['reference'] = third
    return values