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