Ignore:
File:
1 edited

Legend:

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

    rdcb91cf r463e7ffc  
    11""" 
    22    File handler to support different file extensions. 
    3     Uses reflectometer registry utility. 
     3    Uses reflectometry's 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 
    31 from loader_exceptions import NoKnownLoaderException, FileContentsException,\ 
    32     DefaultReaderException 
    3331from readers import ascii_reader 
    3432from readers import cansas_reader 
    35 from readers import cansas_reader_HDF5 
    3633 
    3734logger = logging.getLogger(__name__) 
    38  
    3935 
    4036class Registry(ExtensionRegistry): 
     
    4339    Readers and writers are supported. 
    4440    """ 
     41 
    4542    def __init__(self): 
    4643        super(Registry, self).__init__() 
    4744 
    48         # Writers 
     45        ## Writers 
    4946        self.writers = {} 
    5047 
    51         # List of wildcards 
     48        ## List of wildcards 
    5249        self.wildcards = ['All (*.*)|*.*'] 
    5350 
    54         # Creation time, for testing 
     51        ## Creation time, for testing 
    5552        self._created = time.time() 
    5653 
     
    6663            of a particular reader 
    6764 
    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 
     65        Defaults to the ascii (multi-column) reader 
     66        if no reader was registered for the file's 
     67        extension. 
     68        """ 
    7369        try: 
    7470            return super(Registry, self).load(path, format=format) 
    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) 
     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) 
    12579 
    12680    def find_plugins(self, dir): 
Note: See TracChangeset for help on using the changeset viewer.