Changeset 7f75a3f in sasview


Ignore:
Timestamp:
Apr 5, 2017 11:08:59 AM (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:
278ddee
Parents:
69400ec
git-author:
Jeff Krzywon <krzywon@…> (04/05/17 11:08:59)
git-committer:
krzywon <krzywon@…> (04/05/17 11:08:59)
Message:

More explicit error messages when file loading fails. see #889

Location:
src/sas
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/data_util/registry.py

    r270c882b r7f75a3f  
    9494        Return the loader associated with the file type of path. 
    9595         
    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 
    9799        """         
    98100        # Find matching extensions 
     
    113115        if len(loaders) == 0: 
    114116            raise ValueError("Unknown file type for "+path) 
    115         # All done 
    116117        return loaders 
    117118 
     
    120121        Call the loader for the file type of path. 
    121122 
    122         Raises ValueError if no loader is available. 
    123         Raises KeyError if 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. 
    125126        """ 
     127        loaders = [] 
    126128        if format is None: 
    127129            try: 
     
    141143        # If we get here it is because all loaders failed 
    142144        raise NoKnownLoaderException(e.message)  # raise generic exception 
    143  
    144  
    145 # TODO: Move this to the unit test folder 
    146 def test(): 
    147     reg = ExtensionRegistry() 
    148     class CxError(Exception): pass 
    149     def cx(file): return 'cx' 
    150     def new_cx(file): return 'new_cx' 
    151     def fail_cx(file): raise CxError 
    152     def cat(file): return 'cat' 
    153     def gunzip(file): return 'gunzip' 
    154     reg['.cx'] = cx 
    155     reg['.cx1'] = cx 
    156     reg['.cx'] = new_cx 
    157     reg['.gz'] = gunzip 
    158     reg['.cx.gz'] = new_cx 
    159     reg['.cx1.gz'] = fail_cx 
    160     reg['.cx1'] = fail_cx 
    161     reg['.cx2'] = fail_cx 
    162     reg['new_cx'] = new_cx 
    163  
    164     # Two loaders associated with .cx 
    165     assert reg.lookup('hello.cx') == [new_cx,cx] 
    166     # Make sure the last loader applies first 
    167     assert reg.load('hello.cx') == 'new_cx' 
    168     # Make sure the next loader applies if the first fails 
    169     assert reg.load('hello.cx1') == 'cx' 
    170     # Make sure the format override works 
    171     assert reg.load('hello.cx1',format='.cx.gz') == 'new_cx' 
    172     # Make sure the format override works 
    173     assert reg.load('hello.cx1',format='new_cx') == 'new_cx' 
    174     # Make sure the case of all loaders failing is correct 
    175     try:  reg.load('hello.cx2') 
    176     except CxError: pass # correct failure 
    177     else: raise AssertError,"Incorrect error on load failure" 
    178     # Make sure the case of no loaders fails correctly 
    179     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 filename 
    187     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 fails 
    190  
    191 if __name__ == "__main__": test() 
  • src/sas/sascalc/dataloader/loader.py

    r270c882b r7f75a3f  
    7070        except NoKnownLoaderException as e: 
    7171            pass  # try the ASCII reader 
    72         except FileContentsException as e: 
    73             pass 
    7472        try: 
    7573            ascii_loader = ascii_reader.Reader() 
     
    8684            return cansas_nexus_loader.read(path) 
    8785        except FileContentsException: 
     86            logging.errors("No default loader can load the data") 
    8887            # No known reader available. Give up and throw an error 
    8988            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 
    9292 
    9393    def find_plugins(self, dir): 
  • src/sas/sascalc/dataloader/readers/ascii_reader.py

    r9a5097c r7f75a3f  
    1717import os 
    1818from sas.sascalc.dataloader.data_info import Data1D 
     19from sas.sascalc.dataloader.loader_exceptions import FileContentsException 
    1920 
    2021# Check whether we have a converter available 
     
    173174                if not is_data: 
    174175                    msg = "ascii_reader: x has no data" 
    175                     raise RuntimeError, msg 
     176                    raise FileContentsException, msg 
    176177                # Sanity check 
    177178                if has_error_dy == True and not len(ty) == len(tdy): 
    178179                    msg = "ascii_reader: y and dy have different length" 
    179                     raise RuntimeError, msg 
     180                    raise FileContentsException, msg 
    180181                if has_error_dx == True and not len(tx) == len(tdx): 
    181182                    msg = "ascii_reader: y and dy have different length" 
    182                     raise RuntimeError, msg 
     183                    raise FileContentsException, msg 
    183184                # If the data length is zero, consider this as 
    184185                # though we were not able to read the file. 
    185186                if len(tx) == 0: 
    186                     raise RuntimeError, "ascii_reader: could not load file" 
     187                    raise FileContentsException, "ascii_reader: could not load file" 
    187188 
    188189                #Let's re-order the data to make cal. 
  • src/sas/sascalc/dataloader/readers/cansas_reader.py

    r8434365 r7f75a3f  
    2929from sas.sascalc.dataloader.readers.xml_reader import XMLreader 
    3030from sas.sascalc.dataloader.readers.cansas_constants import CansasConstants, CurrentLevel 
     31from sas.sascalc.dataloader.loader_exceptions import FileContentsException 
    3132 
    3233# The following 2 imports *ARE* used. Do not remove either. 
     
    534535 
    535536        # 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 
    537543        self.cansas_version = self.xmlroot.get("version", "1.0") 
    538544 
  • src/sas/sascalc/dataloader/readers/cansas_reader_HDF5.py

    rc94280c r7f75a3f  
    1313    TransmissionSpectrum, Detector 
    1414from sas.sascalc.dataloader.data_info import combine_data_info_with_plottable 
     15from sas.sascalc.dataloader.loader_exceptions import FileContentsException 
    1516 
    1617 
     
    7576            if extension in self.ext or self.allow_all: 
    7677                # 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 
    7882                # Read in all child elements of top level SASroot 
    7983                self.read_children(self.raw_data, []) 
  • src/sas/sascalc/dataloader/readers/xml_reader.py

    ra235f715 r7f75a3f  
    7070            self.xmldoc = etree.parse(self.xml, parser=PARSER) 
    7171            self.xmlroot = self.xmldoc.getroot() 
    72         except etree.XMLSyntaxError as xml_error: 
    73             logging.info(xml_error) 
     72        except etree.XMLSyntaxError: 
     73            raise 
    7474        except Exception: 
    7575            self.xml = None 
  • src/sas/sasgui/guiframe/local_perspectives/data_loader/data_loader.py

    r69400ec r7f75a3f  
    99 
    1010from sas.sascalc.dataloader.loader import Loader 
     11from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException 
    1112from sas.sasgui.guiframe.plugin_base import PluginBase 
    1213from sas.sasgui.guiframe.events import StatusEvent 
     
    3940APPLICATION_WLIST = config.APPLICATION_WLIST 
    4041 
     42 
    4143class Plugin(PluginBase): 
    4244 
     
    5456        """ 
    5557        # menu for data files 
    56         menu_list = [] 
    5758        data_file_hint = "load one or more data in the application" 
    5859        menu_list = [('&Load Data File(s)', data_file_hint, self.load_data)] 
    5960        gui_style = self.parent.get_style() 
    6061        style = gui_style & GUIFRAME.MULTIPLE_APPLICATIONS 
    61         style1 = gui_style & GUIFRAME.DATALOADER_ON 
    6262        if style == GUIFRAME.MULTIPLE_APPLICATIONS: 
    6363            # menu for data from folder 
     
    100100        self.get_data(file_list) 
    101101 
    102  
    103102    def can_load_data(self): 
    104103        """ 
     
    106105        """ 
    107106        return True 
    108  
    109107 
    110108    def _load_folder(self, event): 
     
    114112        path = None 
    115113        self._default_save_location = self.parent._default_save_location 
    116         if self._default_save_location == None: 
     114        if self._default_save_location is None: 
    117115            self._default_save_location = os.getcwd() 
    118116        dlg = wx.DirDialog(self.parent, "Choose a directory", 
     
    138136        """ 
    139137        if error is not None or str(error).strip() != "": 
    140             dial = wx.MessageDialog(self.parent, str(error), 'Error Loading File', 
     138            dial = wx.MessageDialog(self.parent, str(error), 
     139                                    'Error Loading File', 
    141140                                    wx.OK | wx.ICON_EXCLAMATION) 
    142141            dial.ShowModal() 
     
    147146        """ 
    148147        if os.path.isdir(path): 
    149             return [os.path.join(os.path.abspath(path), filename) for filename in os.listdir(path)] 
     148            return [os.path.join(os.path.abspath(path), filename) for filename 
     149                    in os.listdir(path)] 
    150150 
    151151    def _process_data_and_errors(self, item, p_file, output, message): 
     
    220220                info="info") 
    221221 
     222            except NoKnownLoaderException as e: 
     223                logging.error(e.message) 
     224                self.load_update(output=None, message=e.message, info="warning") 
     225                self.load_complete(output=None, 
     226                                   message="Loading data failed!", 
     227                                   info="warning") 
    222228            except: 
    223229                logging.error(sys.exc_value) 
    224230 
    225                 error_message = "The Data file you selected could not be loaded.\n" 
    226                 error_message += "Make sure the content of your file" 
     231                error_message = "The Data file you selected could not be " 
     232                error_message += "loaded.\nMake sure the content of your file" 
    227233                error_message += " is properly formatted.\n" 
    228234                error_message += "When contacting the SasView team, mention the" 
     
    230236                error_message += "Error: " + str(sys.exc_info()[1]) 
    231237                file_errors[basename] = [error_message] 
    232                 self.load_update(output=output, message=error_message, info="warning") 
     238                self.load_update(output=output, message=error_message, 
     239                                 info="warning") 
    233240 
    234241        if len(file_errors) > 0: 
     
    243250 
    244251        self.load_complete(output=output, message="Loading data complete!", 
    245             info="info") 
     252                           info="info") 
    246253 
    247254    def load_update(self, output=None, message="", info="warning"): 
     
    252259            wx.PostEvent(self.parent, StatusEvent(status=message, info=info, 
    253260                                                  type="progress")) 
    254     def load_complete(self, output, message="", error_message="", path=None, 
    255                       info="warning"): 
    256         """ 
    257          post message to  status bar and return list of data 
    258         """ 
    259         wx.PostEvent(self.parent, StatusEvent(status=message, 
    260                                               info=info, 
     261 
     262    def load_complete(self, output, message="", info="warning"): 
     263        """ 
     264         post message to status bar and return list of data 
     265        """ 
     266        wx.PostEvent(self.parent, StatusEvent(status=message, info=info, 
    261267                                              type="stop")) 
    262         # if error_message != "": 
    263         #    self.load_error(error_message) 
    264268        self.parent.add_data(data_list=output) 
Note: See TracChangeset for help on using the changeset viewer.