1 """GNUmed SOAP importer
2
3 (specification by Karsten Hilbert <Karsten.Hilbert@gmx.net>)
4
5 This script is designed for importing GNUmed SOAP input "bundles".
6
7 - "bundle" is list of dicts
8 - each "bundle" is processed dict by dict
9 - the dicts in the list are INDEPENDANT of each other
10 - each dict contains information for one new clin_narrative row
11 - each dict has the keys: 'soap', 'types', 'text', 'clin_context'
12 - 'soap':
13 - relates to clin_narrative.soap_cat
14 - 'types':
15 - a list of strings
16 - the strings must be found in clin_item_type.type
17 - strings not found in clin_item_type.type are ignored during
18 import and the user is warned about that
19 - 'text':
20 - the narrative for clin_narrative.narrative, imported as is
21 - 'clin_context':
22 - 'clin_context' is a dictionary containing clinical
23 context information, required to properly create clinical items.
24 Its 'episode_id' must always be supplied.
25 """
26
27 __version__ = "$Revision: 1.24 $"
28 __author__ = "Carlos Moro <cfmoro1976@yahoo.es>"
29 __license__ = "GPL v2 or later (details at http://www.gnu.org)"
30
31
32 import sys, re, logging
33
34
35
36 from Gnumed.pycommon import gmExceptions, gmI18N, gmDispatcher
37 from Gnumed.business import gmClinNarrative, gmPerson, gmPersonSearch
38
39
40 _log = logging.getLogger('gm.soap')
41
42
43
44 soap_bundle_SOAP_CAT_KEY = "soap"
45 soap_bundle_TYPES_KEY = "types"
46 soap_bundle_TEXT_KEY = "text"
47 soap_bundle_CLIN_CTX_KEY = "clin_context"
48 soap_bundle_TYPE_KEY = "type"
49 soap_bundle_EPISODE_ID_KEY = "episode_id"
50 soap_bundle_ENCOUNTER_ID_KEY = "encounter_id"
51 soap_bundle_STAFF_ID_KEY = "staff_id"
52 soap_bundle_SOAP_CATS = ['s','o','a','p']
53
55 """
56 Main SOAP importer class
57 """
58
61
62
63
65 """
66 Import supplied GNUmed SOAP input "bundle". For details consult current
67 module's description information.
68
69 @param bundle: GNUmed SOAP input data (as described in module's information)
70 @type bundle: list of dicts
71 """
72
73 for soap_entry in bundle:
74 if not self.__import_narrative(soap_entry):
75 _log.error('skipping soap entry')
76 continue
77 gmDispatcher.send(signal = 'clin_item_updated')
78 return True
79
80
81
117
118 - def __verify_soap_entry(self, soap_entry):
119 """Perform basic integrity check of a supplied SOAP entry.
120
121 @param soap_entry: dictionary containing information related to one
122 SOAP input
123 @type soap_entry: dictionary with keys 'soap', 'types', 'text'
124 """
125 required_keys = [
126 soap_bundle_SOAP_CAT_KEY,
127 soap_bundle_CLIN_CTX_KEY,
128 soap_bundle_TEXT_KEY
129 ]
130
131 for a_key in required_keys:
132 try:
133 soap_entry[a_key]
134 except KeyError:
135 _log.error('key [%s] is missing from soap entry' % a_key)
136 _log.error('%s' % soap_entry)
137 return False
138
139 if not soap_entry[soap_bundle_SOAP_CAT_KEY] in soap_bundle_SOAP_CATS:
140 _log.error('invalid soap category [%s]' % soap_entry[soap_bundle_SOAP_CAT_KEY])
141 _log.error('%s' % soap_entry)
142 return False
143 try:
144 soap_entry[soap_bundle_CLIN_CTX_KEY][soap_bundle_EPISODE_ID_KEY]
145 except KeyError:
146 _log.error('SOAP entry does not provide mandatory episode ID')
147 _log.error('%s' % soap_entry)
148 return False
149 return True
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172 if __name__ == '__main__':
173 _log.info("starting SOAP importer...")
174
175
176 patient = gmPersonSearch.ask_for_patient()
177 if patient is None:
178 print("No patient. Exiting gracefully...")
179 sys.exit(0)
180 gmPerson.set_active_patient(patient=patient)
181
182
183 importer = cSOAPImporter()
184 bundle = [
185 {soap_bundle_SOAP_CAT_KEY: 's',
186 soap_bundle_TYPES_KEY: ['Hx'],
187 soap_bundle_TEXT_KEY: 'Test subjective narrative',
188 soap_bundle_CLIN_CTX_KEY: {soap_bundle_EPISODE_ID_KEY: '1'}
189 },
190 {soap_bundle_SOAP_CAT_KEY: 'o',
191 soap_bundle_TYPES_KEY: ['Hx'],
192 soap_bundle_TEXT_KEY: 'Test objective narrative',
193 soap_bundle_CLIN_CTX_KEY: {soap_bundle_EPISODE_ID_KEY: '1'}
194 },
195 {soap_bundle_SOAP_CAT_KEY: 'a',
196 soap_bundle_TYPES_KEY: ['Hx'],
197 soap_bundle_TEXT_KEY: 'Test assesment narrative',
198 soap_bundle_CLIN_CTX_KEY: {soap_bundle_EPISODE_ID_KEY: '1'}
199 },
200 {soap_bundle_SOAP_CAT_KEY: 'p',
201 soap_bundle_TYPES_KEY: ['Hx'],
202 soap_bundle_TEXT_KEY: 'Test plan narrative. [:tetanus:]. [:pneumoniae:].',
203 soap_bundle_CLIN_CTX_KEY: {
204 soap_bundle_EPISODE_ID_KEY: '1',
205 soap_bundle_ENCOUNTER_ID_KEY: '1',
206 soap_bundle_STAFF_ID_KEY: '1'
207 },
208 }
209 ]
210 importer.import_soap(bundle)
211
212
213 if patient is not None:
214 try:
215 patient.cleanup()
216 except Exception:
217 print("error cleaning up patient")
218
219 _log.info("closing SOAP importer...")
220
221