Changeset 7f75a3f in sasview for src/sas/sascalc/data_util


Ignore:
Timestamp:
Apr 5, 2017 11:08:59 AM (8 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:
278ddee
Parents:
69400ec
git-author:
Jeff Krzywon <krzywon@…> (04/05/17 11:08:59)
git-committer:
krzywon <krzywon@…> (04/05/17 11:08:59)
Message:

More explicit error messages when file loading fails. see #889

File:
1 edited

Legend:

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

    r270c882b r7f75a3f  
    9494        Return the loader associated with the file type of path. 
    9595         
    96         Raises ValueError if file type is not known. 
     96        :param path: Data file path 
     97        :raises ValueError: When no loaders are found for the file. 
     98        :return: List of available readers for the file extension 
    9799        """         
    98100        # Find matching extensions 
     
    113115        if len(loaders) == 0: 
    114116            raise ValueError("Unknown file type for "+path) 
    115         # All done 
    116117        return loaders 
    117118 
     
    120121        Call the loader for the file type of path. 
    121122 
    122         Raises ValueError if no loader is available. 
    123         Raises KeyError if format is not available. 
    124         May raise a loader-defined exception if loader fails.         
     123        :raise ValueError: if no loader is available. 
     124        :raise KeyError: if format is not available. 
     125        May raise a loader-defined exception if loader fails. 
    125126        """ 
     127        loaders = [] 
    126128        if format is None: 
    127129            try: 
     
    141143        # If we get here it is because all loaders failed 
    142144        raise NoKnownLoaderException(e.message)  # raise generic exception 
    143  
    144  
    145 # TODO: Move this to the unit test folder 
    146 def test(): 
    147     reg = ExtensionRegistry() 
    148     class CxError(Exception): pass 
    149     def cx(file): return 'cx' 
    150     def new_cx(file): return 'new_cx' 
    151     def fail_cx(file): raise CxError 
    152     def cat(file): return 'cat' 
    153     def gunzip(file): return 'gunzip' 
    154     reg['.cx'] = cx 
    155     reg['.cx1'] = cx 
    156     reg['.cx'] = new_cx 
    157     reg['.gz'] = gunzip 
    158     reg['.cx.gz'] = new_cx 
    159     reg['.cx1.gz'] = fail_cx 
    160     reg['.cx1'] = fail_cx 
    161     reg['.cx2'] = fail_cx 
    162     reg['new_cx'] = new_cx 
    163  
    164     # Two loaders associated with .cx 
    165     assert reg.lookup('hello.cx') == [new_cx,cx] 
    166     # Make sure the last loader applies first 
    167     assert reg.load('hello.cx') == 'new_cx' 
    168     # Make sure the next loader applies if the first fails 
    169     assert reg.load('hello.cx1') == 'cx' 
    170     # Make sure the format override works 
    171     assert reg.load('hello.cx1',format='.cx.gz') == 'new_cx' 
    172     # Make sure the format override works 
    173     assert reg.load('hello.cx1',format='new_cx') == 'new_cx' 
    174     # Make sure the case of all loaders failing is correct 
    175     try:  reg.load('hello.cx2') 
    176     except CxError: pass # correct failure 
    177     else: raise AssertError,"Incorrect error on load failure" 
    178     # Make sure the case of no loaders fails correctly 
    179     try: reg.load('hello.missing') 
    180     except ValueError,msg: 
    181         assert str(msg)=="Unknown file type for hello.missing",\ 
    182             'Message: <%s>'%(msg) 
    183     else: raise AssertError,"No error raised for missing extension" 
    184     assert reg.formats() == ['new_cx'] 
    185     assert reg.extensions() == ['.cx','.cx.gz','.cx1','.cx1.gz','.cx2','.gz'] 
    186     # make sure that it supports multiple '.' in filename 
    187     assert reg.load('hello.extra.cx1') == 'cx' 
    188     assert reg.load('hello.gz') == 'gunzip' 
    189     assert reg.load('hello.cx1.gz') == 'gunzip' # Since .cx1.gz fails 
    190  
    191 if __name__ == "__main__": test() 
Note: See TracChangeset for help on using the changeset viewer.