source: sasview/sansguiframe/src/sans/guiframe/local_perspectives/data_loader/data_loader.py @ 805e2e7

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 805e2e7 was 805e2e7, checked in by Gervaise Alina <gervyh@…>, 13 years ago

edit icon for help menu

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