1
2 import sys, string, re, types
3
4
5 from Gnumed.pycommon import gmCfg, gmDrugObject, gmExceptions
6 from Gnumed.business import gmSurgery
7
8
9 _log = gmLog.gmDefLog
10 _cfg = gmCfg.gmDefCfgFile
11
12 darkblue = '#00006C'
13 darkgreen = '#0106D0A'
14 darkbrown = '#841310'
15
16
18 """handles a given Interface to a drug database via the Drug object"""
19
21 """
22 Initialize the DrugView object with information supplied via
23 the standard config file. The data should be grouped together
24 in the group designated by the database name.
25 """
26
27 if aDatabaseName == None:
28 raise gmExceptions.ConstructorError,"No database name specified."
29
30
31
32
33
34
35 currWorkplace = gmSurgery.gmCurrentPractice().active_workplace
36 if currWorkplace is None:
37
38 self.dbConfFile = _cfg.get(aDatabaseName, 'configfile')
39 else:
40
41 self.dbConfFile, match = gmCfg.getDBParam(
42 workplace=currWorkplace,
43 option="DrugReferenceBrowser.%s.configfile" % aDatabaseName
44 )
45
46 _log.Log(gmLog.lInfo, "dbConfFile is [%s]" % str(self.dbConfFile))
47
48 if self.dbConfFile is None:
49 _log.Log(gmLog.lErr, "No config information on drug database [%s] found." % aDatabaseName)
50 raise gmExceptions.ConstructorError,"No DrugDB config file specified."
51
52 try:
53 self.mDrugInterface = gmDrugObject.cDrug(queryCfgSource = self.dbConfFile)
54 except:
55 _log.LogException("Unhandled exception while opening config file", sys.exc_info(), verbose = 0)
56 raise gmExceptions.ConstructorError,"Couldn't initialize drug object for DB %s" % aDatabaseName
57
58 self.__mFormatString = {}
59 self.__mGroupPos = {}
60 self.__mFormatType = {}
61 self.__mHeading = {}
62 self.__mUsedVars = {}
63
64 self.__getFormatInfo()
65
66
67 self.mLastId = {}
68 self.mCurrId = -1
69
70 - def SearchIndex(self, aType=None, aName=None , aMode='exact', format=0):
71 """
72 Search a for a certain drug. Possible values for index type include
73 0 (brand name index), 1 (generic name index) and 2 (indication index).
74 mode can be either exact matching (that is, match all given letters
75 using the LIKE operator), regular expression ('re') matching or
76 complete list ('complete').
77 """
78
79 if aName == None:
80 return None
81
82
83 if aType == 0:
84 index = 'brand_index'
85 elif aType == 1:
86 index = 'generic_index'
87 elif aType == 2:
88 index = 'indication_index'
89 else:
90 return None
91
92 searchExact = 0
93 searchAll = 0
94 searchRE = 0
95
96 if aMode == 'exact':
97 suffix = '_exact'
98 search_exact = 1
99 elif aMode == 're':
100 suffix = '_re'
101 searchRE = 1
102 elif aMode == 'complete':
103 suffix = '_all'
104 searchAll = 1
105
106 if not searchAll:
107 self.mDrugInterface.mVars['Key'] = aName
108
109 result = self.mDrugInterface.GetData(index + suffix,refresh=1)
110 return result
111
113 """
114 Returns a list of drugs for a given generic substance.
115 The substance is specified by aID.
116 """
117 if aId is None:
118 return None
119
120 self.mDrugInterface.mVars['ID']=aId
121
122 result = self.mDrugInterface.GetData('brandsForGeneric',refresh=1)
123 return result
124
126 """
127 Returns an HTML-formatted drug information sheet for display
128 in the Pharmaceutical Reference Browser.
129 The drug is specified by aID.
130 """
131 if aId is None:
132 return None
133
134 self.mDrugInterface.mVars['ID']=aId
135 self.mCurrId = aId
136
137
138 piText=''
139 headings=[]
140 groupPosList = self.__mGroupPos.keys()
141
142
143
144
145 groupPosList.sort()
146 for pos in groupPosList:
147 textPart = self.__getTextPart(pos)
148 if textPart != '':
149 piText += textPart
150 if self.__mHeading.has_key(pos):
151 headings.append(self.__mHeading[pos])
152
153
154
155 piTotalLen = len(piText)
156 if piTotalLen == 0:
157 pitext = "<HTML><HEAD></HEAD><BODY BGCOLOR='#FFFFFF8'> <FONT SIZE=3>"
158 pitext = pitext + _("No product information available.")
159 pitext = pitext + "</FONT></BODY></HTML>"
160 return pitext
161
162
163
164
165
166
167
168 piTextComplete="<HTML><HEAD></HEAD><BODY BGCOLOR='#FFFFFF8'> <FONT SIZE=-1>"
169
170
171
172
173 piTextComplete = piTextComplete + "<A NAME=\"Description\"></A><BR><FONT SIZE=4 COLOR='" + darkblue + "'><B>Description</B></FONT><BR>"
174 piTextComplete = piTextComplete + piText + "</FONT></BODY></HTML>"
175
176 return (piText,headings)
177
178
179 - def __getTextPart(self, pos = 0):
180 """
181 get the formatted result of a numbered text entry.
182 Every entry has a number that is used as an pointer in several lists.
183 These lists hold the entry type (one of 'heading', 'noheading',
184 'single' or 'list'), the query group containing the parameters in a
185 dictionary, the format string and the names of the parameters used
186 (the latter is used to test for completely empty parameter sets that
187 wont be displayed).
188 Short explanation of types:
189 heading : holds only a heading, takes no parameters from dict
190 noheading : the contrary: no heading, only format string is used
191 single: has a heading and uses the format string
192 list: has a heading, but does not use the format string. Instead all
193 values found for an parameter are put in an itemized list.
194
195 All types using parameters from a query must supply a list of parameters
196 used via entry 'usedvars' in config file.
197 """
198
199
200 group = self.__mGroupPos[pos]
201 format_type = self.__mFormatType[pos]
202
203
204 refresh=0
205 if not self.mLastId.has_key(group):
206 self.mLastId[group] = -1
207 if self.mLastId[group] != self.mCurrId:
208 refresh=1
209 self.mLastId[group] = self.mCurrId
210
211
212
213
214
215 queryResult = self.mDrugInterface.GetData(group,refresh)
216
217
218
219 resultTotalLen = 0
220 if format_type != 'heading':
221 usedVars = self.__mUsedVars[pos]
222 if not queryResult is None:
223 for item in usedVars:
224 if not queryResult.has_key(item):
225 _log.Log(gmLog.lWarn, "Variable name invalid: %s" % item)
226 else:
227 value = queryResult[item]
228 if value == []:
229 value = ''
230 resultTotalLen += len(str(value))
231
232
233 else:
234 resultTotalLen = -1
235
236
237 if queryResult is None or resultTotalLen == 0:
238 return ''
239
240
241 if format_type == 'noheading':
242 formattedInfo = self.__mFormatString[pos] % queryResult
243 text = translateASCII2HTML(formattedInfo)
244 return text
245 else:
246
247 heading = self.__mHeading[pos]
248 if heading != '':
249 text = "<A NAME=\"" + heading + "\"></A><BR><FONT SIZE=5 COLOR='" + darkblue + "'><B>" + heading + "</B></FONT><BR>"
250 else:
251 text = ''
252
253 if format_type == "heading":
254 return text
255
256 if format_type == 'single':
257 formattedInfo = self.__mFormatString[pos] % queryResult
258 text = text + translateASCII2HTML(formattedInfo)
259 elif format_type == 'list':
260
261
262 resultTotalLen = 0
263
264
265
266
267 itemList = queryResult[usedVars[0]]
268
269 if not type(itemList) is types.ListType:
270 itemList = [itemList]
271
272 tmpText=''
273
274 for item_raw in itemList:
275
276 item=str(item_raw)
277 itemLen = len(item)
278
279 if itemLen > 0:
280 resultTotalLen += itemLen
281 tmpText = tmpText + "<li>" + item + "</li>"
282
283
284 if resultTotalLen > 0:
285 text += '<ul>' + tmpText + '</ul>'
286 else:
287 text = ''
288 else:
289
290 _log.Log(gmLog.lWarn, "Unknown format type: [%s]" % format_type)
291 text = ''
292
293 return text
294
295
296
380
381
382
383
384 if __name__ == "__main__":
385 print "please write unit test code"
386
387
388