source: sasview/src/sas/guiframe/local_perspectives/data_loader/data_loader.py @ 7a04dbb

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 7a04dbb was 7a04dbb, checked in by krzywon, 9 years ago

Removed the last vestiges of the SANS references and ran run.py. Tested
loading multiple file types and ran fits, p(r) and inversion. Help
windows loaded properly. Saving and loading projects and analyses acted
the same as before the conversion.

All unit tests run by utest_sasview.py passed.

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