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