Changeset 3ece5dd in sasview for src/sas/sascalc/dataloader/readers
- 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/dataloader/readers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
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.