Changeset 3ece5dd in sasview
- Timestamp:
- Jul 26, 2017 6:31:27 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:
- bc570f4
- Parents:
- 371b9e2
- Location:
- src/sas/sascalc
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sascalc/data_util/registry.py
r5d8f9b3 r3ece5dd 26 26 registry['.tar.gz'] = untar 27 27 28 # Generic extensions to use after trying more specific extensions; 28 # Generic extensions to use after trying more specific extensions; 29 29 # these will be checked after the more specific extensions fail. 30 30 registry['.gz'] = gunzip … … 92 92 """ 93 93 Return the loader associated with the file type of path. 94 94 95 95 :param path: Data file path 96 96 :raises ValueError: When no loaders are found for the file. 97 97 :return: List of available readers for the file extension 98 """ 98 """ 99 99 # Find matching extensions 100 100 extlist = [ext for ext in self.extensions() if path.endswith(ext)] … … 135 135 except KeyError as e: 136 136 pass 137 last_exc = None 137 138 for fn in loaders: 138 139 try: 139 140 return fn(path) 140 141 except Exception as e: 142 last_exc = e 141 143 pass # give other loaders a chance to succeed 142 144 # If we get here it is because all loaders failed 145 if last_exc is not None and len(loaders) != 0: 146 # If file has associated loader(s) and they;ve failed 147 raise last_exc 143 148 raise NoKnownLoaderException(e.message) # raise generic exception -
src/sas/sascalc/dataloader/loader.py
r371b9e2 r3ece5dd 72 72 return super(Registry, self).load(path, format=format) 73 73 except NoKnownLoaderException as nkl_e: 74 pass # try the ASCII reader4 75 except Exception as reg_e: 74 pass # try the ASCII reader 75 except FileContentsException as fc_exc: 76 raise RuntimeError(fc_exc.message) 77 except Exception: 76 78 pass 77 79 try: … … 79 81 return ascii_loader.read(path) 80 82 except DefaultReaderException: 81 pass # Loader specific error to try the asciireader82 except FileContentsException :83 pass # Loader specific error to try the cansas XML reader 84 except FileContentsException as e: 83 85 # TODO: handle error 86 raise RuntimeError(e) 84 87 pass 85 88 try: … … 88 91 except DefaultReaderException: 89 92 pass # Loader specific error to try the cansas NeXuS reader 90 except FileContentsException :93 except FileContentsException as e: 91 94 # TODO: Handle errors properly 95 raise RuntimeError(e) 92 96 pass 93 97 except Exception as csr: -
src/sas/sascalc/dataloader/loader_exceptions.py
r371b9e2 r3ece5dd 10 10 loader.py. 11 11 """ 12 def __init__(self, e ):12 def __init__(self, e=None): 13 13 self.message = e 14 14 … … 19 19 default readers only to tell Loader to try the next reader. 20 20 """ 21 def __init__(self, e ):21 def __init__(self, e=None): 22 22 self.message = e 23 23 … … 28 28 This is useful for catching loader or file format issues. 29 29 """ 30 def __init__(self, e ):30 def __init__(self, e=None): 31 31 self.message = e 32 32 … … 38 38 Any exceptions of this type should be put into the datainfo.errors 39 39 """ 40 def __init__(self, e ):40 def __init__(self, e=None): 41 41 self.message = e -
src/sas/sascalc/dataloader/readers/cansas_reader.py
r278ddee r3ece5dd 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 from sas.sascalc.dataloader.loader_exceptions import FileContentsException, \ 32 DefaultReaderException, DataReaderException 32 33 33 34 # The following 2 imports *ARE* used. Do not remove either. … … 123 124 if os.path.isfile(xml_file): 124 125 basename, extension = os.path.splitext(os.path.basename(xml_file)) 125 # If the file type is not allowed, return nothing 126 if extension in self.ext or self.allow_all: 126 try: 127 127 # Get the file location of 128 128 self.load_file_and_schema(xml_file, schema_path) … … 130 130 # Try to load the file, but raise an error if unable to. 131 131 # Check the file matches the XML schema 132 self.is_cansas(extension) # Raises FileContentsException if not CanSAS 133 self.invalid = False 134 # Get each SASentry from XML file and add it to a list. 135 entry_list = self.xmlroot.xpath( 136 '/ns:SASroot/ns:SASentry', 137 namespaces={'ns': self.cansas_defaults.get("ns")}) 138 self.names.append("SASentry") 139 140 # Get all preprocessing events and encoding 141 self.set_processing_instructions() 142 143 # Parse each <SASentry> item 144 for entry in entry_list: 145 # Create a new DataInfo object for every <SASentry> 146 147 # Set the file name and then parse the entry. 148 self.current_datainfo.filename = basename + extension 149 self.current_datainfo.meta_data["loader"] = "CanSAS XML 1D" 150 self.current_datainfo.meta_data[PREPROCESS] = \ 151 self.processing_instructions 152 153 # Parse the XML SASentry 154 self._parse_entry(entry) 155 # Combine datasets with datainfo 156 self.add_data_set() 157 except FileContentsException as fc_exc: 132 158 try: 133 self.is_cansas(extension)134 self.invalid = False135 # Get each SASentry from XML file and add it to a list.136 entry_list = self.xmlroot.xpath(137 '/ns:SASroot/ns:SASentry',138 namespaces={'ns': self.cansas_defaults.get("ns")})139 self.names.append("SASentry")140 141 # Get all preprocessing events and encoding142 self.set_processing_instructions()143 144 # Parse each <SASentry> item145 for entry in entry_list:146 # Create a new DataInfo object for every <SASentry>147 148 # Set the file name and then parse the entry.149 self.current_datainfo.filename = basename + extension150 self.current_datainfo.meta_data["loader"] = "CanSAS XML 1D"151 self.current_datainfo.meta_data[PREPROCESS] = \152 self.processing_instructions153 154 # Parse the XML SASentry155 self._parse_entry(entry)156 # Combine datasets with datainfo157 self.add_data_set()158 except RuntimeError:159 159 # If the file does not match the schema, raise this error 160 160 invalid_xml = self.find_invalid_xml() … … 170 170 invalid_schema = INVALID_SCHEMA_PATH_1_0.format(base, self.cansas_defaults.get("schema")) 171 171 self.set_schema(invalid_schema) 172 try: 173 if self.invalid: 174 if self.is_cansas(): 175 self.output = self.read(xml_file, invalid_schema, False) 176 else: 177 raise RuntimeError 178 else: 179 raise RuntimeError 180 except RuntimeError: 181 x = np.zeros(1) 182 y = np.zeros(1) 183 self.current_data1d = Data1D(x,y) 184 self.current_data1d.errors = self.errors 185 return [self.current_data1d] 172 if self.invalid: 173 self.output = self.read(xml_file, invalid_schema, False) 174 else: 175 raise fc_exc 176 except FileContentsException as fc_exc: 177 msg = "CanSAS Reader could not load the file {}".format(xml_file) 178 if not extension in self.ext: 179 raise DefaultReaderException(msg) 180 if fc_exc.message is not None: 181 msg = fc_exc.message 182 raise FileContentsException(msg) 183 except Exception as e: 184 raise FileContentsException(e.message) 186 185 else: 187 186 self.output.append("Not a valid file path.") … … 525 524 if ext == "svs": 526 525 return True 527 raise RuntimeError526 raise FileContentsException("Not valid CanSAS") 528 527 529 528 def load_file_and_schema(self, xml_file, schema_path=""): … … 543 542 self.set_xml_file(xml_file) 544 543 except etree.XMLSyntaxError: 545 msg = "Cansas cannot load this file"546 raise FileContentsException , msg544 msg = "Cansas cannot load {}.\n Invalid XML syntax.".format(xml_file) 545 raise FileContentsException(msg) 547 546 self.cansas_version = self.xmlroot.get("version", "1.0") 548 547 -
src/sas/sascalc/dataloader/readers/xml_reader.py
r235f514 r3ece5dd 74 74 except etree.XMLSyntaxError as xml_error: 75 75 logger.info(xml_error) 76 raise xml_error 76 77 except Exception: 77 78 self.xml = None … … 206 207 Create a unique key value for any dictionary to prevent overwriting 207 208 Recurses until a unique key value is found. 208 209 209 210 :param dictionary: A dictionary with any number of entries 210 211 :param name: The index of the item to be added to dictionary … … 222 223 Create an element tree for processing from an etree element 223 224 224 :param root: etree Element(s) 225 :param root: etree Element(s) 225 226 """ 226 227 return etree.ElementTree(root)
Note: See TracChangeset
for help on using the changeset viewer.