source: sasview/sansguiframe/src/sans/guiframe/local_perspectives/data_loader/data_loader.py @ 6a7cf2c

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 6a7cf2c was c553b18, checked in by Jae Cho <jhjcho@…>, 13 years ago

remember the last folder opened/saved

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