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