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