Changeset 278ddee in sasview for src/sas/sascalc/data_util/registry.py
- Timestamp:
- Apr 11, 2017 9:51:05 AM (7 years ago)
- 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:
- beba407
- Parents:
- 7f75a3f (diff), 97c60f8 (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:
- Jeff Krzywon <krzywon@…> (04/11/17 09:51:05)
- git-committer:
- krzywon <krzywon@…> (04/11/17 09:51:05)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/data_util/registry.py
rb699768 r7f75a3f 7 7 """ 8 8 9 import os.path 9 from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException 10 10 11 11 12 class ExtensionRegistry(object): … … 61 62 def __init__(self, **kw): 62 63 self.loaders = {} 64 63 65 def __setitem__(self, ext, loader): 64 66 if ext not in self.loaders: 65 67 self.loaders[ext] = [] 66 68 self.loaders[ext].insert(0,loader) 69 67 70 def __getitem__(self, ext): 68 71 return self.loaders[ext] 72 69 73 def __contains__(self, ext): 70 74 return ext in self.loaders 75 71 76 def formats(self): 72 77 """ … … 76 81 names.sort() 77 82 return names 83 78 84 def extensions(self): 79 85 """ … … 83 89 exts.sort() 84 90 return exts 91 85 92 def lookup(self, path): 86 93 """ 87 94 Return the loader associated with the file type of path. 88 95 89 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 90 99 """ 91 100 # Find matching extensions … … 105 114 # Raise an error if there are no matching extensions 106 115 if len(loaders) == 0: 107 raise ValueError, "Unknown file type for "+path 108 # All done 116 raise ValueError("Unknown file type for "+path) 109 117 return loaders 118 110 119 def load(self, path, format=None): 111 120 """ 112 121 Call the loader for the file type of path. 113 122 114 Raises ValueErrorif no loader is available.115 Raises KeyErrorif format is not available.116 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. 117 126 """ 127 loaders = [] 118 128 if format is None: 119 loaders = self.lookup(path) 129 try: 130 loaders = self.lookup(path) 131 except ValueError as e: 132 pass 120 133 else: 121 loaders = self.loaders[format] 134 try: 135 loaders = self.loaders[format] 136 except KeyError as e: 137 pass 122 138 for fn in loaders: 123 139 try: 124 140 return fn(path) 125 except :126 pass # give other loaders a chance to succeed141 except Exception as e: 142 pass # give other loaders a chance to succeed 127 143 # If we get here it is because all loaders failed 128 raise # reraises last exception 129 130 def test(): 131 reg = ExtensionRegistry() 132 class CxError(Exception): pass 133 def cx(file): return 'cx' 134 def new_cx(file): return 'new_cx' 135 def fail_cx(file): raise CxError 136 def cat(file): return 'cat' 137 def gunzip(file): return 'gunzip' 138 reg['.cx'] = cx 139 reg['.cx1'] = cx 140 reg['.cx'] = new_cx 141 reg['.gz'] = gunzip 142 reg['.cx.gz'] = new_cx 143 reg['.cx1.gz'] = fail_cx 144 reg['.cx1'] = fail_cx 145 reg['.cx2'] = fail_cx 146 reg['new_cx'] = new_cx 147 148 # Two loaders associated with .cx 149 assert reg.lookup('hello.cx') == [new_cx,cx] 150 # Make sure the last loader applies first 151 assert reg.load('hello.cx') == 'new_cx' 152 # Make sure the next loader applies if the first fails 153 assert reg.load('hello.cx1') == 'cx' 154 # Make sure the format override works 155 assert reg.load('hello.cx1',format='.cx.gz') == 'new_cx' 156 # Make sure the format override works 157 assert reg.load('hello.cx1',format='new_cx') == 'new_cx' 158 # Make sure the case of all loaders failing is correct 159 try: reg.load('hello.cx2') 160 except CxError: pass # correct failure 161 else: raise AssertError,"Incorrect error on load failure" 162 # Make sure the case of no loaders fails correctly 163 try: reg.load('hello.missing') 164 except ValueError,msg: 165 assert str(msg)=="Unknown file type for hello.missing",'Message: <%s>'%(msg) 166 else: raise AssertError,"No error raised for missing extension" 167 assert reg.formats() == ['new_cx'] 168 assert reg.extensions() == ['.cx','.cx.gz','.cx1','.cx1.gz','.cx2','.gz'] 169 # make sure that it supports multiple '.' in filename 170 assert reg.load('hello.extra.cx1') == 'cx' 171 assert reg.load('hello.gz') == 'gunzip' 172 assert reg.load('hello.cx1.gz') == 'gunzip' # Since .cx1.gz fails 173 174 if __name__ == "__main__": test() 144 raise NoKnownLoaderException(e.message) # raise generic exception
Note: See TracChangeset
for help on using the changeset viewer.