Changeset 4654 for trunk/GSASIIfiles.py


Ignore:
Timestamp:
Nov 10, 2020 11:24:39 AM (12 months ago)
Author:
toby
Message:

new video tutorial; misc minor RB GUI fixes for Windows; option to show GPX sizes; remove use of deprecated imp module

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIfiles.py

    r4635 r4654  
    2828import inspect
    2929import numpy as np
    30 import imp
    3130
    3231import GSASIIpath
     
    388387
    389388    readerlist = []
    390     pathlist = sys.path[:]
    391     if '.' not in pathlist:
    392         pathlist.append('.')
    393 
    394     potential_files = []
    395     for path in pathlist:
     389    import_files = {}
     390    if '.' not in sys.path: sys.path.append('.')
     391    for path in sys.path:
    396392        for filename in glob.iglob(os.path.join(path, 'G2'+prefix+'*.py')):
    397             potential_files.append(filename)
    398 
    399     potential_files = sorted(list(set(potential_files)))
    400     for filename in potential_files:
    401         path, rootname = os.path.split(filename)
    402         pkg = os.path.splitext(rootname)[0]
    403 
     393            pkg = os.path.splitext(os.path.split(filename)[1])[0]
     394            if pkg in import_files:
     395                G2Print('Warning: file {} hidden by {}'
     396                                  .format(os.path.abspath(filename),import_files[pkg]))
     397            else:
     398                import_files[pkg] = filename           
     399
     400    for pkg in sorted(import_files.keys()):
    404401        try:
    405             fp = None
    406             fp, fppath, desc = imp.find_module(pkg, [path])
    407             pkg = imp.load_module(pkg, fp, fppath, desc)
    408             for name, value in inspect.getmembers(pkg):
     402            exec('import '+pkg)
     403            for name, value in inspect.getmembers(eval(pkg)):
    409404                if name.startswith('_'):
    410405                    continue
     
    420415                            readerlist.append(reader)
    421416        except AttributeError:
    422             G2Print ('Import_' + errprefix + ': Attribute Error ' + filename)
     417            G2Print ('Import_' + errprefix + ': Attribute Error ' + import_files[pkg])
    423418            if traceback:
    424419                traceback.print_exc(file=sys.stdout)
    425420        except Exception as exc:
    426             G2Print ('\nImport_' + errprefix + ': Error importing file ' + filename)
     421            G2Print ('\nImport_' + errprefix + ': Error importing file ' + import_files[pkg])
    427422            G2Print (u'Error message: {}\n'.format(exc))
    428423            if traceback:
    429424                traceback.print_exc(file=sys.stdout)
    430         finally:
    431             if fp:
    432                 fp.close()
    433 
    434425    return readerlist
    435426
     
    438429    '''
    439430    exporterlist = []
    440     pathlist = sys.path
    441     filelist = []
    442     for path in pathlist:
     431    import_files = {}
     432    if '.' not in sys.path: sys.path.append('.')
     433    for path in sys.path:
    443434        for filename in glob.iglob(os.path.join(path,"G2export*.py")):
    444                     filelist.append(filename)   
    445     filelist = sorted(list(set(filelist))) # remove duplicates
     435            pkg = os.path.splitext(os.path.split(filename)[1])[0]
     436            if pkg in import_files:
     437                G2Print('Warning: file {} hidden by {}'
     438                                  .format(os.path.abspath(filename),import_files[pkg]))
     439            else:
     440                import_files[pkg] = filename           
    446441    # go through the routines and import them, saving objects that
    447442    # have export routines (method Exporter)
    448     for filename in filelist:
    449         path,rootname = os.path.split(filename)
    450         pkg = os.path.splitext(rootname)[0]
     443    for pkg in sorted(import_files.keys()):
    451444        try:
    452             fp = None
    453             fp, fppath,desc = imp.find_module(pkg,[path,])
    454             pkg = imp.load_module(pkg,fp,fppath,desc)
    455             for clss in inspect.getmembers(pkg): # find classes defined in package
     445            exec('import '+pkg)
     446            for clss in inspect.getmembers(eval(pkg)): # find classes defined in package
    456447                if clss[0].startswith('_'): continue
    457448                if not inspect.isclass(clss[1]): continue
     
    484475            if traceback:
    485476                traceback.print_exc(file=sys.stdout)
    486         finally:
    487             if fp:
    488                 fp.close()
    489477    return exporterlist
    490478
Note: See TracChangeset for help on using the changeset viewer.