Changeset da8bb53 in sasview for src/sas/sascalc


Ignore:
Timestamp:
Apr 17, 2017 4:30:10 PM (7 years ago)
Author:
krzywon
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:
ad92c5a
Parents:
8ffafd1
git-author:
Jeff Krzywon <krzywon@…> (04/17/17 16:30:10)
git-committer:
krzywon <krzywon@…> (04/17/17 16:30:10)
Message:

Added a 4th data loader exception for generic readers that cannot open file.

Location:
src/sas/sascalc/dataloader
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/file_reader_base_class.py

    rb09095a rda8bb53  
    1010from abc import abstractmethod 
    1111from loader_exceptions import NoKnownLoaderException, FileContentsException,\ 
    12     DataReaderException 
     12    DataReaderException, DefaultReaderException 
    1313from data_info import Data1D, Data2D, DataInfo, plottable_1D, plottable_2D,\ 
    1414    combine_data_info_with_plottable 
     
    4747        if os.path.isfile(filepath): 
    4848            basename, extension = os.path.splitext(os.path.basename(filepath)) 
     49            self.extension = extension.lower() 
    4950            # If the file type is not allowed, return nothing 
    50             if extension in self.ext or self.allow_all: 
     51            if self.extension in self.ext or self.allow_all: 
    5152                # Try to load the file, but raise an error if unable to. 
    5253                try: 
    53                     self.unit_converter() 
     54                    self.load_unit_converter() 
    5455                    self.f_open = open(filepath, 'rb') 
    5556                    self.get_file_contents() 
    5657                    self.sort_one_d_data() 
    57                 except RuntimeError: 
    58                     # Reader specific errors 
    59                     # TODO: Give a specific error. 
    60                     pass 
     58                except FileContentsException as e: 
     59                    self.handle_error_message(e.message) 
    6160                except OSError as e: 
    6261                    # If the file cannot be opened 
     
    6463                    msg += e.message 
    6564                    self.handle_error_message(msg) 
    66                 except Exception as e: 
    67                     # Handle any other generic error 
    68                     # TODO: raise or log? 
    69                     raise 
    7065                finally: 
     66                    # Close the file handle if it is open 
    7167                    if not self.f_open.closed: 
    7268                        self.f_open.close() 
     
    9894        self.output.append(data_obj) 
    9995 
    100     def unit_converter(self): 
     96    def load_unit_converter(self): 
    10197        """ 
    10298        Generic unit conversion import  
     
    134130        self.output = final_list 
    135131 
     132    def set_all_to_none(self): 
     133        """ 
     134        Set all mutable values to None for error handling purposes 
     135        """ 
     136        self.current_dataset = None 
     137        self.current_datainfo = None 
     138        self.output = [] 
     139 
    136140    @staticmethod 
    137141    def splitline(line): 
  • src/sas/sascalc/dataloader/loader.py

    r278ddee rda8bb53  
    2525import logging 
    2626import time 
     27import mimetypes 
    2728from zipfile import ZipFile 
    2829from sas.sascalc.data_util.registry import ExtensionRegistry 
    2930# Default readers are defined in the readers sub-module 
    3031import readers 
    31 from loader_exceptions import NoKnownLoaderException, FileContentsException 
     32from loader_exceptions import NoKnownLoaderException, FileContentsException,\ 
     33    DefaultReaderException 
    3234from readers import ascii_reader 
    3335from readers import cansas_reader 
     
    7577            ascii_loader = ascii_reader.Reader() 
    7678            return ascii_loader.read(path) 
    77         except FileContentsException: 
    78             pass  # try the cansas XML reader 
     79        except DefaultReaderException: 
     80            pass  # Loader specific error to try the cansas XML reader 
    7981        try: 
    8082            cansas_loader = cansas_reader.Reader() 
    8183            return cansas_loader.read(path) 
     84        except DefaultReaderException: 
     85            pass  # Loader specific error to try the cansas NeXuS reader 
    8286        except FileContentsException: 
    83             pass  # try the cansas NeXuS reader 
     87            # TODO: Handle errors properly 
     88            pass 
     89        except Exception as csr: 
     90            # TODO: Modify cansas reader to throw DefaultReaderException 
     91            pass 
    8492        try: 
    8593            cansas_nexus_loader = cansas_reader_HDF5.Reader() 
    8694            return cansas_nexus_loader.read(path) 
    87         except FileContentsException: 
     95        except DefaultReaderException: 
    8896            logging.errors("No default loader can load the data") 
    8997            # No known reader available. Give up and throw an error 
     
    92100            msg += "Traceback:\n%s" % e.message 
    93101            raise NoKnownLoaderException, msg 
     102        except FileContentsException: 
     103            # TODO: Handle error(s) properly 
     104            pass 
    94105 
    95106    def find_plugins(self, dir): 
  • src/sas/sascalc/dataloader/loader_exceptions.py

    r270c882b rda8bb53  
    2323 
    2424 
     25class DefaultReaderException(Exception): 
     26    """ 
     27    Exception for files with no associated reader. This should be thrown by 
     28    default readers only to tell Loader to try the next reader. 
     29    """ 
     30    def __init__(self, e): 
     31        self.message = e 
     32 
     33 
    2534class DataReaderException(Exception): 
    2635    """ 
  • src/sas/sascalc/dataloader/readers/ascii_reader.py

    r8ffafd1 rda8bb53  
    1717from sas.sascalc.dataloader.file_reader_base_class import FileReader 
    1818from sas.sascalc.dataloader.data_info import DataInfo, plottable_1D 
     19from sas.sascalc.dataloader.loader_exceptions import FileContentsException,\ 
     20    DefaultReaderException 
    1921 
    2022logger = logging.getLogger(__name__) 
     
    110112                line_no += 1 
    111113            except ValueError: 
     114                # ValueError is raised when non numeric strings conv. to float 
    112115                # It is data and meet non - number, then stop reading 
    113116                if is_data: 
     
    121124                # Reset # of lines of data candidates 
    122125                candidate_lines = 0 
    123             except Exception: 
    124                 # Handle any unexpected exceptions 
    125                 raise 
    126126 
    127127        if not is_data: 
    128             # TODO: Check file extension - primary reader, throw error. 
    129             # TODO: Secondary check, pass and try next reader 
    130             msg = "ascii_reader: x has no data" 
    131             raise RuntimeError(msg) 
     128            self.set_all_to_none() 
     129            if self.extension in self.ext: 
     130                msg = "ASCII Reader error: Fewer than five Q data points found " 
     131                msg += "in {}.".format(filepath) 
     132                raise FileContentsException(msg) 
     133            else: 
     134                msg = "ASCII Reader could not load the file {}".format(filepath) 
     135                raise DefaultReaderException(msg) 
    132136        # Sanity check 
    133137        if has_error_dy and not len(self.current_dataset.y) == \ 
    134138                len(self.current_dataset.dy): 
    135             msg = "ascii_reader: y and dy have different length" 
    136             raise RuntimeError(msg) 
     139            msg = "ASCII Reader error: Number of I and dI data points are" 
     140            msg += " different in {}.".format(filepath) 
     141            # TODO: Add error to self.current_datainfo.errors instead? 
     142            self.set_all_to_none() 
     143            raise FileContentsException(msg) 
    137144        if has_error_dx and not len(self.current_dataset.x) == \ 
    138145                len(self.current_dataset.dx): 
    139             msg = "ascii_reader: y and dy have different length" 
    140             raise RuntimeError(msg) 
    141         # If the data length is zero, consider this as 
    142         # though we were not able to read the file. 
    143         if len(self.current_dataset.x) < 1: 
    144             raise RuntimeError("ascii_reader: could not load file") 
     146            msg = "ASCII Reader error: Number of Q and dQ data points are" 
     147            msg += " different in {}.".format(filepath) 
     148            # TODO: Add error to self.current_datainfo.errors instead? 
     149            self.set_all_to_none() 
     150            raise FileContentsException(msg) 
    145151 
    146         # Data 
     152        # Remove any point where Q == 0 
    147153        x = self.current_dataset.x 
    148154        self.current_dataset.x = self.current_dataset.x[x != 0] 
Note: See TracChangeset for help on using the changeset viewer.