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 | try: |
---|
185 | message = "Loading {}...\n".format(p_file) |
---|
186 | self.load_update(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 | |
---|
203 | self.load_update(message="Loaded {}\n".format(p_file), |
---|
204 | info="info") |
---|
205 | except NoKnownLoaderException as e: |
---|
206 | exception_occurred = True |
---|
207 | error_message = "Loading data failed!\n" + e.message |
---|
208 | file_errors[basename] = [error_message] |
---|
209 | except Exception as e: |
---|
210 | exception_occurred = True |
---|
211 | file_err = "The Data file you selected could not be " |
---|
212 | file_err += "loaded.\nMake sure the content of your file" |
---|
213 | file_err += " is properly formatted.\n" |
---|
214 | file_err += "When contacting the SasView team, mention the" |
---|
215 | file_err += " following:\n" |
---|
216 | file_err += e.message |
---|
217 | file_errors[basename] = [file_err] |
---|
218 | |
---|
219 | if len(file_errors) > 0: |
---|
220 | error_message = "" |
---|
221 | for filename, error_array in file_errors.iteritems(): |
---|
222 | error_message += "The following issues were found whilst " |
---|
223 | error_message += "loading {}:\n".format(filename) |
---|
224 | for message in error_array: |
---|
225 | error_message += message + "\n" |
---|
226 | error_message = error_message[:-1] |
---|
227 | self.load_complete(output=output, |
---|
228 | message=error_message, |
---|
229 | info="error") |
---|
230 | |
---|
231 | elif not exception_occurred: # Everything loaded as expected |
---|
232 | self.load_complete(output=output, message="Loading data complete!", |
---|
233 | info="info") |
---|
234 | |
---|
235 | def load_update(self, message="", info="warning"): |
---|
236 | """ |
---|
237 | print update on the status bar |
---|
238 | """ |
---|
239 | if message != "": |
---|
240 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
241 | info=info, |
---|
242 | type="progress")) |
---|
243 | |
---|
244 | def load_complete(self, output, message="", info="warning"): |
---|
245 | """ |
---|
246 | post message to status bar and return list of data |
---|
247 | """ |
---|
248 | wx.PostEvent(self.parent, StatusEvent(status=message, |
---|
249 | info=info, |
---|
250 | type="stop")) |
---|
251 | if output is not None: |
---|
252 | self.parent.add_data(data_list=output) |
---|