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