Changeset 69363c7 in sasview for src/sas/sascalc/data_util


Ignore:
Timestamp:
Sep 22, 2017 8:29:48 AM (7 years ago)
Author:
Paul Kienzle <pkienzle@…>
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, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
ab0b93f
Parents:
1386b2f (diff), d76c43a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Paul Kienzle <pkienzle@…> (09/22/17 08:28:48)
git-committer:
Paul Kienzle <pkienzle@…> (09/22/17 08:29:48)
Message:

Merge branch 'master' into ticket-853-fit-gui-to-calc

File:
1 edited

Legend:

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

    ra1b8fee r5a8cdbb  
    1 # This program is public domain 
    21""" 
    32File extension registry. 
     
    87from __future__ import print_function 
    98 
    10 import os.path 
     9from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException 
     10 
    1111 
    1212class ExtensionRegistry(object): 
     
    2222        # Add an association by setting an element 
    2323        registry['.zip'] = unzip 
    24          
     24 
    2525        # Multiple extensions for one loader 
    2626        registry['.tgz'] = untar 
    2727        registry['.tar.gz'] = untar 
    2828 
    29         # Generic extensions to use after trying more specific extensions;  
     29        # Generic extensions to use after trying more specific extensions; 
    3030        # these will be checked after the more specific extensions fail. 
    3131        registry['.gz'] = gunzip 
     
    3838        # Show registered extensions 
    3939        print registry.extensions() 
    40          
     40 
    4141        # Can also register a format name for explicit control from caller 
    4242        registry['cx3'] = cx3 
     
    6262    def __init__(self, **kw): 
    6363        self.loaders = {} 
     64 
    6465    def __setitem__(self, ext, loader): 
    6566        if ext not in self.loaders: 
    6667            self.loaders[ext] = [] 
    6768        self.loaders[ext].insert(0,loader) 
     69 
    6870    def __getitem__(self, ext): 
    6971        return self.loaders[ext] 
     72 
    7073    def __contains__(self, ext): 
    7174        return ext in self.loaders 
     75 
    7276    def formats(self): 
    7377        """ 
     
    7781        names.sort() 
    7882        return names 
     83 
    7984    def extensions(self): 
    8085        """ 
     
    8489        exts.sort() 
    8590        return exts 
     91 
    8692    def lookup(self, path): 
    8793        """ 
    8894        Return the loader associated with the file type of path. 
    89          
    90         Raises ValueError if file type is not known. 
    91         """         
     95 
     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 
     99        """ 
    92100        # Find matching extensions 
    93101        extlist = [ext for ext in self.extensions() if path.endswith(ext)] 
     
    106114        # Raise an error if there are no matching extensions 
    107115        if len(loaders) == 0: 
    108             raise ValueError, "Unknown file type for "+path 
    109         # All done 
     116            raise ValueError("Unknown file type for "+path) 
    110117        return loaders 
     118 
    111119    def load(self, path, format=None): 
    112120        """ 
    113121        Call the loader for the file type of path. 
    114122 
    115         Raises ValueError if no loader is available. 
    116         Raises KeyError if format is not available. 
    117         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. 
    118126        """ 
     127        loaders = [] 
    119128        if format is None: 
    120             loaders = self.lookup(path) 
     129            try: 
     130                loaders = self.lookup(path) 
     131            except ValueError as e: 
     132                pass 
    121133        else: 
    122             loaders = self.loaders[format] 
     134            try: 
     135                loaders = self.loaders[format] 
     136            except KeyError as e: 
     137                pass 
     138        last_exc = None 
    123139        for fn in loaders: 
    124140            try: 
    125141                return fn(path) 
    126             except: 
    127                 pass # give other loaders a chance to succeed 
     142            except Exception as e: 
     143                last_exc = e 
     144                pass  # give other loaders a chance to succeed 
    128145        # If we get here it is because all loaders failed 
    129         raise # reraises last exception 
    130  
    131 def test(): 
    132     reg = ExtensionRegistry() 
    133     class CxError(Exception): pass 
    134     def cx(file): return 'cx' 
    135     def new_cx(file): return 'new_cx' 
    136     def fail_cx(file): raise CxError 
    137     def cat(file): return 'cat' 
    138     def gunzip(file): return 'gunzip' 
    139     reg['.cx'] = cx 
    140     reg['.cx1'] = cx 
    141     reg['.cx'] = new_cx 
    142     reg['.gz'] = gunzip 
    143     reg['.cx.gz'] = new_cx 
    144     reg['.cx1.gz'] = fail_cx 
    145     reg['.cx1'] = fail_cx 
    146     reg['.cx2'] = fail_cx 
    147     reg['new_cx'] = new_cx 
    148  
    149     # Two loaders associated with .cx 
    150     assert reg.lookup('hello.cx') == [new_cx,cx] 
    151     # Make sure the last loader applies first 
    152     assert reg.load('hello.cx') == 'new_cx' 
    153     # Make sure the next loader applies if the first fails 
    154     assert reg.load('hello.cx1') == 'cx' 
    155     # Make sure the format override works 
    156     assert reg.load('hello.cx1',format='.cx.gz') == 'new_cx' 
    157     # Make sure the format override works 
    158     assert reg.load('hello.cx1',format='new_cx') == 'new_cx' 
    159     # Make sure the case of all loaders failing is correct 
    160     try:  reg.load('hello.cx2') 
    161     except CxError: pass # correct failure 
    162     else: raise AssertError,"Incorrect error on load failure" 
    163     # Make sure the case of no loaders fails correctly 
    164     try: reg.load('hello.missing') 
    165     except ValueError,msg: 
    166         assert str(msg)=="Unknown file type for hello.missing",'Message: <%s>'%(msg) 
    167     else: raise AssertError,"No error raised for missing extension" 
    168     assert reg.formats() == ['new_cx'] 
    169     assert reg.extensions() == ['.cx','.cx.gz','.cx1','.cx1.gz','.cx2','.gz'] 
    170     # make sure that it supports multiple '.' in filename 
    171     assert reg.load('hello.extra.cx1') == 'cx' 
    172     assert reg.load('hello.gz') == 'gunzip' 
    173     assert reg.load('hello.cx1.gz') == 'gunzip' # Since .cx1.gz fails 
    174  
    175 if __name__ == "__main__": test() 
     146        if last_exc is not None and len(loaders) != 0: 
     147            # If file has associated loader(s) and they;ve failed 
     148            raise last_exc 
     149        raise NoKnownLoaderException(e.message)  # raise generic exception 
Note: See TracChangeset for help on using the changeset viewer.