[d26dea0] | 1 | |
---|
| 2 | """ |
---|
| 3 | plugin DataLoader responsible of loading data |
---|
| 4 | """ |
---|
| 5 | import os |
---|
| 6 | import sys |
---|
| 7 | import wx |
---|
| 8 | import logging |
---|
| 9 | |
---|
[463e7ffc] | 10 | logger = logging.getLogger(__name__) |
---|
[c155a16] | 11 | |
---|
[b963b20] | 12 | from sas import get_local_config |
---|
| 13 | |
---|
[b699768] | 14 | from sas.sascalc.dataloader.loader import Loader |
---|
[7f75a3f] | 15 | from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException |
---|
[efe730d] | 16 | |
---|
[d85c194] | 17 | from sas.sasgui.guiframe.plugin_base import PluginBase |
---|
| 18 | from sas.sasgui.guiframe.events import StatusEvent |
---|
| 19 | from sas.sasgui.guiframe.gui_style import GUIFRAME |
---|
| 20 | from sas.sasgui.guiframe.gui_manager import DEFAULT_OPEN_FOLDER |
---|
[d26dea0] | 21 | |
---|
[efe730d] | 22 | config = get_local_config() |
---|
[d26dea0] | 23 | extension_list = [] |
---|
| 24 | if config.APPLICATION_STATE_EXTENSION is not None: |
---|
| 25 | extension_list.append(config.APPLICATION_STATE_EXTENSION) |
---|
| 26 | EXTENSIONS = config.PLUGIN_STATE_EXTENSIONS + extension_list |
---|
| 27 | PLUGINS_WLIST = config.PLUGINS_WLIST |
---|
| 28 | APPLICATION_WLIST = config.APPLICATION_WLIST |
---|
| 29 | |
---|
[7f75a3f] | 30 | |
---|
[d26dea0] | 31 | class Plugin(PluginBase): |
---|
| 32 | |
---|
[f21d496] | 33 | def __init__(self): |
---|
| 34 | PluginBase.__init__(self, name="DataLoader") |
---|
[d26dea0] | 35 | # Default location |
---|
| 36 | self._default_save_location = DEFAULT_OPEN_FOLDER |
---|
| 37 | self.loader = Loader() |
---|
| 38 | self._data_menu = None |
---|
| 39 | |
---|
| 40 | def populate_file_menu(self): |
---|
| 41 | """ |
---|
| 42 | get a menu item and append it under file menu of the application |
---|
| 43 | add load file menu item and load folder item |
---|
| 44 | """ |
---|
| 45 | # menu for data files |
---|
| 46 | data_file_hint = "load one or more data in the application" |
---|
| 47 | menu_list = [('&Load Data File(s)', data_file_hint, self.load_data)] |
---|
| 48 | gui_style = self.parent.get_style() |
---|
| 49 | style = gui_style & GUIFRAME.MULTIPLE_APPLICATIONS |
---|
| 50 | if style == GUIFRAME.MULTIPLE_APPLICATIONS: |
---|
| 51 | # menu for data from folder |
---|
| 52 | data_folder_hint = "load multiple data in the application" |
---|
| 53 | menu_list.append(('&Load Data Folder', data_folder_hint, |
---|
| 54 | self._load_folder)) |
---|
| 55 | return menu_list |
---|
| 56 | |
---|
| 57 | def load_data(self, event): |
---|
| 58 | """ |
---|
| 59 | Load data |
---|
| 60 | """ |
---|
| 61 | path = None |
---|
| 62 | self._default_save_location = self.parent._default_save_location |
---|
[235f514] | 63 | if self._default_save_location is None: |
---|
[d26dea0] | 64 | self._default_save_location = os.getcwd() |
---|
| 65 | |
---|
| 66 | cards = self.loader.get_wildcards() |
---|
| 67 | temp = [APPLICATION_WLIST] + PLUGINS_WLIST |
---|
| 68 | for item in temp: |
---|
| 69 | if item in cards: |
---|
| 70 | cards.remove(item) |
---|
| 71 | wlist = '|'.join(cards) |
---|
| 72 | style = wx.OPEN | wx.FD_MULTIPLE |
---|
| 73 | dlg = wx.FileDialog(self.parent, |
---|
| 74 | "Choose a file", |
---|
| 75 | self._default_save_location, "", |
---|
| 76 | wlist, |
---|
| 77 | style=style) |
---|
| 78 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 79 | file_list = dlg.GetPaths() |
---|
[c1d5aea] | 80 | if len(file_list) >= 0 and file_list[0] is not None: |
---|
[d26dea0] | 81 | self._default_save_location = os.path.dirname(file_list[0]) |
---|
| 82 | path = self._default_save_location |
---|
| 83 | dlg.Destroy() |
---|
| 84 | |
---|
| 85 | if path is None or not file_list or file_list[0] is None: |
---|
| 86 | return |
---|
| 87 | self.parent._default_save_location = self._default_save_location |
---|
| 88 | self.get_data(file_list) |
---|
| 89 | |
---|
| 90 | def can_load_data(self): |
---|
| 91 | """ |
---|
[20fa5fe] | 92 | if return True, then call handler to load data |
---|
[d26dea0] | 93 | """ |
---|
| 94 | return True |
---|
| 95 | |
---|
| 96 | def _load_folder(self, event): |
---|
| 97 | """ |
---|
| 98 | Load entire folder |
---|
| 99 | """ |
---|
| 100 | path = None |
---|
| 101 | self._default_save_location = self.parent._default_save_location |
---|
[235f514] | 102 | if self._default_save_location is None: |
---|
[d26dea0] | 103 | self._default_save_location = os.getcwd() |
---|
| 104 | dlg = wx.DirDialog(self.parent, "Choose a directory", |
---|
| 105 | self._default_save_location, |
---|
| 106 | style=wx.DD_DEFAULT_STYLE) |
---|
| 107 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 108 | path = dlg.GetPath() |
---|
| 109 | self._default_save_location = path |
---|
| 110 | dlg.Destroy() |
---|
| 111 | if path is not None: |
---|
| 112 | self._default_save_location = os.path.dirname(path) |
---|
| 113 | else: |
---|
| 114 | return |
---|
| 115 | file_list = self.get_file_path(path) |
---|
| 116 | self.get_data(file_list) |
---|
| 117 | self.parent._default_save_location = self._default_save_location |
---|
| 118 | |
---|
| 119 | def load_error(self, error=None): |
---|
| 120 | """ |
---|
| 121 | Pop up an error message. |
---|
| 122 | |
---|
| 123 | :param error: details error message to be displayed |
---|
| 124 | """ |
---|
| 125 | if error is not None or str(error).strip() != "": |
---|
[7f75a3f] | 126 | dial = wx.MessageDialog(self.parent, str(error), |
---|
| 127 | 'Error Loading File', |
---|
[d26dea0] | 128 | wx.OK | wx.ICON_EXCLAMATION) |
---|
| 129 | dial.ShowModal() |
---|
| 130 | |
---|
| 131 | def get_file_path(self, path): |
---|
| 132 | """ |
---|
| 133 | Receive a list containing folder then return a list of file |
---|
| 134 | """ |
---|
| 135 | if os.path.isdir(path): |
---|
[7f75a3f] | 136 | return [os.path.join(os.path.abspath(path), filename) for filename |
---|
| 137 | in os.listdir(path)] |
---|
[d26dea0] | 138 | |
---|
| 139 | def _process_data_and_errors(self, item, p_file, output, message): |
---|
| 140 | """ |
---|
| 141 | Check to see if data set loaded with any errors. If so, append to |
---|
| 142 | error message to be sure user knows the issue. |
---|
| 143 | """ |
---|
| 144 | data_error = False |
---|
[faa3ae7] | 145 | if hasattr(item, 'errors'): |
---|
| 146 | for error_data in item.errors: |
---|
| 147 | data_error = True |
---|
| 148 | message += "\tError: {0}\n".format(error_data) |
---|
| 149 | else: |
---|
[c155a16] | 150 | logger.error("Loader returned an invalid object:\n %s" % str(item)) |
---|
[d26dea0] | 151 | data_error = True |
---|
[cba0c77] | 152 | |
---|
[d26dea0] | 153 | data = self.parent.create_gui_data(item, p_file) |
---|
| 154 | output[data.id] = data |
---|
| 155 | return output, message, data_error |
---|
| 156 | |
---|
| 157 | def get_data(self, path, format=None): |
---|
| 158 | """ |
---|
| 159 | """ |
---|
[a674d0b] | 160 | file_errors = {} |
---|
[d26dea0] | 161 | output = {} |
---|
[a674d0b] | 162 | exception_occurred = False |
---|
[cba0c77] | 163 | |
---|
[d26dea0] | 164 | for p_file in path: |
---|
| 165 | basename = os.path.basename(p_file) |
---|
[69400ec] | 166 | # Skip files that start with a period |
---|
| 167 | if basename.startswith("."): |
---|
| 168 | msg = "The folder included a potential hidden file - %s." \ |
---|
| 169 | % basename |
---|
| 170 | msg += " Do you wish to load this file as data?" |
---|
| 171 | msg_box = wx.MessageDialog(None, msg, 'Warning', |
---|
| 172 | wx.OK | wx.CANCEL) |
---|
| 173 | if msg_box.ShowModal() == wx.ID_CANCEL: |
---|
| 174 | continue |
---|
[d26dea0] | 175 | _, extension = os.path.splitext(basename) |
---|
| 176 | if extension.lower() in EXTENSIONS: |
---|
| 177 | log_msg = "Data Loader cannot " |
---|
[a674d0b] | 178 | log_msg += "load: {}\n".format(str(p_file)) |
---|
| 179 | log_msg += "Please try to open that file from \"open project\"" |
---|
| 180 | log_msg += "or \"open analysis\" menu." |
---|
[c155a16] | 181 | logger.info(log_msg) |
---|
[a674d0b] | 182 | file_errors[basename] = [log_msg] |
---|
[d26dea0] | 183 | continue |
---|
| 184 | try: |
---|
[a674d0b] | 185 | message = "Loading {}...\n".format(p_file) |
---|
[2924532] | 186 | self.load_update(message=message, info="info") |
---|
[d26dea0] | 187 | temp = self.loader.load(p_file, format) |
---|
[a674d0b] | 188 | if not isinstance(temp, list): |
---|
| 189 | temp = [temp] |
---|
| 190 | for item in temp: |
---|
| 191 | error_message = "" |
---|
[d26dea0] | 192 | output, error_message, data_error = \ |
---|
[a674d0b] | 193 | self._process_data_and_errors(item, |
---|
| 194 | p_file, |
---|
| 195 | output, |
---|
| 196 | error_message) |
---|
| 197 | if data_error: |
---|
| 198 | if basename in file_errors.keys(): |
---|
| 199 | file_errors[basename] += [error_message] |
---|
| 200 | else: |
---|
| 201 | file_errors[basename] = [error_message] |
---|
| 202 | |
---|
[2924532] | 203 | self.load_update(message="Loaded {}\n".format(p_file), |
---|
| 204 | info="info") |
---|
[7f75a3f] | 205 | except NoKnownLoaderException as e: |
---|
[9df816b] | 206 | exception_occurred = True |
---|
[9c7e2b8] | 207 | error_message = "Loading data failed!" + e.message |
---|
[7ab3b79b] | 208 | file_errors[basename] = [error_message] |
---|
[9df816b] | 209 | except Exception as e: |
---|
| 210 | exception_occurred = True |
---|
| 211 | file_err = "The Data file you selected could not be " |
---|
| 212 | file_err += "loaded.\nMake sure the content of your file" |
---|
| 213 | file_err += " is properly formatted.\n" |
---|
| 214 | file_err += "When contacting the SasView team, mention the" |
---|
| 215 | file_err += " following:\n" |
---|
[fafe52a] | 216 | file_err += e.message |
---|
[009abcc] | 217 | file_errors[basename] = [file_err] |
---|
[a674d0b] | 218 | |
---|
| 219 | if len(file_errors) > 0: |
---|
| 220 | error_message = "" |
---|
| 221 | for filename, error_array in file_errors.iteritems(): |
---|
[2924532] | 222 | error_message += "The following issues were found whilst " |
---|
[a674d0b] | 223 | error_message += "loading {}:\n".format(filename) |
---|
| 224 | for message in error_array: |
---|
| 225 | error_message += message + "\n" |
---|
[2924532] | 226 | self.load_complete(output=output, |
---|
| 227 | message=error_message, |
---|
| 228 | info="error") |
---|
[9df816b] | 229 | |
---|
[2924532] | 230 | elif not exception_occurred: # Everything loaded as expected |
---|
[9df816b] | 231 | self.load_complete(output=output, message="Loading data complete!", |
---|
| 232 | info="info") |
---|
| 233 | else: |
---|
| 234 | self.load_complete(output=None, message=error_message, info="error") |
---|
[a674d0b] | 235 | |
---|
[2924532] | 236 | def load_update(self, message="", info="warning"): |
---|
[d26dea0] | 237 | """ |
---|
| 238 | print update on the status bar |
---|
| 239 | """ |
---|
| 240 | if message != "": |
---|
[2924532] | 241 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 242 | info=info, |
---|
[d26dea0] | 243 | type="progress")) |
---|
[7f75a3f] | 244 | |
---|
| 245 | def load_complete(self, output, message="", info="warning"): |
---|
[d26dea0] | 246 | """ |
---|
[7f75a3f] | 247 | post message to status bar and return list of data |
---|
[d26dea0] | 248 | """ |
---|
[2924532] | 249 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 250 | info=info, |
---|
[d26dea0] | 251 | type="stop")) |
---|
[8dec7e7] | 252 | if output is not None: |
---|
| 253 | self.parent.add_data(data_list=output) |
---|