source: sasview/guiframe/local_perspectives/data_loader/data_loader.py @ 7a8faf8

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 7a8faf8 was e26d0db, checked in by Gervaise Alina <gervyh@…>, 13 years ago

woprking on removing data from panel

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