1 | |
---|
2 | """ |
---|
3 | plugin DataLoader responsible of loading data |
---|
4 | """ |
---|
5 | import os |
---|
6 | import sys |
---|
7 | import wx |
---|
8 | import logging |
---|
9 | |
---|
10 | logger = logging.getLogger(__name__) |
---|
11 | |
---|
12 | from sas import get_local_config |
---|
13 | |
---|
14 | from sas.sascalc.dataloader.loader import Loader |
---|
15 | from sas.sascalc.dataloader.loader_exceptions import NoKnownLoaderException |
---|
16 | |
---|
17 | from sas.sasgui.guiframe.plugin_base import PluginBase |
---|
18 | from sas.sasgui.guiframe.events import StatusEvent |
---|
19 | from sas.sasgui.guiframe.gui_style import GUIFRAME |
---|
20 | from sas.sasgui.guiframe.gui_manager import DEFAULT_OPEN_FOLDER |
---|
21 | |
---|
22 | config = get_local_config() |
---|
23 | extension_list = [] |
---|
24 | if config.APPLICATION_STATE_EXTENSION is not None: |
---|
25 | extension_list.append(config.APPLICATION_STATE_EXTENSION) |
---|
26 | EXTENSIONS = config.PLUGIN_STATE_EXTENSIONS + extension_list |
---|
27 | PLUGINS_WLIST = config.PLUGINS_WLIST |
---|
28 | APPLICATION_WLIST = config.APPLICATION_WLIST |
---|
29 | |
---|
30 | |
---|
31 | class Plugin(PluginBase): |
---|
32 | |
---|
33 | def __init__(self): |
---|
34 | PluginBase.__init__(self, name="DataLoader") |
---|
35 | # Default location |
---|
36 | self._default_save_location = DEFAULT_OPEN_FOLDER |
---|
37 | self.loader = Loader() |
---|
38 | self._data_menu = None |
---|
39 | |
---|
40 | def populate_file_menu(self): |
---|
41 | """ |
---|
42 | get a menu item and append it under file menu of the application |
---|
43 | add load file menu item and load folder item |
---|
44 | """ |
---|
45 | # menu for data files |
---|
46 | data_file_hint = "load one or more data in the application" |
---|
47 | menu_list = [('&Load Data File(s)', data_file_hint, self.load_data)] |
---|
48 | gui_style = self.parent.get_style() |
---|
49 | style = gui_style & GUIFRAME.MULTIPLE_APPLICATIONS |
---|
50 | if style == GUIFRAME.MULTIPLE_APPLICATIONS: |
---|
51 | # menu for data from folder |
---|
52 | data_folder_hint = "load multiple data in the application" |
---|
53 | menu_list.append(('&Load Data Folder', data_folder_hint, |
---|
54 | self._load_folder)) |
---|
55 | return menu_list |
---|
56 | |
---|
57 | def load_data(self, event): |
---|
58 | """ |
---|
59 | Load data |
---|
60 | """ |
---|
61 | path = None |
---|
62 | self._default_save_location = self.parent._default_save_location |
---|
63 | if self._default_save_location is None: |
---|
64 | self._default_save_location = os.getcwd() |
---|
65 | |
---|
66 | cards = self.loader.get_wildcards() |
---|
67 | temp = [APPLICATION_WLIST] + PLUGINS_WLIST |
---|
68 | for item in temp: |
---|
69 | if item in cards: |
---|
70 | cards.remove(item) |
---|
71 | wlist = '|'.join(cards) |
---|
72 | style = wx.OPEN | wx.FD_MULTIPLE |
---|
73 | dlg = wx.FileDialog(self.parent, |
---|
74 | "Choose a file", |
---|
75 | self._default_save_location, "", |
---|
76 | wlist, |
---|
77 | style=style) |
---|
78 | if dlg.ShowModal() == wx.ID_OK: |
---|
79 | file_list = dlg.GetPaths() |
---|
80 | if len(file_list) >= 0 and file_list[0] is not None: |
---|
81 | self._default_save_location = os.path.dirname(file_list[0]) |
---|
82 | path = self._default_save_location |
---|
83 | dlg.Destroy() |
---|
84 | |
---|
85 | if path is None or not file_list or file_list[0] is None: |
---|
86 | return |
---|
87 | self.parent._default_save_location = self._default_save_location |
---|
88 | self.get_data(file_list) |
---|
89 | |
---|
90 | def can_load_data(self): |
---|
91 | """ |
---|
92 | if return True, then call handler to load data |
---|
93 | """ |
---|
94 | return True |
---|
95 | |
---|
96 | def _load_folder(self, event): |
---|
97 | """ |
---|
98 | Load entire folder |
---|
99 | """ |
---|
100 | path = None |
---|
101 | self._default_save_location = self.parent._default_save_location |
---|
102 | if self._default_save_location is None: |
---|
103 | self._default_save_location = os.getcwd() |
---|
104 | dlg = wx.DirDialog(self.parent, "Choose a directory", |
---|
105 | self._default_save_location, |
---|
106 | style=wx.DD_DEFAULT_STYLE) |
---|
107 | if dlg.ShowModal() == wx.ID_OK: |
---|
108 | path = dlg.GetPath() |
---|
109 | self._default_save_location = path |
---|
110 | dlg.Destroy() |
---|
111 | if path is not None: |
---|
112 | self._default_save_location = os.path.dirname(path) |
---|
113 | else: |
---|
114 | return |
---|
115 | file_list = self.get_file_path(path) |
---|
116 | self.get_data(file_list) |
---|
117 | self.parent._default_save_location = self._default_save_location |
---|
118 | |
---|
119 | def load_error(self, error=None): |
---|
120 | """ |
---|
121 | Pop up an error message. |
---|
122 | |
---|
123 | :param error: details error message to be displayed |
---|
124 | """ |
---|
125 | if error is not None or str(error).strip() != "": |
---|
126 | dial = wx.MessageDialog(self.parent, str(error), |
---|
127 | 'Error Loading File', |
---|
128 | wx.OK | wx.ICON_EXCLAMATION) |
---|
129 | dial.ShowModal() |
---|
130 | |
---|
131 | def get_file_path(self, path): |
---|
132 | """ |
---|
133 | Receive a list containing folder then return a list of file |
---|
134 | """ |
---|
135 | if os.path.isdir(path): |
---|
136 | return [os.path.join(os.path.abspath(path), filename) for filename |
---|
137 | in os.listdir(path)] |
---|
138 | |
---|
139 | def _process_data_and_errors(self, item, p_file, output, message): |
---|
140 | """ |
---|
141 | Check to see if data set loaded with any errors. If so, append to |
---|
142 | error message to be sure user knows the issue. |
---|
143 | """ |
---|
144 | data_error = False |
---|
145 | if hasattr(item, 'errors'): |
---|
146 | for error_data in item.errors: |
---|
147 | data_error = True |
---|
148 | message += "\tError: {0}\n".format(error_data) |
---|
149 | else: |
---|
150 | logger.error("Loader returned an invalid object:\n %s" % str(item)) |
---|
151 | data_error = True |
---|
152 | |
---|
153 | data = self.parent.create_gui_data(item, p_file) |
---|
154 | output[data.id] = data |
---|
155 | return output, message, data_error |
---|
156 | |
---|
157 | def get_data(self, path, format=None): |
---|
158 | """ |
---|
159 | """ |
---|
160 | file_errors = {} |
---|
161 | output = {} |
---|
162 | exception_occurred = False |
---|
163 | |
---|
164 | for p_file in path: |
---|
165 | basename = os.path.basename(p_file) |
---|
166 | # Skip files that start with a period |
---|
167 | if basename.startswith("."): |
---|
168 | msg = "The folder included a potential hidden file - %s." \ |
---|
169 | % basename |
---|
170 | msg += " Do you wish to load this file as data?" |
---|
171 | msg_box = wx.MessageDialog(None, msg, 'Warning', |
---|
172 | wx.OK | wx.CANCEL) |
---|
173 | if msg_box.ShowModal() == wx.ID_CANCEL: |
---|
174 | continue |
---|
175 | _, extension = os.path.splitext(basename) |
---|
176 | if extension.lower() in EXTENSIONS: |
---|
177 | log_msg = "Data Loader cannot " |
---|
178 | log_msg += "load: {}\n".format(str(p_file)) |
---|
179 | log_msg += "Please try to open that file from \"open project\"" |
---|
180 | log_msg += "or \"open analysis\" menu." |
---|
181 | logger.info(log_msg) |
---|
182 | file_errors[basename] = [log_msg] |
---|
183 | continue |
---|
184 | |
---|
185 | try: |
---|
186 | message = "Loading {}...\n".format(p_file) |
---|
187 | self.load_update(message=message, info="info") |
---|
188 | temp = self.loader.load(p_file, format) |
---|
189 | if not isinstance(temp, list): |
---|
190 | temp = [temp] |
---|
191 | for item in temp: |
---|
192 | error_message = "" |
---|
193 | output, error_message, data_error = \ |
---|
194 | self._process_data_and_errors(item, |
---|
195 | p_file, |
---|
196 | output, |
---|
197 | error_message) |
---|
198 | if data_error: |
---|
199 | if basename in file_errors.keys(): |
---|
200 | file_errors[basename] += [error_message] |
---|
201 | else: |
---|
202 | file_errors[basename] = [error_message] |
---|
203 | |
---|
204 | self.load_update(message="Loaded {}\n".format(p_file), |
---|
205 | info="info") |
---|
206 | |
---|
207 | except NoKnownLoaderException as e: |
---|
208 | exception_occurred = True |
---|
209 | error_message = "Loading data failed!\n" + e.message |
---|
210 | self.load_complete(output=None, |
---|
211 | message=error_message, |
---|
212 | info="warning") |
---|
213 | |
---|
214 | except Exception as e: |
---|
215 | exception_occurred = True |
---|
216 | file_err = "The Data file you selected could not be " |
---|
217 | file_err += "loaded.\nMake sure the content of your file" |
---|
218 | file_err += " is properly formatted.\n" |
---|
219 | file_err += "When contacting the SasView team, mention the" |
---|
220 | file_err += " following:\n" |
---|
221 | file_err += e.message |
---|
222 | self.load_complete(output=None, |
---|
223 | message=file_err, |
---|
224 | info="error") |
---|
225 | |
---|
226 | if len(file_errors) > 0: |
---|
227 | error_message = "" |
---|
228 | for filename, error_array in file_errors.iteritems(): |
---|
229 | error_message += "The following issues were found whilst " |
---|
230 | error_message += "loading {}:\n".format(filename) |
---|
231 | for message in error_array: |
---|
232 | error_message += message + "\n" |
---|
233 | error_message = error_message[:-1] |
---|
234 | self.load_complete(output=output, |
---|
235 | message=error_message, |
---|
236 | info="error") |
---|
237 | |
---|
238 | elif not exception_occurred: # Everything loaded as expected |
---|
239 | self.load_complete(output=output, message="Loading data complete!", |
---|
240 | info="info") |
---|
241 | else: |
---|
242 | self.load_complete(output=None, message=error_message, info="error") |
---|
243 | |
---|
244 | def load_update(self, message="", info="warning"): |
---|
245 | """ |
---|
246 | print update on the status bar |
---|
247 | """ |
---|
248 | if message != "": |
---|
249 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
250 | info=info, |
---|
251 | type="progress")) |
---|
252 | |
---|
253 | def load_complete(self, output, message="", info="warning"): |
---|
254 | """ |
---|
255 | post message to status bar and return list of data |
---|
256 | """ |
---|
257 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
258 | info=info, |
---|
259 | type="stop")) |
---|
260 | if output is not None: |
---|
261 | self.parent.add_data(data_list=output) |
---|