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