Changeset 641 for trunk/GSASIIIO.py
- Timestamp:
- May 29, 2012 10:23:48 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/GSASIIIO.py
r630 r641 1330 1330 self.EndModal(wx.ID_CANCEL) 1331 1331 1332 def ExtractFileFromZip(filename, selection=None, confirmread=True, 1333 confirmoverwrite=True, parent=None): 1334 '''If the filename is a zip file, extract a file from that archive. 1335 selection is used to predefine the name of the file to be extracted 1336 filename case and zip directory name are ignored in selection; 1337 the first matching file is used 1338 confirmread if True asks the user to confirm before expanding 1339 the only file in a zip 1340 confirmoverwrite if True asks the user to confirm before 1341 overwriting if the extracted file already exists 1342 If only one file is present, do not ask which one, otherwise offer a 1343 list of choices (unless selection is used) 1344 Return the name of the file that has been created 1345 If the file is not a zipfile, return the name of the input file. 1346 If the zipfile is empty or no file has been selected, return None 1347 ''' 1348 import zipfile # do this now, since we can save startup time by doing this only on need 1349 import shutil 1350 zloc = os.path.split(filename)[0] 1351 if not zipfile.is_zipfile(filename): 1352 #print("not zip") 1353 return filename 1354 1355 z = zipfile.ZipFile(filename,'r') 1356 zinfo = z.infolist() 1357 1358 if len(zinfo) == 0: 1359 #print('Zip has no files!') 1360 zindex = -1 1361 if selection: 1362 choices = [os.path.split(i.filename)[1].lower() for i in zinfo] 1363 if selection.lower() in choices: 1364 zindex = choices.index(selection.lower()) 1365 else: 1366 print('debug: file '+str(selection)+' was not found in '+str(filename)) 1367 zindex = -1 1368 elif len(zinfo) == 1 and confirmread: 1369 result = wx.ID_NO 1370 dlg = wx.MessageDialog( 1371 parent, 1372 'Is file '+str(zinfo[0].filename)+ 1373 ' what you want to extract from '+ 1374 str(os.path.split(filename)[1])+'?', 1375 'Confirm file', 1376 wx.YES_NO | wx.ICON_QUESTION) 1377 try: 1378 result = dlg.ShowModal() 1379 finally: 1380 dlg.Destroy() 1381 if result == wx.ID_NO: 1382 zindex = -1 1383 else: 1384 zindex = 0 1385 elif len(zinfo) == 1: 1386 zindex = 0 1387 else: 1388 # select from list 1389 choices = [i.filename for i in zinfo] 1390 dlg = wx.SingleChoiceDialog( 1391 parent, 1392 'Select file to extract from zip file'+str(filename), 1393 'Choose file', 1394 choices, 1395 ) 1396 if dlg.ShowModal() == wx.ID_OK: 1397 zindex = dlg.GetSelection() 1398 dlg.Destroy() 1399 else: 1400 dlg.Destroy() 1401 zindex = -1 1402 1403 if zindex >= 0: 1404 efil = os.path.join(zloc, os.path.split(zinfo[zindex].filename)[1]) 1405 if os.path.exists(efil) and confirmoverwrite: 1406 result = wx.ID_NO 1407 dlg = wx.MessageDialog( 1408 parent, 1409 'File '+str(efil)+' already exists. OK to overwrite it?', 1410 'Confirm overwrite', 1411 wx.YES_NO | wx.ICON_QUESTION) 1412 try: 1413 result = dlg.ShowModal() 1414 finally: 1415 dlg.Destroy() 1416 if result == wx.ID_NO: 1417 zindex = -1 1418 if zindex >= 0: 1419 # extract the file to the current directory, regardless of it's original path 1420 eloc,efil = os.path.split(zinfo[zindex].filename) 1421 outfile = os.path.join(zloc, efil) 1422 fpin = z.open(zinfo[zindex]) 1423 fpout = file(outfile, "wb") 1424 shutil.copyfileobj(fpin, fpout) 1425 fpin.close() 1426 fpout.close() 1427 #z.extract(zinfo[zindex],zloc) 1428 z.close() 1429 return outfile 1430 z.close() 1431 return None 1432 1332 1433 ###################################################################### 1333 1434 # base classes for reading various types of data files … … 1624 1725 app = wx.PySimpleApp() 1625 1726 frm = wx.Frame(None) # create a frame 1626 choicelist=[ ('a','b','c'), 1627 ('test1','test2'),('no choice',)] 1628 titles = [ 'a, b or c', 'tests', 'No option here'] 1629 dlg = MultipleChoicesDialog( 1630 choicelist,titles, 1631 parent=frm) 1632 if dlg.ShowModal() == wx.ID_OK: 1633 print 'Got OK' 1727 frm.Show(True) 1728 filename = '/tmp/notzip.zip' 1729 filename = '/tmp/all.zip' 1730 #filename = '/tmp/11bmb_7652.zip' 1731 1732 #selection=None, confirmoverwrite=True, parent=None 1733 #print ExtractFileFromZip(filename, selection='11bmb_7652.fxye',parent=frm) 1734 print ExtractFileFromZip(filename) 1735 #confirmread=False, confirmoverwrite=False) 1736 1737 # choicelist=[ ('a','b','c'), 1738 # ('test1','test2'),('no choice',)] 1739 # titles = [ 'a, b or c', 'tests', 'No option here'] 1740 # dlg = MultipleChoicesDialog( 1741 # choicelist,titles, 1742 # parent=frm) 1743 # if dlg.ShowModal() == wx.ID_OK: 1744 # print 'Got OK'
Note: See TracChangeset
for help on using the changeset viewer.