-
Notifications
You must be signed in to change notification settings - Fork 2
/
mt_101.py
144 lines (110 loc) · 5.07 KB
/
mt_101.py
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
import re
def parse_first_block(msg):
block_entries = []
labeled_entries = []
regex = r"{1:(\w{1})(\d+)(\w{12})(\d{4})(\d+)}"
matches = re.finditer(regex, msg, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum += 1
for groupNum in range(0, len(match.groups())):
groupNum += 1
block_entries.append(match.group(groupNum))
if len(block_entries) == 5:
labeled_entries.append({'Applid': block_entries[0]})
labeled_entries.append({'Servid': block_entries[1]})
labeled_entries.append({'LTaddrBlk1': block_entries[2]})
labeled_entries.append({'Sesno': block_entries[3]})
labeled_entries.append({'Osn': block_entries[4]})
return {'BLOCK 1': labeled_entries}
def parse_2nd_block(msg):
block_entries = []
labeled_entries = []
regex = '{\d{1}:(\w{1})(\d{3})(\d{4})(\d{6})(\w{12})(\d{4})(\d{6})(\d{6})(\d{4})(\w{1})'
matches = re.finditer(regex, msg, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum += 1
for groupNum in range(0, len(match.groups())):
groupNum += 1
block_entries.append(match.group(groupNum))
if len(block_entries) >= 10:
labeled_entries.append({'Inoutind': block_entries[0]})
labeled_entries.append({'Msgtype': block_entries[1]})
labeled_entries.append({'Intime': block_entries[2]})
labeled_entries.append({'Indate': block_entries[3]})
labeled_entries.append({'LTaddrBlk2': block_entries[4]})
labeled_entries.append({'Ssesno': block_entries[5]})
labeled_entries.append({'Isn': block_entries[6]})
labeled_entries.append({'Outdate': block_entries[7]})
labeled_entries.append({'Outtime': block_entries[8]})
labeled_entries.append({'Msgprior': block_entries[9]})
return {'BLOCK 2': labeled_entries}
def parse_3rd_block(msg):
block_entries = []
labeled_entries = []
regex = '{\d+:(MT101 \d+\s+\w{2}\s\d+)'
matches = re.finditer(regex, msg, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum += 1
for groupNum in range(0, len(match.groups())):
groupNum += 1
block_entries.append(match.group(groupNum))
if len(block_entries) > 0:
labeled_entries.append({'Tag108': block_entries[0]})
return {'BLOCK 3': labeled_entries}
def parse_4th_block(msg):
block_entries = []
labeled_entries = []
# regex = ':20:(\d+)\n?:28D:(\d\/\d)\n?:50H:\/(\w+.?\w+.?\w+)(\n\w+)?\n?:30:(\d{6})\n?:21:(\w+)\n?:32B:(\w+),?\n?:50L:(\w+)\n?:59:\/(\w+\n\w+?)\n?:71A:(\w+)\n?-}'
regex = '{4:\n?:20:(\d+)\n?:28D:(\d\/\d)\n?:50H:\/(\w+.?\w+.?\w+\n\w+)?\n?:30:(\d{6})\n?:21:(\w+)\n?:32B:(\w+),?\n?:50L:(\w+)\n?:59:\/(\w+\n\w+?)\n?:71A:(\w+)\n?-}'
matches = re.finditer(regex, msg, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum += 1
for groupNum in range(0, len(match.groups())):
groupNum += 1
block_entries.append(match.group(groupNum))
if len(block_entries) >= 9:
labeled_entries.append({'Senders Reference': block_entries[0]})
labeled_entries.append({'Message Index/Total': block_entries[1]})
labeled_entries.append({'50H - Ordering Customer': block_entries[2]})
labeled_entries.append({'30 - Requested Execution Date': block_entries[3]})
labeled_entries.append({'21 - Transaction Reference': block_entries[4]})
labeled_entries.append({'32B - Currency/Transaction Amount': block_entries[5]})
labeled_entries.append({'50L - Instructing Party': block_entries[6]})
labeled_entries.append({'59 - Beneficiary': block_entries[7]})
labeled_entries.append({'71A - Details of Charges': block_entries[8]})
return {'BLOCK 4': labeled_entries}
def parse_5th_block(msg):
block_entries = []
labeled_entries = []
regex = '{5:{MAC:(\w+)}{CHK:(\w+)}{TNG:(\w+|)}'
matches = re.finditer(regex, msg, re.MULTILINE)
for matchNum, match in enumerate(matches):
matchNum += 1
for groupNum in range(0, len(match.groups())):
groupNum += 1
block_entries.append(match.group(groupNum))
if len(block_entries) >= 3:
labeled_entries.append({'MAC': block_entries[0]})
labeled_entries.append({'CHK': block_entries[1]})
labeled_entries.append({'TNG': block_entries[2]})
return {'BLOCK 5': labeled_entries}
def parse(message_text):
mt_message = []
try:
if message_text is not None:
mt_message.append(parse_first_block(message_text))
mt_message.append(parse_2nd_block(message_text))
mt_message.append(parse_3rd_block(message_text))
mt_message.append(parse_4th_block(message_text))
mt_message.append(parse_5th_block(message_text))
except Exception as ex:
print('Exception in parse')
print(str(ex))
finally:
return mt_message
if __name__ == '__main__':
data = None
with open('101.txt', encoding='utf8') as f:
data = f.read()
result = parse(data)
print(result)