[b7c7a1c] | 1 | |
---|
| 2 | """ |
---|
| 3 | plugin DataLoader responsible of loading data |
---|
| 4 | """ |
---|
[f444b20] | 5 | import os |
---|
| 6 | import sys |
---|
| 7 | import wx |
---|
[df7046f] | 8 | import logging |
---|
[b7c7a1c] | 9 | |
---|
[79492222] | 10 | from sas.dataloader.loader import Loader |
---|
| 11 | import sas.dataloader.data_info as DataInfo |
---|
| 12 | from sas.guiframe.plugin_base import PluginBase |
---|
| 13 | from sas.guiframe.events import StatusEvent |
---|
| 14 | from sas.guiframe.events import NewPlotEvent |
---|
| 15 | from sas.guiframe.dataFitting import Data1D |
---|
| 16 | from sas.guiframe.dataFitting import Data2D |
---|
| 17 | from sas.guiframe.utils import parse_name |
---|
| 18 | from sas.guiframe.gui_style import GUIFRAME |
---|
| 19 | from sas.guiframe.gui_manager import DEFAULT_OPEN_FOLDER |
---|
[957723f] | 20 | try: |
---|
| 21 | # Try to find a local config |
---|
| 22 | import imp |
---|
| 23 | path = os.getcwd() |
---|
| 24 | if(os.path.isfile("%s/%s.py" % (path, 'local_config'))) or \ |
---|
| 25 | (os.path.isfile("%s/%s.pyc" % (path, 'local_config'))): |
---|
| 26 | fObj, path, descr = imp.find_module('local_config', [path]) |
---|
| 27 | config = imp.load_module('local_config', fObj, path, descr) |
---|
| 28 | else: |
---|
| 29 | # Try simply importing local_config |
---|
| 30 | import local_config as config |
---|
| 31 | except: |
---|
| 32 | # Didn't find local config, load the default |
---|
[7a04dbb] | 33 | import sas.guiframe.config as config |
---|
[d72ef56] | 34 | |
---|
[70ecd530] | 35 | if config is None: |
---|
[7a04dbb] | 36 | import sas.guiframe.config as config |
---|
[70ecd530] | 37 | |
---|
[d72ef56] | 38 | |
---|
[957723f] | 39 | extension_list = [] |
---|
[84e5533] | 40 | if config.APPLICATION_STATE_EXTENSION is not None: |
---|
| 41 | extension_list.append(config.APPLICATION_STATE_EXTENSION) |
---|
| 42 | EXTENSIONS = config.PLUGIN_STATE_EXTENSIONS + extension_list |
---|
[87f4dcc] | 43 | PLUGINS_WLIST = config.PLUGINS_WLIST |
---|
| 44 | APPLICATION_WLIST = config.APPLICATION_WLIST |
---|
| 45 | |
---|
[b7c7a1c] | 46 | class Plugin(PluginBase): |
---|
| 47 | |
---|
| 48 | def __init__(self, standalone=False): |
---|
[f444b20] | 49 | PluginBase.__init__(self, name="DataLoader", standalone=standalone) |
---|
| 50 | #Default location |
---|
[d72ef56] | 51 | self._default_save_location = DEFAULT_OPEN_FOLDER |
---|
[75fbd17] | 52 | self.loader = Loader() |
---|
| 53 | self._data_menu = None |
---|
| 54 | |
---|
[b2629b4] | 55 | def help(self, evt): |
---|
| 56 | """ |
---|
| 57 | Show a general help dialog. |
---|
| 58 | """ |
---|
| 59 | from help_panel import HelpWindow |
---|
[805e2e7] | 60 | frame = HelpWindow(None, -1) |
---|
| 61 | if hasattr(frame, "IsIconized"): |
---|
| 62 | if not frame.IsIconized(): |
---|
| 63 | try: |
---|
| 64 | icon = self.parent.GetIcon() |
---|
| 65 | frame.SetIcon(icon) |
---|
| 66 | except: |
---|
| 67 | pass |
---|
[b2629b4] | 68 | frame.Show(True) |
---|
| 69 | |
---|
[570cb96] | 70 | def populate_file_menu(self): |
---|
[75fbd17] | 71 | """ |
---|
[570cb96] | 72 | get a menu item and append it under file menu of the application |
---|
| 73 | add load file menu item and load folder item |
---|
[75fbd17] | 74 | """ |
---|
[570cb96] | 75 | #menu for data files |
---|
| 76 | menu_list = [] |
---|
| 77 | data_file_hint = "load one or more data in the application" |
---|
[a03d419] | 78 | menu_list = [('&Load Data File(s)', data_file_hint, self.load_data)] |
---|
[570cb96] | 79 | gui_style = self.parent.get_style() |
---|
| 80 | style = gui_style & GUIFRAME.MULTIPLE_APPLICATIONS |
---|
| 81 | style1 = gui_style & GUIFRAME.DATALOADER_ON |
---|
| 82 | if style == GUIFRAME.MULTIPLE_APPLICATIONS: |
---|
| 83 | #menu for data from folder |
---|
| 84 | data_folder_hint = "load multiple data in the application" |
---|
| 85 | menu_list.append(('&Load Data Folder', data_folder_hint, |
---|
| 86 | self._load_folder)) |
---|
| 87 | return menu_list |
---|
[e26d0db] | 88 | |
---|
[75fbd17] | 89 | |
---|
[a03d419] | 90 | def load_data(self, event): |
---|
[f444b20] | 91 | """ |
---|
| 92 | Load data |
---|
| 93 | """ |
---|
[e75b5fa] | 94 | path = None |
---|
[c553b18] | 95 | self._default_save_location = self.parent._default_save_location |
---|
[f444b20] | 96 | if self._default_save_location == None: |
---|
| 97 | self._default_save_location = os.getcwd() |
---|
| 98 | |
---|
| 99 | cards = self.loader.get_wildcards() |
---|
[87f4dcc] | 100 | temp = [APPLICATION_WLIST] + PLUGINS_WLIST |
---|
| 101 | for item in temp: |
---|
| 102 | if item in cards: |
---|
| 103 | cards.remove(item) |
---|
[75fbd17] | 104 | wlist = '|'.join(cards) |
---|
| 105 | style = wx.OPEN|wx.FD_MULTIPLE |
---|
[f444b20] | 106 | dlg = wx.FileDialog(self.parent, |
---|
| 107 | "Choose a file", |
---|
| 108 | self._default_save_location, "", |
---|
| 109 | wlist, |
---|
| 110 | style=style) |
---|
| 111 | if dlg.ShowModal() == wx.ID_OK: |
---|
[75fbd17] | 112 | file_list = dlg.GetPaths() |
---|
| 113 | if len(file_list) >= 0 and not(file_list[0]is None): |
---|
| 114 | self._default_save_location = os.path.dirname(file_list[0]) |
---|
[e75b5fa] | 115 | path = self._default_save_location |
---|
[f444b20] | 116 | dlg.Destroy() |
---|
[75fbd17] | 117 | |
---|
[e75b5fa] | 118 | if path is None or not file_list or file_list[0] is None: |
---|
[75fbd17] | 119 | return |
---|
[d72ef56] | 120 | self.parent._default_save_location = self._default_save_location |
---|
[75fbd17] | 121 | self.get_data(file_list) |
---|
[e75b5fa] | 122 | |
---|
[75fbd17] | 123 | |
---|
| 124 | def can_load_data(self): |
---|
[f444b20] | 125 | """ |
---|
[75fbd17] | 126 | if return True, then call handler to laod data |
---|
| 127 | """ |
---|
| 128 | return True |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | def _load_folder(self, event): |
---|
| 132 | """ |
---|
| 133 | Load entire folder |
---|
[f444b20] | 134 | """ |
---|
[e75b5fa] | 135 | path = None |
---|
[c553b18] | 136 | self._default_save_location = self.parent._default_save_location |
---|
[f444b20] | 137 | if self._default_save_location == None: |
---|
| 138 | self._default_save_location = os.getcwd() |
---|
| 139 | dlg = wx.DirDialog(self.parent, "Choose a directory", |
---|
| 140 | self._default_save_location, |
---|
| 141 | style=wx.DD_DEFAULT_STYLE) |
---|
| 142 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 143 | path = dlg.GetPath() |
---|
| 144 | self._default_save_location = path |
---|
| 145 | dlg.Destroy() |
---|
[75fbd17] | 146 | if path is not None: |
---|
| 147 | self._default_save_location = os.path.dirname(path) |
---|
| 148 | else: |
---|
| 149 | return |
---|
| 150 | file_list = self.get_file_path(path) |
---|
| 151 | self.get_data(file_list) |
---|
[d72ef56] | 152 | self.parent._default_save_location = self._default_save_location |
---|
[e75b5fa] | 153 | |
---|
[f444b20] | 154 | def load_error(self, error=None): |
---|
| 155 | """ |
---|
| 156 | Pop up an error message. |
---|
| 157 | |
---|
| 158 | :param error: details error message to be displayed |
---|
| 159 | """ |
---|
[8cb8c89] | 160 | if error is not None or str(error).strip() != "": |
---|
| 161 | dial = wx.MessageDialog(self.parent, str(error), 'Error Loading File', |
---|
[f444b20] | 162 | wx.OK | wx.ICON_EXCLAMATION) |
---|
[8cb8c89] | 163 | dial.ShowModal() |
---|
[f444b20] | 164 | |
---|
| 165 | def get_file_path(self, path): |
---|
| 166 | """ |
---|
| 167 | Receive a list containing folder then return a list of file |
---|
| 168 | """ |
---|
| 169 | if os.path.isdir(path): |
---|
| 170 | return [os.path.join(os.path.abspath(path), |
---|
| 171 | file) for file in os.listdir(path)] |
---|
[75fbd17] | 172 | |
---|
[b3efb7d] | 173 | def _process_data_and_errors(self, item, p_file, output, message): |
---|
| 174 | """ |
---|
| 175 | Check to see if data set loaded with any errors. If so, append to |
---|
| 176 | error message to be sure user knows the issue. |
---|
| 177 | """ |
---|
| 178 | data_error = False |
---|
| 179 | for error_data in item.errors: |
---|
| 180 | data_error = True |
---|
| 181 | message += "\tError: {0}\n".format(error_data) |
---|
| 182 | data = self.parent.create_gui_data(item, p_file) |
---|
| 183 | output[data.id] = data |
---|
| 184 | return output, message, data_error |
---|
| 185 | |
---|
[75fbd17] | 186 | def get_data(self, path, format=None): |
---|
[f444b20] | 187 | """ |
---|
| 188 | """ |
---|
| 189 | message = "" |
---|
[df7046f] | 190 | log_msg = '' |
---|
[e88ebfd] | 191 | output = {} |
---|
[986da97] | 192 | any_error = False |
---|
[b3efb7d] | 193 | data_error = False |
---|
[f444b20] | 194 | error_message = "" |
---|
| 195 | for p_file in path: |
---|
[986da97] | 196 | info = "info" |
---|
[f444b20] | 197 | basename = os.path.basename(p_file) |
---|
[b3efb7d] | 198 | _, extension = os.path.splitext(basename) |
---|
[75fbd17] | 199 | if extension.lower() in EXTENSIONS: |
---|
[986da97] | 200 | any_error = True |
---|
[75fbd17] | 201 | log_msg = "Data Loader cannot " |
---|
| 202 | log_msg += "load: %s\n" % str(p_file) |
---|
[99f9ecf] | 203 | log_msg += """Please try to open that file from "open project" """ |
---|
| 204 | log_msg += """or "open analysis" menu\n""" |
---|
| 205 | error_message = log_msg + "\n" |
---|
[75fbd17] | 206 | logging.info(log_msg) |
---|
| 207 | continue |
---|
| 208 | |
---|
[f444b20] | 209 | try: |
---|
[b3efb7d] | 210 | message = "Loading Data... " + str(p_file) + "\n" |
---|
| 211 | self.load_update(output=output, message=message, info=info) |
---|
[ec489f5] | 212 | temp = self.loader.load(p_file, format) |
---|
[f444b20] | 213 | if temp.__class__.__name__ == "list": |
---|
| 214 | for item in temp: |
---|
[b3efb7d] | 215 | output, error_message, data_error = \ |
---|
| 216 | self._process_data_and_errors(item, |
---|
| 217 | p_file, |
---|
| 218 | output, |
---|
| 219 | error_message) |
---|
[f444b20] | 220 | else: |
---|
[b3efb7d] | 221 | output, error_message, data_error = \ |
---|
| 222 | self._process_data_and_errors(temp, |
---|
| 223 | p_file, |
---|
| 224 | output, |
---|
| 225 | error_message) |
---|
[f444b20] | 226 | except: |
---|
[986da97] | 227 | any_error = True |
---|
[b3efb7d] | 228 | if any_error or error_message != "": |
---|
| 229 | if error_message == "": |
---|
| 230 | error = "Error: " + str(sys.exc_value) + "\n" |
---|
| 231 | error += "while loading Data: \n%s\n" % str(p_file) |
---|
| 232 | error_message = "The data file you selected could not be loaded.\n" |
---|
| 233 | error_message += "Make sure the content of your file" |
---|
| 234 | error_message += " is properly formatted.\n\n" |
---|
[b45cde3] | 235 | error_message += "When contacting the SasView team, mention the" |
---|
[b3efb7d] | 236 | error_message += " following:\n%s" % str(error) |
---|
| 237 | elif data_error: |
---|
| 238 | base_message = "Errors occurred while loading {0}\n".format(p_file) |
---|
| 239 | base_message += "The data file loaded but with errors.\n" |
---|
| 240 | error_message = base_message + error_message |
---|
| 241 | else: |
---|
| 242 | error_message += "%s\n"% str(p_file) |
---|
| 243 | info = "error" |
---|
| 244 | self.load_update(output=output, message=error_message, |
---|
[c151afc] | 245 | info=info) |
---|
[f444b20] | 246 | |
---|
[b3efb7d] | 247 | else: |
---|
| 248 | message = "Loading Data Complete! " |
---|
[df7046f] | 249 | message += log_msg |
---|
[f444b20] | 250 | self.load_complete(output=output, error_message=error_message, |
---|
[c151afc] | 251 | message=message, path=path, info=info) |
---|
[f444b20] | 252 | |
---|
[c151afc] | 253 | def load_update(self, output=None, message="", info="warning"): |
---|
[f444b20] | 254 | """ |
---|
| 255 | print update on the status bar |
---|
| 256 | """ |
---|
| 257 | if message != "": |
---|
[986da97] | 258 | wx.PostEvent(self.parent, StatusEvent(status=message, info='info', |
---|
[c151afc] | 259 | type="progress")) |
---|
| 260 | def load_complete(self, output, message="", error_message="", path=None, |
---|
| 261 | info="warning"): |
---|
[f444b20] | 262 | """ |
---|
| 263 | post message to status bar and return list of data |
---|
| 264 | """ |
---|
[c151afc] | 265 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 266 | info=info, |
---|
[f444b20] | 267 | type="stop")) |
---|
[986da97] | 268 | #if error_message != "": |
---|
| 269 | # self.load_error(error_message) |
---|
[e88ebfd] | 270 | self.parent.add_data(data_list=output) |
---|
[75fbd17] | 271 | |
---|
| 272 | |
---|
[f444b20] | 273 | |
---|
[75fbd17] | 274 | |
---|