source: sasview/guiframe/local_perspectives/data_loader/data_loader.py @ c83a5af

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 c83a5af was 75fbd17, checked in by Gervaise Alina <gervyh@…>, 14 years ago

working on save state

  • Property mode set to 100644
File size: 7.1 KB
Line 
1
2"""
3plugin DataLoader responsible of loading data
4"""
5import os
6import sys
7import wx
8import logging
9
10from DataLoader.loader import Loader
11import 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
19
20EXTENSIONS = ['.svs', '.prv','.fitv', '.inv']
21
22class 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.data_name_dict = {}
29        self.loader = Loader() 
30        self._data_menu = None 
31       
32    def populate_menu(self, parent):
33        """
34        """
35         # Add menu data
36        self._data_menu = wx.Menu()
37        #menu for data files
38        data_file_id = wx.NewId()
39        data_file_hint = "load one or more data in the application"
40        self._data_menu.Append(data_file_id, 
41                         '&Load Data File(s)', data_file_hint)
42        wx.EVT_MENU(self.parent, data_file_id, self._load_data)
43        gui_style = self.parent.get_style()
44        style = gui_style & GUIFRAME.MULTIPLE_APPLICATIONS
45        style1 = gui_style & GUIFRAME.DATALOADER_ON
46        if style == GUIFRAME.MULTIPLE_APPLICATIONS:
47            #menu for data from folder
48            data_folder_id = wx.NewId()
49            data_folder_hint = "load multiple data in the application"
50            self._data_menu.Append(data_folder_id, 
51                             '&Load Data Folder', data_folder_hint)
52            wx.EVT_MENU(self.parent, data_folder_id, self._load_folder)
53           
54        return [(self._data_menu, 'Data')]
55
56    def _load_data(self, event):
57        """
58        Load data
59        """
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        dlg.Destroy()
76       
77        if not file_list or file_list[0] is None:
78            return
79        self.get_data(file_list)
80        self.parent.show_data_panel(event=None)
81       
82    def can_load_data(self):
83        """
84        if return True, then call handler to laod data
85        """
86        return True
87 
88       
89    def _load_folder(self, event):
90        """
91        Load entire folder
92        """
93        if self._default_save_location == None:
94            self._default_save_location = os.getcwd()
95        dlg = wx.DirDialog(self.parent, "Choose a directory", 
96                           self._default_save_location,
97                            style=wx.DD_DEFAULT_STYLE)
98        if dlg.ShowModal() == wx.ID_OK:
99            path = dlg.GetPath()
100            self._default_save_location = path
101        dlg.Destroy()
102        if path is not None:
103            self._default_save_location = os.path.dirname(path)
104        else:
105            return   
106        file_list = self.get_file_path(path)
107        self.get_data(file_list)
108        self.parent.show_data_panel(event=None)
109   
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.append(data)
159                else:
160                    data = self.parent.create_gui_data(temp, p_file)
161                    output.append(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(output)
192   
193   
194       
195   
Note: See TracBrowser for help on using the repository browser.