Changeset 641 for trunk


Ignore:
Timestamp:
May 29, 2012 10:23:48 PM (13 years ago)
Author:
toby
Message:

add support for zipped data and zip up largest exercise files

Location:
trunk
Files:
9 added
3 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/GSASII.py

    r636 r641  
    243243        '''
    244244        self.lastimport = ''
     245        self.zipfile = None
    245246        if reader is None:
    246247            multiple = False
    247248            #print "use all formats"
    248249            choices = "any file (*.*)|*.*"
     250            choices += "|zip archive (.zip)|*.zip"
    249251            extdict = {}
    250252            # compile a list of allowed extensions
     
    269271                w += "*" + extn
    270272            choices += w + ")|" + w
     273            choices += "|zip archive (.zip)|*.zip"
    271274            if not reader.strictExtension:
    272275                choices += "|any file (*.*)|*.*"
     
    296299        rd_list = []
    297300        for filename in filelist:
    298         # set what formats are compatible with this file
     301            # is this a zip file?
     302            if os.path.splitext(filename)[1].lower() == '.zip':
     303                extractedfile = G2IO.ExtractFileFromZip(filename,parent=self)
     304                if extractedfile is None: continue # error or Cancel
     305                if extractedfile != filename:
     306                    filename,self.zipfile = extractedfile,filename # now use the file that was created
     307            # set what formats are compatible with this file
    299308            primaryReaders = []
    300309            secondaryReaders = []
     
    596605                print 'debug: open/read failed',instfile
    597606                pass # fail silently
    598        
     607
     608        # did we read the data file from a zip? If so, look there for a
     609        # instrument parameter file
     610        if self.zipfile:
     611            for ext in '.inst','.prm':
     612                instfile = G2IO.ExtractFileFromZip(
     613                    self.zipfile,
     614                    selection=os.path.split(basename + ext)[1],
     615                    parent=self)
     616                if instfile is not None and instfile != self.zipfile:
     617                    print 'debug:',instfile,'created from ',self.zipfile
     618                    Iparm = self.ReadPowderIparm(instfile,bank,numbanks,rd)
     619                    if Iparm:
     620                        rd.instfile = instfile
     621                        rd.instmsg = instfile + ' bank ' + str(rd.instbank)
     622                        return Iparm
     623                    else:
     624                        print 'debug: open/read for',instfile,'from',self.zipfile,'failed'
     625                        pass # fail silently
     626
    599627        while True: # loop until we get a file that works or we get a cancel
    600628            instfile = ''
     
    10241052    def OnImageRead(self,event):
    10251053        self.CheckNotebook()
    1026         dlg = wx.FileDialog(self, 'Choose image files', '.', '',\
    1027         'Any image file (*.tif;*.tiff;*.mar*;*.avg;*.sum;*.img;*.G2img)\
    1028         |*.tif;*.tiff;*.mar*;*.avg;*.sum;*.img;*.G2img|\
    1029         Any detector tif (*.tif;*.tiff)|*.tif;*.tiff|\
    1030         MAR file (*.mar*)|*.mar*|\
    1031         GE Image (*.avg;*.sum)|*.avg;*.sum|\
    1032         ADSC Image (*.img)|*.img|\
    1033         GSAS-II Image (*.G2img)|*.G2img|\
    1034         All files (*.*)|*.*',
    1035         wx.OPEN | wx.MULTIPLE|wx.CHANGE_DIR)
     1054        dlg = wx.FileDialog(
     1055            self, 'Choose image files', '.', '',
     1056            'Any image file (*.tif;*.tiff;*.mar*;*.avg;*.sum;*.img;*.G2img)|'
     1057            '*.tif;*.tiff;*.mar*;*.avg;*.sum;*.img;*.G2img;*.zip|'
     1058            'Any detector tif (*.tif;*.tiff)|*.tif;*.tiff|'
     1059            'MAR file (*.mar*)|*.mar*|'
     1060            'GE Image (*.avg;*.sum)|*.avg;*.sum|'
     1061            'ADSC Image (*.img)|*.img|'
     1062            'GSAS-II Image (*.G2img)|*.G2img|'
     1063            'Zip archive (*.zip)|*.zip|'
     1064            'All files (*.*)|*.*',
     1065            wx.OPEN | wx.MULTIPLE|wx.CHANGE_DIR)
    10361066        try:
    10371067            if dlg.ShowModal() == wx.ID_OK:
     
    10391069                imagefiles.sort()
    10401070                for imagefile in imagefiles:
     1071                    # if a zip file, open and extract
     1072                    if os.path.splitext(imagefile)[1].lower() == '.zip':
     1073                        extractedfile = G2IO.ExtractFileFromZip(imagefile,parent=self)
     1074                        if extractedfile is not None and extractedfile != imagefile:
     1075                            imagefile = extractedfile
    10411076                    Comments,Data,Npix,Image = G2IO.GetImageData(self,imagefile)
    10421077                    if Comments:
  • TabularUnified trunk/GSASIIIO.py

    r630 r641  
    13301330        self.EndModal(wx.ID_CANCEL)             
    13311331           
     1332def 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
    13321433######################################################################
    13331434# base classes for reading various types of data files
     
    16241725    app = wx.PySimpleApp()
    16251726    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'
  • TabularUnified trunk/imports/G2pwd_xye.py

    r614 r641  
    4949                print 'ContentsValidator: '+self.formatName
    5050                print 'Unexpected information in line:',i+1 # debug info
    51                 print line
     51                print S
    5252                return False
    5353        return True # no errors encountered
Note: See TracChangeset for help on using the changeset viewer.