-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhl7magic.py
193 lines (132 loc) · 6.43 KB
/
hl7magic.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
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
from burp import IBurpExtender
from burp import IMessageEditorTab
from burp import IMessageEditorTabFactory
from hl7apy.parser import parse_message
from hl7apy.exceptions import UnsupportedVersion
import json
import sys
class BurpExtender(IBurpExtender, IMessageEditorTabFactory):
def registerExtenderCallbacks(self, callbacks):
self._callbacks = callbacks
self._helpers = callbacks.getHelpers()
callbacks.setExtensionName("HL7Magic")
callbacks.registerMessageEditorTabFactory(self)
sys.stdout = callbacks.getStdout()
sys.stderr = callbacks.getStderr()
def createNewInstance(self, controller, editable):
return HL7ConverterTab(self, controller, editable, self._callbacks)
class HL7ConverterTab(IMessageEditorTab):
def __init__(self, extender, controller, editable, callbacks):
self._extender = extender
self.controller = controller
self.editable = editable
self._editor = callbacks.createTextEditor()
self._helpers = extender._helpers
self.req = None
def getTabCaption(self):
return "HL7Magic"
def getUiComponent(self):
return self._editor.getComponent()
def isEnabled(self, content, isRequest):
if isRequest and content:
return True
return False
def setMessage(self, content, isRequest):
if content is None:
self._editor.setText(None)
self._editor.setEditable(False)
else:
self.req = self._helpers.analyzeRequest(content)
params = content[(self.req.getBodyOffset()):].tostring()
json_data = self.hl72json(params)
self._editor.setText(json_data)
self._editor.setEditable(True)
def getMessage(self):
message = self._editor.getText()
hl7_message = self.json2hl7(message)
return self._helpers.buildHttpMessage(self.req.getHeaders(), hl7_message)
def getSelectedData(self):
return self._editor.getSelectedText()
def isModified(self):
return self._editor.isTextModified()
def hl72json(self, message):
try:
self.m = parse_message(message.replace("\n", "\r"), find_groups=False)
except Exception as e:
print("Error", e)
seg_list = []
result = {
"messageType": "",
"segments": []
}
for seg in self.m.children:
seg_list.append(seg.name)
for segName in set(seg_list):
if len(getattr(self.m, segName)) > 1:
multi_segs = []
for i, subSeg in enumerate(getattr(self.m, segName)):
segment_name = subSeg.name
current_segment = {
"name": segment_name,
"fields": []
}
for f in getattr(self.m, segName)[i].children:
field_data = {
"short_name": f.name,
"long_name": f.long_name,
}
if len(getattr(getattr(self.m, segName)[1], f.name)) > 1:
subfields = []
for v in getattr(getattr(self.m, segName)[i], f.name):
subfields.append(v.value)
field_data.update({"value": subfields})
else:
field_data.update({"value": f.value})
current_segment["fields"].append(field_data)
multi_segs.append(current_segment)
result["segments"].append(multi_segs)
else:
segment_name = segName
current_segment = {
"name": segment_name,
"fields": []
}
if current_segment is not None:
for f in getattr(self.m, segName).children:
field_data = {
"short_name": f.name,
"long_name": f.long_name,
}
if len(getattr(getattr(self.m, segName), f.name)) > 1:
subfields = []
for v in getattr(getattr(self.m, segName), f.name):
subfields.append(v.value)
field_data.update({"value": subfields})
else:
field_data.update({"value": f.value})
current_segment["fields"].append(field_data)
result["segments"].append(current_segment)
d = json.dumps(result, indent=4)
output_body = bytes(d)
return output_body
def json2hl7(self, json_message):
d = self._helpers.bytesToString(json_message)
result = eval(d)
for s in result['segments']:
if isinstance(s, list):
for i, item in enumerate(s):
for field_data in item['fields']:
if isinstance(field_data['value'], list):
for j, value in enumerate(field_data['value']):
getattr(getattr(self.m, item['name'])[i], field_data['short_name'])[j].value = value
else:
setattr(getattr(self.m, item['name'])[i], field_data['short_name'], field_data['value'])
else:
for field_data in s['fields']:
if isinstance(field_data['value'], list):
for i, value in enumerate(field_data['value']):
getattr(getattr(self.m, s['name']), field_data['short_name'])[i].value = value
else:
setattr(getattr(self.m, s['name']), field_data['short_name'], field_data['value'])
amended = self.m.to_er7().replace("\r", "\n")
return amended