Home | Trees | Indices | Help |
|
---|
|
1 """GNUmed data mining related widgets.""" 2 3 #================================================================ 4 __author__ = 'karsten.hilbert@gmx.net' 5 __license__ = 'GPL v2 or later (details at http://www.gnu.org)' 6 7 8 # stdlib 9 import sys 10 import os 11 import fileinput 12 import logging 13 import io 14 import csv 15 16 17 # 3rd party 18 import wx 19 20 21 # GNUmed 22 if __name__ == '__main__': 23 sys.path.insert(0, '../../') 24 from Gnumed.pycommon import gmDispatcher 25 from Gnumed.pycommon import gmMimeLib 26 from Gnumed.pycommon import gmTools 27 from Gnumed.pycommon import gmPG2 28 from Gnumed.pycommon import gmMatchProvider 29 from Gnumed.pycommon import gmI18N 30 from Gnumed.pycommon import gmNetworkTools 31 from Gnumed.pycommon.gmExceptions import ConstructorError 32 33 from Gnumed.business import gmPerson 34 from Gnumed.business import gmDataMining 35 from Gnumed.business import gmPersonSearch 36 37 from Gnumed.wxpython import gmGuiHelpers 38 from Gnumed.wxpython import gmListWidgets 39 40 41 _log = logging.getLogger('gm.ui') 42 #================================================================44154 155 #================================================================ 156 from Gnumed.wxGladeWidgets import wxgPatientListingPnl 15746 """<patient_key> must index or name a column in self.__data""" 47 try: 48 self.patient_key = kwargs['patient_key'] 49 del kwargs['patient_key'] 50 except KeyError: 51 self.patient_key = None 52 53 gmListWidgets.cReportListCtrl.__init__(self, *args, **kwargs) 54 55 self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self._on_list_item_activated, self)56 #------------------------------------------------------------58 if self.data is None: 59 return None 60 61 if len(self.data) == 0: 62 return None 63 64 if data is None: 65 data = self.get_selected_item_data(only_one = True) 66 67 if data is None: 68 data = self.get_item_data(item_idx = 0) 69 70 if data is None: 71 return None 72 73 if self.patient_key is not None: 74 try: 75 data[self.patient_key] 76 return self.patient_key 77 except (KeyError, IndexError, TypeError): 78 # programming error 79 _log.error('misconfigured identifier column <%s>', self.patient_key) 80 81 _log.debug('identifier column not configured, trying to detect') 82 83 if 'pk_patient' in data: 84 return 'pk_patient' 85 86 if 'fk_patient' in data: 87 return 'fk_patient' 88 89 if 'pk_identity' in data: 90 return 'pk_identity' 91 92 if 'fk_identity' in data: 93 return 'fk_identity' 94 95 if 'id_identity' in data: 96 return 'id_identity' 97 98 return gmListWidgets.get_choices_from_list ( 99 parent = self, 100 msg = _( 101 'The report result list does not contain any of the following columns:\n' 102 '\n' 103 ' <%s> / pk_patient / fk_patient\n' 104 ' pk_identity / fk_identity / id_identity\n' 105 '\n' 106 'Select the column which contains patient IDs:\n' 107 ) % self.patient_key, 108 caption = _('Choose column from query results ...'), 109 choices = data.keys(), 110 columns = [_('Column name')], 111 single_selection = True 112 )113 114 patient_pk_data_key = property(__get_patient_pk_data_key, lambda x:x) 115 #------------------------------------------------------------ 116 # event handling 117 #------------------------------------------------------------119 data = self.get_selected_item_data(only_one = True) 120 pk_pat_col = self.__get_patient_pk_data_key(data = data) 121 122 if pk_pat_col is None: 123 gmDispatcher.send(signal = 'statustext', msg = _('List not known to be patient-related.')) 124 return 125 126 pat_data = data[pk_pat_col] 127 try: 128 pat_pk = int(pat_data) 129 pat = gmPerson.cPerson(aPK_obj = pat_pk) 130 except (ValueError, TypeError): 131 searcher = gmPersonSearch.cPatientSearcher_SQL() 132 idents = searcher.get_identities(pat_data) 133 if len(idents) == 0: 134 gmDispatcher.send(signal = 'statustext', msg = _('No matching patient found.')) 135 return 136 if len(idents) == 1: 137 pat = idents[0] 138 else: 139 from Gnumed.wxpython import gmPatSearchWidgets 140 dlg = gmPatSearchWidgets.cSelectPersonFromListDlg(parent=wx.GetTopLevelParent(self), id=-1) 141 dlg.set_persons(persons=idents) 142 result = dlg.ShowModal() 143 if result == wx.ID_CANCEL: 144 dlg.Destroy() 145 return 146 pat = dlg.get_selected_person() 147 dlg.Destroy() 148 except ConstructorError: 149 gmDispatcher.send(signal = 'statustext', msg = _('No matching patient found.')) 150 return 151 152 from Gnumed.wxpython import gmPatSearchWidgets 153 gmPatSearchWidgets.set_active_patient(patient = pat)159206 207 #================================================================ 208 from Gnumed.wxGladeWidgets import wxgDataMiningPnl 209161 162 try: 163 button_defs = kwargs['button_defs'][:5] 164 del kwargs['button_defs'] 165 except KeyError: 166 button_defs = [] 167 168 try: 169 msg = kwargs['message'] 170 del kwargs['message'] 171 except KeyError: 172 msg = None 173 174 wxgPatientListingPnl.wxgPatientListingPnl.__init__(self, *args, **kwargs) 175 176 if msg is not None: 177 self._lbl_msg.SetLabel(msg) 178 179 buttons = [self._BTN_1, self._BTN_2, self._BTN_3, self._BTN_4, self._BTN_5] 180 for idx in range(len(button_defs)): 181 button_def = button_defs[idx] 182 if button_def['label'].strip() == '': 183 continue 184 buttons[idx].SetLabel(button_def['label']) 185 buttons[idx].SetToolTip(button_def['tooltip']) 186 buttons[idx].Enable(True) 187 188 self.Fit()189 #------------------------------------------------------------ 190 # event handling 191 #------------------------------------------------------------193 event.Skip()194 #------------------------------------------------------------196 event.Skip()197 #------------------------------------------------------------199 event.Skip()200 #------------------------------------------------------------202 event.Skip()203 #------------------------------------------------------------205 event.Skip()211596 597 #================================================================ 598 # main 599 #---------------------------------------------------------------- 600 if __name__ == '__main__': 601 from Gnumed.pycommon import gmI18N, gmDateTime 602 603 gmI18N.activate_locale() 604 gmI18N.install_domain() 605 gmDateTime.init() 606 607 #------------------------------------------------------------213 wxgDataMiningPnl.wxgDataMiningPnl.__init__(self, *args, **kwargs) 214 215 self.__init_ui() 216 217 # make me a file drop target 218 dt = gmGuiHelpers.cFileDropTarget(target = self) 219 self.SetDropTarget(dt)220 #--------------------------------------------------------222 mp = gmMatchProvider.cMatchProvider_SQL2 ( 223 queries = [""" 224 SELECT DISTINCT ON (label) 225 cmd, 226 label 227 FROM cfg.report_query 228 WHERE 229 label %(fragment_condition)s 230 OR 231 cmd %(fragment_condition)s 232 """] 233 ) 234 mp.setThresholds(2,3,5) 235 self._PRW_report_name.matcher = mp 236 self._PRW_report_name.add_callback_on_selection(callback = self._on_report_selected) 237 self._PRW_report_name.add_callback_on_lose_focus(callback = self._auto_load_report)238 #--------------------------------------------------------240 if self._TCTRL_query.GetValue() == '': 241 if self._PRW_report_name.GetData() is not None: 242 self._TCTRL_query.SetValue(self._PRW_report_name.GetData()) 243 self._BTN_run.SetFocus()244 #-------------------------------------------------------- 248 #-------------------------------------------------------- 249 # file drop target API 250 #--------------------------------------------------------252 # act on first file only 253 fname = filenames[0] 254 _log.debug('importing SQL from <%s>', fname) 255 # act on text files only 256 mime_type = gmMimeLib.guess_mimetype(fname) 257 _log.debug('mime type: %s', mime_type) 258 if not mime_type.startswith('text/'): 259 _log.debug('not a text file') 260 gmDispatcher.send(signal='statustext', msg = _('Cannot read SQL from [%s]. Not a text file.') % fname, beep = True) 261 return False 262 # # act on "small" files only 263 # stat_val = os.stat(fname) 264 # if stat_val.st_size > 5000: 265 # gmDispatcher.send(signal='statustext', msg = _('Cannot read SQL from [%s]. File too big (> 2000 bytes).') % fname, beep = True) 266 # return False 267 # all checks passed 268 for line in fileinput.input(fname): 269 self._TCTRL_query.AppendText(line)270 #-------------------------------------------------------- 271 # notebook plugin API 272 #-------------------------------------------------------- 275 #-------------------------------------------------------- 276 # event handlers 277 #-------------------------------------------------------- 334 #-------------------------------------------------------- 338 #-------------------------------------------------------- 350 #-------------------------------------------------------- 355 #-------------------------------------------------------- 371 #-------------------------------------------------------- 419 420 #-------------------------------------------------------- 455 456 #-------------------------------------------------------- 515 516 #--------------------------------------------------------609 app = wx.PyWidgetTester(size = (400, 500)) 610 lst = cPatientListingCtrl(app.frame, patient_key = 0) 611 lst.set_columns(['name', 'comment']) 612 lst.set_string_items([ 613 ['Kirk', 'Kirk by name'], 614 ['#12', 'Kirk by ID'], 615 ['unknown', 'unknown patient'] 616 ]) 617 # app.SetWidget(cPatientListingCtrl, patient_key = 0) 618 app.frame.Show() 619 app.MainLoop()620 #------------------------------------------------------------ 621 622 test_pat_list_ctrl() 623
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Thu May 10 01:55:20 2018 | http://epydoc.sourceforge.net |