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