Changeset 3216 for trunk/GSASIIfiles.py


Ignore:
Timestamp:
Jan 7, 2018 9:06:19 PM (4 years ago)
Author:
toby
Message:

add data & parameter access in scripting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIfiles.py

    r3207 r3216  
    369369    return readerlist
    370370
    371 def LoadExportRoutines():
    372     '''Placeholder that will someday retrieve the exporters
     371def LoadExportRoutines(parent, traceback=False):
     372    '''Routine to locate GSASII exporters
    373373    '''
    374     pass
     374    exporterlist = []
     375    pathlist = sys.path
     376    filelist = []
     377    for path in pathlist:
     378        for filename in glob.iglob(os.path.join(path,"G2export*.py")):
     379                    filelist.append(filename)   
     380    filelist = sorted(list(set(filelist))) # remove duplicates
     381    # go through the routines and import them, saving objects that
     382    # have export routines (method Exporter)
     383    for filename in filelist:
     384        path,rootname = os.path.split(filename)
     385        pkg = os.path.splitext(rootname)[0]
     386        try:
     387            fp = None
     388            fp, fppath,desc = imp.find_module(pkg,[path,])
     389            pkg = imp.load_module(pkg,fp,fppath,desc)
     390            for clss in inspect.getmembers(pkg): # find classes defined in package
     391                if clss[0].startswith('_'): continue
     392                if not inspect.isclass(clss[1]): continue
     393                # check if we have the required methods
     394                if not hasattr(clss[1],'Exporter'): continue
     395                if not callable(getattr(clss[1],'Exporter')): continue
     396                if parent is None:
     397                    if not hasattr(clss[1],'Writer'): continue
     398                else:
     399                    if not hasattr(clss[1],'loadParmDict'): continue
     400                    if not callable(getattr(clss[1],'loadParmDict')): continue
     401                try:
     402                    exporter = clss[1](parent) # create an export instance
     403                except AttributeError:
     404                    pass
     405                except Exception as exc:
     406                    print ('\nExport init: Error substantiating class ' + clss[0])
     407                    print (u'Error message: {}\n'.format(exc))
     408                    if traceback:
     409                        traceback.print_exc(file=sys.stdout)
     410                    continue
     411                exporterlist.append(exporter)
     412        except AttributeError:
     413            print ('Export Attribute Error ' + filename)
     414            if traceback:
     415                traceback.print_exc(file=sys.stdout)
     416        except Exception as exc:
     417            print ('\nExport init: Error importing file ' + filename)
     418            print (u'Error message: {}\n'.format(exc))
     419            if traceback:
     420                traceback.print_exc(file=sys.stdout)
     421        finally:
     422            if fp:
     423                fp.close()
     424    return exporterlist
Note: See TracChangeset for help on using the changeset viewer.