Coverage for mt940/parser.py: 0%
18 statements
« prev ^ index » next coverage.py v7.2.7, created at 2025-03-01 10:17 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2025-03-01 10:17 +0000
1# vim: fileencoding=utf-8:
2'''
4Format
5---------------------
7Sources:
9.. _Swift for corporates: http://www.sepaforcorporates.com/\
10 swift-for-corporates/account-statement-mt940-file-format-overview/
11.. _Rabobank MT940: https://www.rabobank.nl/images/\
12 formaatbeschrijving_swift_bt940s_1_0_nl_rib_29539296.pdf
14 - `Swift for corporates`_
15 - `Rabobank MT940`_
17::
19 [] = optional
20 ! = fixed length
21 a = Text
22 x = Alphanumeric, seems more like text actually. Can include special
23 characters (slashes) and whitespace as well as letters and numbers
24 d = Numeric separated by decimal (usually comma)
25 c = Code list value
26 n = Numeric
27'''
29import os
31import mt940
34def parse(src, encoding=None, processors=None, tags=None):
35 '''
36 Parses mt940 data and returns transactions object
38 :param src: file handler to read, filename to read or raw data as string
39 :return: Collection of transactions
40 :rtype: Transactions
41 '''
43 def safe_is_file(filename):
44 try:
45 return os.path.isfile(src)
46 except ValueError: # pragma: no cover
47 return False
49 if hasattr(src, 'read'): # pragma: no branch
50 data = src.read()
51 elif safe_is_file(src):
52 with open(src, 'rb') as fh:
53 data = fh.read()
54 else: # pragma: no cover
55 data = src
57 if hasattr(data, 'decode'): # pragma: no branch
58 exception = None
59 encodings = [encoding, 'utf-8', 'cp852', 'iso8859-15', 'latin1']
61 for encoding in encodings: # pragma: no cover
62 if not encoding:
63 continue
65 try:
66 data = data.decode(encoding)
67 break
68 except UnicodeDecodeError as e:
69 exception = e
70 except UnicodeEncodeError:
71 break
72 else:
73 raise exception # pragma: no cover
75 transactions = mt940.models.Transactions(processors, tags)
76 transactions.parse(data)
78 return transactions