Changes in / [a2e980b:cfc6f3c7] in sasview
- Location:
- src/sas/sascalc
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/data_util/registry.py
rb699768 r270c882b 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 """ … … 105 112 # Raise an error if there are no matching extensions 106 113 if len(loaders) == 0: 107 raise ValueError , "Unknown file type for "+path114 raise ValueError("Unknown file type for "+path) 108 115 # All done 109 116 return loaders 117 110 118 def load(self, path, format=None): 111 119 """ … … 117 125 """ 118 126 if format is None: 119 loaders = self.lookup(path) 127 try: 128 loaders = self.lookup(path) 129 except ValueError as e: 130 pass 120 131 else: 121 loaders = self.loaders[format] 132 try: 133 loaders = self.loaders[format] 134 except KeyError as e: 135 pass 122 136 for fn in loaders: 123 137 try: 124 138 return fn(path) 125 except :126 pass # give other loaders a chance to succeed139 except Exception as e: 140 pass # give other loaders a chance to succeed 127 141 # If we get here it is because all loaders failed 128 raise # reraises lastexception142 raise NoKnownLoaderException(e.message) # raise generic exception 129 143 144 145 # TODO: Move this to the unit test folder 130 146 def test(): 131 147 reg = ExtensionRegistry() … … 163 179 try: reg.load('hello.missing') 164 180 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) 166 183 else: raise AssertError,"No error raised for missing extension" 167 184 assert reg.formats() == ['new_cx'] -
src/sas/sascalc/dataloader/loader.py
rb699768 r270c882b 1 1 """ 2 2 File handler to support different file extensions. 3 Uses reflectomet ry'sregistry utility.3 Uses reflectometer registry utility. 4 4 5 5 The default readers are found in the 'readers' sub-module … … 29 29 # Default readers are defined in the readers sub-module 30 30 import readers 31 from loader_exceptions import NoKnownLoaderException, FileContentsException 31 32 from readers import ascii_reader 32 33 from readers import cansas_reader 34 from readers import cansas_reader_HDF5 35 33 36 34 37 class Registry(ExtensionRegistry): … … 37 40 Readers and writers are supported. 38 41 """ 39 40 42 def __init__(self): 41 43 super(Registry, self).__init__() 42 44 43 # #Writers45 # Writers 44 46 self.writers = {} 45 47 46 # #List of wildcards48 # List of wildcards 47 49 self.wildcards = ['All (*.*)|*.*'] 48 50 49 # #Creation time, for testing51 # Creation time, for testing 50 52 self._created = time.time() 51 53 … … 61 63 of a particular reader 62 64 63 Defaults to the ascii (multi-column) reader 64 if no reader was registered for the file's 65 extension. 65 Defaults to the ascii (multi-column), cansas XML, and cansas NeXuS 66 readers if no reader was registered for the file's extension. 66 67 """ 67 68 try: 68 69 return super(Registry, self).load(path, format=format) 69 except: 70 try: 71 # No reader was found. Default to the ascii reader. 72 ascii_loader = ascii_reader.Reader() 73 return ascii_loader.read(path) 74 except: 75 cansas_loader = cansas_reader.Reader() 76 return cansas_loader.read(path) 70 except NoKnownLoaderException as e: 71 pass # try the ASCII reader 72 except FileContentsException as e: 73 pass 74 try: 75 ascii_loader = ascii_reader.Reader() 76 return ascii_loader.read(path) 77 except FileContentsException: 78 pass # try the cansas XML reader 79 try: 80 cansas_loader = cansas_reader.Reader() 81 return cansas_loader.read(path) 82 except FileContentsException: 83 pass # try the cansas NeXuS reader 84 try: 85 cansas_nexus_loader = cansas_reader_HDF5.Reader() 86 return cansas_nexus_loader.read(path) 87 except FileContentsException: 88 # No known reader available. Give up and throw an error 89 msg = "\n\tUnknown data format: %s.\n\tThe file is not a " % path 90 msg += "known format that can be loaded by SasView." 91 raise NoKnownLoaderException(msg) 77 92 78 93 def find_plugins(self, dir):
Note: See TracChangeset
for help on using the changeset viewer.