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