Changeset 7f75a3f in sasview for src/sas/sascalc
- Timestamp:
- Apr 5, 2017 11:08:59 AM (8 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:
- 278ddee
- Parents:
- 69400ec
- git-author:
- Jeff Krzywon <krzywon@…> (04/05/17 11:08:59)
- git-committer:
- krzywon <krzywon@…> (04/05/17 11:08:59)
- Location:
- src/sas/sascalc
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/data_util/registry.py
r270c882b r7f75a3f 94 94 Return the loader associated with the file type of path. 95 95 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 97 99 """ 98 100 # Find matching extensions … … 113 115 if len(loaders) == 0: 114 116 raise ValueError("Unknown file type for "+path) 115 # All done116 117 return loaders 117 118 … … 120 121 Call the loader for the file type of path. 121 122 122 Raises ValueErrorif no loader is available.123 Raises KeyErrorif 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. 125 126 """ 127 loaders = [] 126 128 if format is None: 127 129 try: … … 141 143 # If we get here it is because all loaders failed 142 144 raise NoKnownLoaderException(e.message) # raise generic exception 143 144 145 # TODO: Move this to the unit test folder146 def test():147 reg = ExtensionRegistry()148 class CxError(Exception): pass149 def cx(file): return 'cx'150 def new_cx(file): return 'new_cx'151 def fail_cx(file): raise CxError152 def cat(file): return 'cat'153 def gunzip(file): return 'gunzip'154 reg['.cx'] = cx155 reg['.cx1'] = cx156 reg['.cx'] = new_cx157 reg['.gz'] = gunzip158 reg['.cx.gz'] = new_cx159 reg['.cx1.gz'] = fail_cx160 reg['.cx1'] = fail_cx161 reg['.cx2'] = fail_cx162 reg['new_cx'] = new_cx163 164 # Two loaders associated with .cx165 assert reg.lookup('hello.cx') == [new_cx,cx]166 # Make sure the last loader applies first167 assert reg.load('hello.cx') == 'new_cx'168 # Make sure the next loader applies if the first fails169 assert reg.load('hello.cx1') == 'cx'170 # Make sure the format override works171 assert reg.load('hello.cx1',format='.cx.gz') == 'new_cx'172 # Make sure the format override works173 assert reg.load('hello.cx1',format='new_cx') == 'new_cx'174 # Make sure the case of all loaders failing is correct175 try: reg.load('hello.cx2')176 except CxError: pass # correct failure177 else: raise AssertError,"Incorrect error on load failure"178 # Make sure the case of no loaders fails correctly179 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 filename187 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 fails190 191 if __name__ == "__main__": test() -
src/sas/sascalc/dataloader/loader.py
r270c882b r7f75a3f 70 70 except NoKnownLoaderException as e: 71 71 pass # try the ASCII reader 72 except FileContentsException as e:73 pass74 72 try: 75 73 ascii_loader = ascii_reader.Reader() … … 86 84 return cansas_nexus_loader.read(path) 87 85 except FileContentsException: 86 logging.errors("No default loader can load the data") 88 87 # No known reader available. Give up and throw an error 89 88 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) 89 msg += "known format that can be loaded by SasView.\n" 90 msg += "Traceback:\n%s" % e.message 91 raise NoKnownLoaderException, msg 92 92 93 93 def find_plugins(self, dir): -
src/sas/sascalc/dataloader/readers/ascii_reader.py
r9a5097c r7f75a3f 17 17 import os 18 18 from sas.sascalc.dataloader.data_info import Data1D 19 from sas.sascalc.dataloader.loader_exceptions import FileContentsException 19 20 20 21 # Check whether we have a converter available … … 173 174 if not is_data: 174 175 msg = "ascii_reader: x has no data" 175 raise RuntimeError, msg176 raise FileContentsException, msg 176 177 # Sanity check 177 178 if has_error_dy == True and not len(ty) == len(tdy): 178 179 msg = "ascii_reader: y and dy have different length" 179 raise RuntimeError, msg180 raise FileContentsException, msg 180 181 if has_error_dx == True and not len(tx) == len(tdx): 181 182 msg = "ascii_reader: y and dy have different length" 182 raise RuntimeError, msg183 raise FileContentsException, msg 183 184 # If the data length is zero, consider this as 184 185 # though we were not able to read the file. 185 186 if len(tx) == 0: 186 raise RuntimeError, "ascii_reader: could not load file"187 raise FileContentsException, "ascii_reader: could not load file" 187 188 188 189 #Let's re-order the data to make cal. -
src/sas/sascalc/dataloader/readers/cansas_reader.py
r8434365 r7f75a3f 29 29 from sas.sascalc.dataloader.readers.xml_reader import XMLreader 30 30 from sas.sascalc.dataloader.readers.cansas_constants import CansasConstants, CurrentLevel 31 from sas.sascalc.dataloader.loader_exceptions import FileContentsException 31 32 32 33 # The following 2 imports *ARE* used. Do not remove either. … … 534 535 535 536 # Load in xml file and get the cansas version from the header 536 self.set_xml_file(xml_file) 537 from lxml import etree 538 try: 539 self.set_xml_file(xml_file) 540 except etree.XMLSyntaxError: 541 msg = "Cansas cannot load this file" 542 raise FileContentsException, msg 537 543 self.cansas_version = self.xmlroot.get("version", "1.0") 538 544 -
src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py
rc94280c r7f75a3f 13 13 TransmissionSpectrum, Detector 14 14 from sas.sascalc.dataloader.data_info import combine_data_info_with_plottable 15 from sas.sascalc.dataloader.loader_exceptions import FileContentsException 15 16 16 17 … … 75 76 if extension in self.ext or self.allow_all: 76 77 # Load the data file 77 self.raw_data = h5py.File(filename, 'r') 78 try: 79 self.raw_data = h5py.File(filename, 'r') 80 except Exception as e: 81 raise FileContentsException, e 78 82 # Read in all child elements of top level SASroot 79 83 self.read_children(self.raw_data, []) -
src/sas/sascalc/dataloader/readers/xml_reader.py
ra235f715 r7f75a3f 70 70 self.xmldoc = etree.parse(self.xml, parser=PARSER) 71 71 self.xmlroot = self.xmldoc.getroot() 72 except etree.XMLSyntaxError as xml_error:73 logging.info(xml_error)72 except etree.XMLSyntaxError: 73 raise 74 74 except Exception: 75 75 self.xml = None
Note: See TracChangeset
for help on using the changeset viewer.