Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/loader.py

    r463e7ffc rdcb91cf  
    11""" 
    22    File handler to support different file extensions. 
    3     Uses reflectometry's registry utility. 
     3    Uses reflectometer registry utility. 
    44 
    55    The default readers are found in the 'readers' sub-module 
     
    1414""" 
    1515##################################################################### 
    16 #This software was developed by the University of Tennessee as part of the 
    17 #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
    18 #project funded by the US National Science Foundation. 
    19 #See the license text in license.txt 
    20 #copyright 2008, University of Tennessee 
     16# This software was developed by the University of Tennessee as part of the 
     17# Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
     18# project funded by the US National Science Foundation. 
     19# See the license text in license.txt 
     20# copyright 2008, University of Tennessee 
    2121###################################################################### 
    2222 
     
    2929# Default readers are defined in the readers sub-module 
    3030import readers 
     31from loader_exceptions import NoKnownLoaderException, FileContentsException,\ 
     32    DefaultReaderException 
    3133from readers import ascii_reader 
    3234from readers import cansas_reader 
     35from readers import cansas_reader_HDF5 
    3336 
    3437logger = logging.getLogger(__name__) 
     38 
    3539 
    3640class Registry(ExtensionRegistry): 
     
    3943    Readers and writers are supported. 
    4044    """ 
    41  
    4245    def __init__(self): 
    4346        super(Registry, self).__init__() 
    4447 
    45         ## Writers 
     48        # Writers 
    4649        self.writers = {} 
    4750 
    48         ## List of wildcards 
     51        # List of wildcards 
    4952        self.wildcards = ['All (*.*)|*.*'] 
    5053 
    51         ## Creation time, for testing 
     54        # Creation time, for testing 
    5255        self._created = time.time() 
    5356 
     
    6366            of a particular reader 
    6467 
    65         Defaults to the ascii (multi-column) reader 
    66         if no reader was registered for the file's 
    67         extension. 
    68         """ 
     68        Defaults to the ascii (multi-column), cansas XML, and cansas NeXuS 
     69        readers if no reader was registered for the file's extension. 
     70        """ 
     71        # Gets set to a string if the file has an associated reader that fails 
     72        msg_from_reader = None 
    6973        try: 
    7074            return super(Registry, self).load(path, format=format) 
    71         except: 
    72             try: 
    73                 # No reader was found. Default to the ascii reader. 
    74                 ascii_loader = ascii_reader.Reader() 
    75                 return ascii_loader.read(path) 
    76             except: 
    77                 cansas_loader = cansas_reader.Reader() 
    78                 return cansas_loader.read(path) 
     75        except NoKnownLoaderException as nkl_e: 
     76            pass  # Try the ASCII reader 
     77        except FileContentsException as fc_exc: 
     78            # File has an associated reader but it failed. 
     79            # Save the error message to display later, but try the 3 default loaders 
     80            msg_from_reader = fc_exc.message 
     81        except Exception: 
     82            pass 
     83 
     84        # File has no associated reader, or the associated reader failed. 
     85        # Try the ASCII reader 
     86        try: 
     87            ascii_loader = ascii_reader.Reader() 
     88            return ascii_loader.read(path) 
     89        except DefaultReaderException: 
     90            pass  # Loader specific error to try the cansas XML reader 
     91        except FileContentsException as e: 
     92            if msg_from_reader is None: 
     93                raise RuntimeError(e.message) 
     94 
     95        # ASCII reader failed - try CanSAS xML reader 
     96        try: 
     97            cansas_loader = cansas_reader.Reader() 
     98            return cansas_loader.read(path) 
     99        except DefaultReaderException: 
     100            pass  # Loader specific error to try the NXcanSAS reader 
     101        except FileContentsException as e: 
     102            if msg_from_reader is None: 
     103                raise RuntimeError(e.message) 
     104        except Exception: 
     105            pass 
     106 
     107        # CanSAS XML reader failed - try NXcanSAS reader 
     108        try: 
     109            cansas_nexus_loader = cansas_reader_HDF5.Reader() 
     110            return cansas_nexus_loader.read(path) 
     111        except DefaultReaderException as e: 
     112            logging.error("No default loader can load the data") 
     113            # No known reader available. Give up and throw an error 
     114            if msg_from_reader is None: 
     115                msg = "\nUnknown data format: {}.\nThe file is not a ".format(path) 
     116                msg += "known format that can be loaded by SasView.\n" 
     117                raise NoKnownLoaderException(msg) 
     118            else: 
     119                # Associated reader and default readers all failed. 
     120                # Show error message from associated reader 
     121                raise RuntimeError(msg_from_reader) 
     122        except FileContentsException as e: 
     123            err_msg = msg_from_reader if msg_from_reader is not None else e.message 
     124            raise RuntimeError(err_msg) 
    79125 
    80126    def find_plugins(self, dir): 
Note: See TracChangeset for help on using the changeset viewer.