Changeset 643


Ignore:
Timestamp:
May 30, 2012 2:12:46 PM (9 years ago)
Author:
toby
Message:

allow multiple selection from zip on data input, read structure CIFs w/o atom type

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASII.py

    r641 r643  
    298298            dlg.Destroy()
    299299        rd_list = []
     300        filelist1 = []
     301        for filename in filelist:
     302            # is this a zip file?
     303            if os.path.splitext(filename)[1].lower() == '.zip':
     304                extractedfiles = G2IO.ExtractFileFromZip(
     305                    filename,parent=self,
     306                    multipleselect=True)
     307                if extractedfiles is None: continue # error or Cancel
     308                if extractedfiles != filename:
     309                    self.zipfile = filename # save zip name
     310                    filelist1 += extractedfiles
     311                    continue
     312            filelist1.append(filename)
     313        filelist = filelist1
    300314        for filename in filelist:
    301315            # is this a zip file?
  • trunk/GSASIIIO.py

    r641 r643  
    13311331           
    13321332def ExtractFileFromZip(filename, selection=None, confirmread=True,
    1333                        confirmoverwrite=True, parent=None):
     1333                       confirmoverwrite=True, parent=None,
     1334                       multipleselect=False):
    13341335    '''If the filename is a zip file, extract a file from that archive.
    13351336      selection is used to predefine the name of the file to be extracted
     
    13401341      confirmoverwrite if True asks the user to confirm before
    13411342        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
    13421345    If only one file is present, do not ask which one, otherwise offer a
    13431346       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)
    13451348      If the file is not a zipfile, return the name of the input file.
    13461349      If the zipfile is empty or no file has been selected, return None
     
    13581361    if len(zinfo) == 0:
    13591362        #print('Zip has no files!')
    1360         zindex = -1
     1363        zlist = [-1]
    13611364    if selection:
    13621365        choices = [os.path.split(i.filename)[1].lower() for i in zinfo]
    13631366        if selection.lower() in choices:
    1364             zindex = choices.index(selection.lower())
     1367            zlist = [choices.index(selection.lower())]
    13651368        else:
    13661369            print('debug: file '+str(selection)+' was not found in '+str(filename))
    1367             zindex = -1
     1370            zlist = [-1]
    13681371    elif len(zinfo) == 1 and confirmread:
    13691372        result = wx.ID_NO
     
    13801383            dlg.Destroy()
    13811384        if result == wx.ID_NO:
    1382             zindex = -1
     1385            zlist = [-1]
    13831386        else:
    1384             zindex = 0
     1387            zlist = [0]
    13851388    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 = []
    13871406    else:
    1388         # select from list
     1407        # select one from a from list
    13891408        choices = [i.filename for i in zinfo]
    13901409        dlg = wx.SingleChoiceDialog(
     
    13951414            )
    13961415        if dlg.ShowModal() == wx.ID_OK:
    1397             zindex = dlg.GetSelection()
     1416            zlist = [dlg.GetSelection()]
    13981417            dlg.Destroy()
    13991418        else:
    14001419            dlg.Destroy()
    1401             zindex = -1
     1420            zlist = [-1]
    14021421       
    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)
    14301450    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
    14321457
    14331458######################################################################
     
    17321757    #selection=None, confirmoverwrite=True, parent=None
    17331758    #print ExtractFileFromZip(filename, selection='11bmb_7652.fxye',parent=frm)
    1734     print ExtractFileFromZip(filename)
     1759    print ExtractFileFromZip(filename,multipleselect=True)
    17351760                             #confirmread=False, confirmoverwrite=False)
    17361761
  • trunk/imports/G2phase_CIF.py

    r614 r643  
    1212import GSASIIIO as G2IO
    1313import GSASIIspc as G2spc
     14import GSASIIElem as G2elem
    1415import GSASIIlattice as G2lat
    1516import CifFile as cif # PyCifRW from James Hester
     
    4748            '_cell_angle_alpha','_cell_angle_beta','_cell_angle_gamma',)
    4849        reqitems = (
    49              '_atom_site_type_symbol',
    5050             '_atom_site_fract_x',
    5151             '_atom_site_fract_y',
     
    181181                        elif key == '_atom_site_u_iso_or_equiv':
    182182                            atomlist[10] =cif.get_number_with_esd(val)[0]
     183                    if not atomlist[1] and atomlist[0]:
     184                        for i in range(2,0,-1):
     185                            typ = atomlist[0].strip()[:i]
     186                            if G2elem.CheckElement(typ):
     187                                atomlist[1] = typ
     188                            if not atomlist[1]: atomlist[1] = 'Xe'
    183189                    ulbl = '_atom_site_aniso_label'
    184190                    if  atomlist[9] == 'A' and atomlist[0] in blk.get(ulbl):
Note: See TracChangeset for help on using the changeset viewer.