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