Changeset 270c882b in sasview


Ignore:
Timestamp:
Apr 5, 2017 3:31:36 AM (3 years ago)
Author:
krzywon
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
cfc6f3c7
Parents:
b9b612a
git-author:
Jeff Krzywon <krzywon@…> (04/05/17 03:31:36)
git-committer:
krzywon <krzywon@…> (04/05/17 03:31:36)
Message:

Beginning implementation of exception handling within data loader.

Location:
src/sas/sascalc
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/data_util/registry.py

    rb699768 r270c882b  
    77""" 
    88 
    9 import os.path 
     9from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException 
     10 
    1011 
    1112class ExtensionRegistry(object): 
     
    6162    def __init__(self, **kw): 
    6263        self.loaders = {} 
     64 
    6365    def __setitem__(self, ext, loader): 
    6466        if ext not in self.loaders: 
    6567            self.loaders[ext] = [] 
    6668        self.loaders[ext].insert(0,loader) 
     69 
    6770    def __getitem__(self, ext): 
    6871        return self.loaders[ext] 
     72 
    6973    def __contains__(self, ext): 
    7074        return ext in self.loaders 
     75 
    7176    def formats(self): 
    7277        """ 
     
    7681        names.sort() 
    7782        return names 
     83 
    7884    def extensions(self): 
    7985        """ 
     
    8389        exts.sort() 
    8490        return exts 
     91 
    8592    def lookup(self, path): 
    8693        """ 
     
    105112        # Raise an error if there are no matching extensions 
    106113        if len(loaders) == 0: 
    107             raise ValueError, "Unknown file type for "+path 
     114            raise ValueError("Unknown file type for "+path) 
    108115        # All done 
    109116        return loaders 
     117 
    110118    def load(self, path, format=None): 
    111119        """ 
     
    117125        """ 
    118126        if format is None: 
    119             loaders = self.lookup(path) 
     127            try: 
     128                loaders = self.lookup(path) 
     129            except ValueError as e: 
     130                pass 
    120131        else: 
    121             loaders = self.loaders[format] 
     132            try: 
     133                loaders = self.loaders[format] 
     134            except KeyError as e: 
     135                pass 
    122136        for fn in loaders: 
    123137            try: 
    124138                return fn(path) 
    125             except: 
    126                 pass # give other loaders a chance to succeed 
     139            except Exception as e: 
     140                pass  # give other loaders a chance to succeed 
    127141        # If we get here it is because all loaders failed 
    128         raise # reraises last exception 
     142        raise NoKnownLoaderException(e.message)  # raise generic exception 
    129143 
     144 
     145# TODO: Move this to the unit test folder 
    130146def test(): 
    131147    reg = ExtensionRegistry() 
     
    163179    try: reg.load('hello.missing') 
    164180    except ValueError,msg: 
    165         assert str(msg)=="Unknown file type for hello.missing",'Message: <%s>'%(msg) 
     181        assert str(msg)=="Unknown file type for hello.missing",\ 
     182            'Message: <%s>'%(msg) 
    166183    else: raise AssertError,"No error raised for missing extension" 
    167184    assert reg.formats() == ['new_cx'] 
  • src/sas/sascalc/dataloader/loader.py

    rb9b612a r270c882b  
    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 
     
    2929# Default readers are defined in the readers sub-module 
    3030import readers 
     31from loader_exceptions import NoKnownLoaderException, FileContentsException 
    3132from readers import ascii_reader 
    3233from readers import cansas_reader 
    3334from readers import cansas_reader_HDF5 
     35 
    3436 
    3537class Registry(ExtensionRegistry): 
     
    3840    Readers and writers are supported. 
    3941    """ 
    40  
    4142    def __init__(self): 
    4243        super(Registry, self).__init__() 
    4344 
    44         ## Writers 
     45        # Writers 
    4546        self.writers = {} 
    4647 
    47         ## List of wildcards 
     48        # List of wildcards 
    4849        self.wildcards = ['All (*.*)|*.*'] 
    4950 
    50         ## Creation time, for testing 
     51        # Creation time, for testing 
    5152        self._created = time.time() 
    5253 
     
    6768        try: 
    6869            return super(Registry, self).load(path, format=format) 
    69         except Exception: 
    70             pass # try the ASCII reader 
     70        except NoKnownLoaderException as e: 
     71            pass  # try the ASCII reader 
     72        except FileContentsException as e: 
     73            pass 
    7174        try: 
    7275            ascii_loader = ascii_reader.Reader() 
    7376            return ascii_loader.read(path) 
    74         except Exception: 
    75             pass # try the cansas XML reader 
     77        except FileContentsException: 
     78            pass  # try the cansas XML reader 
    7679        try: 
    7780            cansas_loader = cansas_reader.Reader() 
    7881            return cansas_loader.read(path) 
    79         except Exception: 
    80             pass # try the cansas NeXuS reader 
     82        except FileContentsException: 
     83            pass  # try the cansas NeXuS reader 
    8184        try: 
    8285            cansas_nexus_loader = cansas_reader_HDF5.Reader() 
    8386            return cansas_nexus_loader.read(path) 
    84         except Exception: 
     87        except FileContentsException: 
    8588            # No known reader available. Give up and throw an error 
    8689            msg = "\n\tUnknown data format: %s.\n\tThe file is not a " % path 
    87             msg += "known format for SasView. The most common formats are " 
    88             msg += "multi-column ASCII, CanSAS XML, and CanSAS NeXuS." 
    89             raise Exception(msg) 
     90            msg += "known format that can be loaded by SasView." 
     91            raise NoKnownLoaderException(msg) 
    9092 
    9193    def find_plugins(self, dir): 
Note: See TracChangeset for help on using the changeset viewer.