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