Package Gnumed :: Package wxpython :: Module gmDataPackWidgets
[frames] | no frames]

Source Code for Module Gnumed.wxpython.gmDataPackWidgets

  1  """GNUmed data pack related widgets.""" 
  2  #================================================================ 
  3  __author__ = 'karsten.hilbert@gmx.net' 
  4  __license__ = 'GPL v2 or later (details at http://www.gnu.org)' 
  5   
  6  # stdlib 
  7  import logging 
  8  import sys 
  9  import urllib2 as wget 
 10   
 11  # 3rd party 
 12  import wx 
 13   
 14   
 15  # GNUmed 
 16  if __name__ == '__main__': 
 17          sys.path.insert(0, '../../') 
 18   
 19  from Gnumed.pycommon import gmCfg2 
 20  from Gnumed.pycommon import gmCfg 
 21  from Gnumed.pycommon import gmTools 
 22  from Gnumed.pycommon import gmNetworkTools 
 23  from Gnumed.business import gmSurgery 
 24  from Gnumed.wxpython import gmListWidgets 
 25  from Gnumed.wxpython import gmGuiHelpers 
 26  from Gnumed.wxpython import gmAuthWidgets 
 27   
 28   
 29  _log = logging.getLogger('gm.ui') 
 30  _cfg = gmCfg2.gmCfgData() 
 31  #================================================================ 
32 -def install_data_pack(data_pack=None):
33 34 if data_pack is None: 35 return False 36 37 _log.info('attempting installation of data pack: %s', data_pack['name']) 38 39 gm_dbo_conn = gmAuthWidgets.get_dbowner_connection(procedure = _('installing data packs')) 40 if gm_dbo_conn is None: 41 msg = _('Lacking permissions to install data pack.') 42 gmGuiHelpers.gm_show_error(msg, _('Installing data pack')) 43 return False 44 45 wx.BeginBusyCursor() 46 verified, data = gmNetworkTools.download_data_pack ( 47 data_pack['pack_url'], 48 md5_url = data_pack['md5_url'] 49 ) 50 wx.EndBusyCursor() 51 if not verified: 52 _log.error('cannot download and verify data pack: %s', data_pack['name']) 53 md5_expected, md5_calculated = data 54 msg = _( 55 'Cannot validate data pack.\n' 56 '\n' 57 ' name: %s\n' 58 ' URL: %s\n' 59 '\n' 60 ' MD5\n' 61 ' calculated: %s\n' 62 ' expected: %s\n' 63 ' source: %s\n' 64 '\n' 65 'You may want to try downloading again or you\n' 66 'may need to contact your administrator.' 67 ) % ( 68 data_pack['name'], 69 data_pack['pack_url'], 70 md5_calculated, 71 md5_expected, 72 data_pack['md5_url'] 73 ) 74 gmGuiHelpers.gm_show_error(msg, _('Verifying data pack')) 75 return False 76 77 data_pack['local_archive'] = data 78 79 wx.BeginBusyCursor() 80 unzip_dir = gmNetworkTools.unzip_data_pack(filename = data) 81 wx.EndBusyCursor() 82 if unzip_dir is None: 83 msg = _( 84 'Cannot unpack data pack.\n' 85 '\n' 86 ' name: %s\n' 87 ' URL: %s\n' 88 ' local: %s\n' 89 '\n' 90 'You may want to try downloading again or you\n' 91 'may need to contact your administrator.' 92 ) % ( 93 data_pack['name'], 94 data_pack['pack_url'], 95 data_pack['local_archive'] 96 ) 97 gmGuiHelpers.gm_show_error(msg, _('Unpacking data pack')) 98 return False 99 100 data_pack['unzip_dir'] = unzip_dir 101 102 wx.BeginBusyCursor() 103 try: 104 installed = gmNetworkTools.install_data_pack(data_pack, gm_dbo_conn) 105 finally: 106 wx.EndBusyCursor() 107 108 if not installed: 109 msg = _( 110 'Installation of data pack failed.\n' 111 '\n' 112 ' name: %s\n' 113 ' URL: %s\n' 114 ' local: %s\n' 115 '\n' 116 'You may want to try downloading again or you\n' 117 'may need to contact your administrator.' 118 ) % ( 119 data_pack['name'], 120 data_pack['pack_url'], 121 data_pack['local_archive'] 122 ) 123 gmGuiHelpers.gm_show_error(msg, _('Installing data pack')) 124 return False 125 126 msg = _( 127 'Successfully installed data pack.\n' 128 '\n' 129 ' name: %s\n' 130 ' URL: %s\n' 131 ) % ( 132 data_pack['name'], 133 data_pack['pack_url'] 134 ) 135 gmGuiHelpers.gm_show_info(msg, _('Installing data pack')) 136 137 return True
138 #----------------------------------------------------------------
139 -def load_data_packs_list():
140 141 dbcfg = gmCfg.cCfgSQL() 142 dpl_url = dbcfg.get2 ( 143 option = u'horstspace.data_packs.url', 144 workplace = gmSurgery.gmCurrentPractice().active_workplace, 145 bias = 'workplace', 146 default = u'http://www.gnumed.de/downloads/data/data-packs.conf' 147 ) 148 149 items = [] 150 data = [] 151 152 dpl_fname = gmNetworkTools.download_data_packs_list(dpl_url) 153 if dpl_fname is None: 154 return (items, data) 155 try: 156 _cfg.add_file_source(source = 'data-packs', file = dpl_fname) 157 except (UnicodeDecodeError): 158 _log.exception("cannot read data pack list from [%s]", dpl_fname) 159 return (items, data) 160 161 packs = _cfg.get('data packs', 'data packs', source_order = [('data-packs', 'return')]) 162 if packs is None: 163 _log.info('no data packs listed in data packs list file') 164 _cfg.remove_source('data-packs') 165 return (items, data) 166 167 for pack in packs: 168 _log.debug('reading pack [%s] metadata', pack) 169 pack_group = u'pack %s' % pack 170 name = _cfg.get(pack_group, u'name', source_order = [('data-packs', 'return')]) 171 pack_url = _cfg.get(pack_group, u'URL', source_order = [('data-packs', 'return')]) 172 md5_url = pack_url + u'.md5' 173 db_min = _cfg.get(pack_group, u'minimum database version', source_order = [('data-packs', 'return')]) 174 converted, db_min = gmTools.input2int ( 175 db_min, 176 # here we introduced data packs: 177 #16, 178 0, 179 # no use looking at data packs requiring a database > the current database: 180 _cfg.get(option = 'database_version') 181 ) 182 if not converted: 183 _log.error('cannot convert minimum database version [%s]', db_min) 184 continue 185 186 db_max = _cfg.get(pack_group, u'maximum database version', source_order = [('data-packs', 'return')]) 187 if db_max is None: 188 db_max = sys.maxint 189 converted, db_max = gmTools.input2int ( 190 db_max, 191 db_min # max version must be at least db_min 192 ) 193 if not converted: 194 _log.error('cannot convert maximum database version [%s]', db_max) 195 continue 196 197 if _cfg.get(option = 'database_version') < db_min: 198 _log.error('ignoring data pack: current database version (%s) < minimum required database version (%s)', _cfg.get(option = 'database_version'), db_min) 199 continue 200 201 if _cfg.get(option = 'database_version') > db_max: 202 _log.error('ignoring data pack: current database version (%s) > maximum allowable database version (%s)', _cfg.get(option = 'database_version'), db_max) 203 continue 204 205 items.append([name, u'v%s' % db_min, u'v%s' % db_max, pack_url]) 206 data.append ({ 207 'name': name, 208 'pack_url': pack_url, 209 'md5_url': md5_url, 210 'db_min': db_min, 211 'db_max': db_max 212 }) 213 214 _cfg.remove_source('data-packs') 215 return (items, data)
216 #----------------------------------------------------------------
217 -def manage_data_packs(parent=None):
218 219 if parent is None: 220 parent = wx.GetApp().GetTopWindow() 221 222 items, data = load_data_packs_list() 223 224 gmListWidgets.get_choices_from_list ( 225 parent = parent, 226 msg = _( 227 'Data packs available for installation into this v%s database.\n' 228 ) % ( 229 _cfg.get(option = 'database_version') 230 ), 231 caption = _('Showing data packs.'), 232 columns = [ _('Data pack'), _('min DB'), _('max DB'), _('Source') ], 233 choices = items, 234 data = data, 235 single_selection = True, 236 can_return_empty = False, 237 ignore_OK_button = True, 238 left_extra_button = ( 239 _('&Install'), 240 _('Install the selected data pack'), 241 install_data_pack 242 ), 243 # middle_extra_button=None, 244 # right_extra_button=None # configure 245 )
246 247 #================================================================ 248 # main 249 #---------------------------------------------------------------- 250 if __name__ == '__main__': 251 252 if len(sys.argv) < 2: 253 sys.exit() 254 255 if sys.argv[1] != 'test': 256 sys.exit() 257 258 from Gnumed.pycommon import gmI18N 259 gmI18N.activate_locale() 260 gmI18N.install_domain() 261 # from Gnumed.pycommon import gmPG2 262 263 #-------------------------------------------------------- 264 # def test_generic_codes_prw(): 265 # gmPG2.get_connection() 266 # app = wx.PyWidgetTester(size = (500, 40)) 267 # pw = cGenericCodesPhraseWheel(app.frame, -1) 268 # #pw.set_context(context = u'zip', val = u'04318') 269 # app.frame.Show(True) 270 # app.MainLoop() 271 # #-------------------------------------------------------- 272 # test_generic_codes_prw() 273 274 #================================================================ 275