[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 |
---|
[b7c7a1c] | 38 | |
---|
| 39 | class Plugin(PluginBase): |
---|
| 40 | |
---|
| 41 | def __init__(self, standalone=False): |
---|
[f444b20] | 42 | PluginBase.__init__(self, name="DataLoader", standalone=standalone) |
---|
| 43 | #Default location |
---|
| 44 | self._default_save_location = None |
---|
[75fbd17] | 45 | self.loader = Loader() |
---|
| 46 | self._data_menu = None |
---|
| 47 | |
---|
[570cb96] | 48 | def populate_file_menu(self): |
---|
[75fbd17] | 49 | """ |
---|
[570cb96] | 50 | get a menu item and append it under file menu of the application |
---|
| 51 | add load file menu item and load folder item |
---|
[75fbd17] | 52 | """ |
---|
[570cb96] | 53 | #menu for data files |
---|
| 54 | menu_list = [] |
---|
| 55 | data_file_hint = "load one or more data in the application" |
---|
| 56 | menu_list = [('&Load Data File(s)', data_file_hint, self._load_data)] |
---|
| 57 | gui_style = self.parent.get_style() |
---|
| 58 | style = gui_style & GUIFRAME.MULTIPLE_APPLICATIONS |
---|
| 59 | style1 = gui_style & GUIFRAME.DATALOADER_ON |
---|
| 60 | if style == GUIFRAME.MULTIPLE_APPLICATIONS: |
---|
| 61 | #menu for data from folder |
---|
| 62 | data_folder_hint = "load multiple data in the application" |
---|
| 63 | menu_list.append(('&Load Data Folder', data_folder_hint, |
---|
| 64 | self._load_folder)) |
---|
| 65 | return menu_list |
---|
| 66 | |
---|
| 67 | def unused_populate_menu(self, parent): |
---|
| 68 | """ |
---|
| 69 | """ |
---|
| 70 | # Add menu data |
---|
[75fbd17] | 71 | self._data_menu = wx.Menu() |
---|
| 72 | #menu for data files |
---|
| 73 | data_file_id = wx.NewId() |
---|
| 74 | data_file_hint = "load one or more data in the application" |
---|
| 75 | self._data_menu.Append(data_file_id, |
---|
| 76 | '&Load Data File(s)', data_file_hint) |
---|
| 77 | wx.EVT_MENU(self.parent, data_file_id, self._load_data) |
---|
| 78 | gui_style = self.parent.get_style() |
---|
| 79 | style = gui_style & GUIFRAME.MULTIPLE_APPLICATIONS |
---|
| 80 | style1 = gui_style & GUIFRAME.DATALOADER_ON |
---|
| 81 | if style == GUIFRAME.MULTIPLE_APPLICATIONS: |
---|
| 82 | #menu for data from folder |
---|
| 83 | data_folder_id = wx.NewId() |
---|
| 84 | data_folder_hint = "load multiple data in the application" |
---|
| 85 | self._data_menu.Append(data_folder_id, |
---|
| 86 | '&Load Data Folder', data_folder_hint) |
---|
| 87 | wx.EVT_MENU(self.parent, data_folder_id, self._load_folder) |
---|
| 88 | |
---|
| 89 | return [(self._data_menu, 'Data')] |
---|
| 90 | |
---|
| 91 | def _load_data(self, event): |
---|
[f444b20] | 92 | """ |
---|
| 93 | Load data |
---|
| 94 | """ |
---|
[e75b5fa] | 95 | path = None |
---|
[f444b20] | 96 | if self._default_save_location == None: |
---|
| 97 | self._default_save_location = os.getcwd() |
---|
| 98 | |
---|
| 99 | cards = self.loader.get_wildcards() |
---|
[75fbd17] | 100 | wlist = '|'.join(cards) |
---|
| 101 | style = wx.OPEN|wx.FD_MULTIPLE |
---|
[f444b20] | 102 | dlg = wx.FileDialog(self.parent, |
---|
| 103 | "Choose a file", |
---|
| 104 | self._default_save_location, "", |
---|
| 105 | wlist, |
---|
| 106 | style=style) |
---|
| 107 | if dlg.ShowModal() == wx.ID_OK: |
---|
[75fbd17] | 108 | file_list = dlg.GetPaths() |
---|
| 109 | if len(file_list) >= 0 and not(file_list[0]is None): |
---|
| 110 | self._default_save_location = os.path.dirname(file_list[0]) |
---|
[e75b5fa] | 111 | path = self._default_save_location |
---|
[f444b20] | 112 | dlg.Destroy() |
---|
[75fbd17] | 113 | |
---|
[e75b5fa] | 114 | if path is None or not file_list or file_list[0] is None: |
---|
[75fbd17] | 115 | return |
---|
| 116 | self.get_data(file_list) |
---|
[e75b5fa] | 117 | |
---|
[75fbd17] | 118 | |
---|
| 119 | def can_load_data(self): |
---|
[f444b20] | 120 | """ |
---|
[75fbd17] | 121 | if return True, then call handler to laod data |
---|
| 122 | """ |
---|
| 123 | return True |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | def _load_folder(self, event): |
---|
| 127 | """ |
---|
| 128 | Load entire folder |
---|
[f444b20] | 129 | """ |
---|
[e75b5fa] | 130 | path = None |
---|
[f444b20] | 131 | if self._default_save_location == None: |
---|
| 132 | self._default_save_location = os.getcwd() |
---|
| 133 | dlg = wx.DirDialog(self.parent, "Choose a directory", |
---|
| 134 | self._default_save_location, |
---|
| 135 | style=wx.DD_DEFAULT_STYLE) |
---|
| 136 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 137 | path = dlg.GetPath() |
---|
| 138 | self._default_save_location = path |
---|
| 139 | dlg.Destroy() |
---|
[75fbd17] | 140 | if path is not None: |
---|
| 141 | self._default_save_location = os.path.dirname(path) |
---|
| 142 | else: |
---|
| 143 | return |
---|
| 144 | file_list = self.get_file_path(path) |
---|
| 145 | self.get_data(file_list) |
---|
[e75b5fa] | 146 | |
---|
[f444b20] | 147 | def load_error(self, error=None): |
---|
| 148 | """ |
---|
| 149 | Pop up an error message. |
---|
| 150 | |
---|
| 151 | :param error: details error message to be displayed |
---|
| 152 | """ |
---|
| 153 | message = "The data file you selected could not be loaded.\n" |
---|
| 154 | message += "Make sure the content of your file" |
---|
| 155 | message += " is properly formatted.\n\n" |
---|
| 156 | |
---|
| 157 | if error is not None: |
---|
| 158 | message += "When contacting the DANSE team, mention the" |
---|
| 159 | message += " following:\n%s" % str(error) |
---|
| 160 | dial = wx.MessageDialog(self.parent, message, 'Error Loading File', |
---|
| 161 | wx.OK | wx.ICON_EXCLAMATION) |
---|
| 162 | dial.ShowModal() |
---|
| 163 | |
---|
| 164 | def get_file_path(self, path): |
---|
| 165 | """ |
---|
| 166 | Receive a list containing folder then return a list of file |
---|
| 167 | """ |
---|
| 168 | if os.path.isdir(path): |
---|
| 169 | return [os.path.join(os.path.abspath(path), |
---|
| 170 | file) for file in os.listdir(path)] |
---|
[75fbd17] | 171 | |
---|
| 172 | def get_data(self, path, format=None): |
---|
[f444b20] | 173 | """ |
---|
| 174 | """ |
---|
| 175 | message = "" |
---|
[df7046f] | 176 | log_msg = '' |
---|
[e88ebfd] | 177 | output = {} |
---|
[f444b20] | 178 | error_message = "" |
---|
| 179 | for p_file in path: |
---|
| 180 | basename = os.path.basename(p_file) |
---|
| 181 | root, extension = os.path.splitext(basename) |
---|
[75fbd17] | 182 | if extension.lower() in EXTENSIONS: |
---|
| 183 | log_msg = "Data Loader cannot " |
---|
| 184 | log_msg += "load: %s\n" % str(p_file) |
---|
| 185 | log_msg += "Try File opening ...." |
---|
| 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: |
---|
[75fbd17] | 201 | error_message = "Error while loading Data: %s\n" % str(p_file) |
---|
[f444b20] | 202 | error_message += str(sys.exc_value) + "\n" |
---|
| 203 | self.load_update(output=output, message=error_message) |
---|
| 204 | |
---|
[75fbd17] | 205 | message = "Loading Data Complete! " |
---|
[df7046f] | 206 | message += log_msg |
---|
[f444b20] | 207 | self.load_complete(output=output, error_message=error_message, |
---|
[75fbd17] | 208 | message=message, path=path) |
---|
[f444b20] | 209 | |
---|
| 210 | def load_update(self, output=None, message=""): |
---|
| 211 | """ |
---|
| 212 | print update on the status bar |
---|
| 213 | """ |
---|
| 214 | if message != "": |
---|
| 215 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 216 | type="progress", |
---|
| 217 | info="warning")) |
---|
[75fbd17] | 218 | def load_complete(self, output, message="", error_message="", path=None): |
---|
[f444b20] | 219 | """ |
---|
| 220 | post message to status bar and return list of data |
---|
| 221 | """ |
---|
| 222 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
| 223 | info="warning", |
---|
| 224 | type="stop")) |
---|
| 225 | if error_message != "": |
---|
| 226 | self.load_error(error_message) |
---|
[e88ebfd] | 227 | self.parent.add_data(data_list=output) |
---|
[75fbd17] | 228 | |
---|
| 229 | |
---|
[f444b20] | 230 | |
---|
[75fbd17] | 231 | |
---|