Changeset 643 for trunk/GSASIIIO.py
- Timestamp:
- May 30, 2012 2:12:46 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/GSASIIIO.py
r641 r643 1331 1331 1332 1332 def ExtractFileFromZip(filename, selection=None, confirmread=True, 1333 confirmoverwrite=True, parent=None): 1333 confirmoverwrite=True, parent=None, 1334 multipleselect=False): 1334 1335 '''If the filename is a zip file, extract a file from that archive. 1335 1336 selection is used to predefine the name of the file to be extracted … … 1340 1341 confirmoverwrite if True asks the user to confirm before 1341 1342 overwriting if the extracted file already exists 1343 multipleselect if True allows more than one zip file to be extracted, 1344 a list of file(s) is returned 1342 1345 If only one file is present, do not ask which one, otherwise offer a 1343 1346 list of choices (unless selection is used) 1344 Return the name of the file that has been created 1347 Return the name of the file that has been created or a list of files (see multipleselect) 1345 1348 If the file is not a zipfile, return the name of the input file. 1346 1349 If the zipfile is empty or no file has been selected, return None … … 1358 1361 if len(zinfo) == 0: 1359 1362 #print('Zip has no files!') 1360 z index = -11363 zlist = [-1] 1361 1364 if selection: 1362 1365 choices = [os.path.split(i.filename)[1].lower() for i in zinfo] 1363 1366 if selection.lower() in choices: 1364 z index = choices.index(selection.lower())1367 zlist = [choices.index(selection.lower())] 1365 1368 else: 1366 1369 print('debug: file '+str(selection)+' was not found in '+str(filename)) 1367 z index = -11370 zlist = [-1] 1368 1371 elif len(zinfo) == 1 and confirmread: 1369 1372 result = wx.ID_NO … … 1380 1383 dlg.Destroy() 1381 1384 if result == wx.ID_NO: 1382 z index = -11385 zlist = [-1] 1383 1386 else: 1384 z index = 01387 zlist = [0] 1385 1388 elif len(zinfo) == 1: 1386 zindex = 0 1389 zlist = [0] 1390 elif multipleselect: 1391 # select one or more from a from list 1392 choices = [i.filename for i in zinfo] 1393 dlg = wx.MultiChoiceDialog( 1394 parent, 1395 'Select file(s) to extract from zip file'+str(filename), 1396 'Choose file(s)', 1397 choices, 1398 wx.CHOICEDLG_STYLE, 1399 ) 1400 if dlg.ShowModal() == wx.ID_OK: 1401 zlist = dlg.GetSelections() 1402 dlg.Destroy() 1403 else: 1404 dlg.Destroy() 1405 zlist = [] 1387 1406 else: 1388 # select from list1407 # select one from a from list 1389 1408 choices = [i.filename for i in zinfo] 1390 1409 dlg = wx.SingleChoiceDialog( … … 1395 1414 ) 1396 1415 if dlg.ShowModal() == wx.ID_OK: 1397 z index = dlg.GetSelection()1416 zlist = [dlg.GetSelection()] 1398 1417 dlg.Destroy() 1399 1418 else: 1400 1419 dlg.Destroy() 1401 z index = -11420 zlist = [-1] 1402 1421 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 1422 outlist = [] 1423 for zindex in zlist: 1424 if zindex >= 0: 1425 efil = os.path.join(zloc, os.path.split(zinfo[zindex].filename)[1]) 1426 if os.path.exists(efil) and confirmoverwrite: 1427 result = wx.ID_NO 1428 dlg = wx.MessageDialog( 1429 parent, 1430 'File '+str(efil)+' already exists. OK to overwrite it?', 1431 'Confirm overwrite', 1432 wx.YES_NO | wx.ICON_QUESTION) 1433 try: 1434 result = dlg.ShowModal() 1435 finally: 1436 dlg.Destroy() 1437 if result == wx.ID_NO: 1438 zindex = -1 1439 if zindex >= 0: 1440 # extract the file to the current directory, regardless of it's original path 1441 #z.extract(zinfo[zindex],zloc) 1442 eloc,efil = os.path.split(zinfo[zindex].filename) 1443 outfile = os.path.join(zloc, efil) 1444 fpin = z.open(zinfo[zindex]) 1445 fpout = file(outfile, "wb") 1446 shutil.copyfileobj(fpin, fpout) 1447 fpin.close() 1448 fpout.close() 1449 outlist.append(outfile) 1430 1450 z.close() 1431 return None 1451 if multipleselect and len(outlist) >= 1: 1452 return outlist 1453 elif len(outlist) == 1: 1454 return outlist[0] 1455 else: 1456 return None 1432 1457 1433 1458 ###################################################################### … … 1732 1757 #selection=None, confirmoverwrite=True, parent=None 1733 1758 #print ExtractFileFromZip(filename, selection='11bmb_7652.fxye',parent=frm) 1734 print ExtractFileFromZip(filename )1759 print ExtractFileFromZip(filename,multipleselect=True) 1735 1760 #confirmread=False, confirmoverwrite=False) 1736 1761
Note: See TracChangeset
for help on using the changeset viewer.