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