source: sasview/DataLoader/loader.py @ d22da51

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

clean and more testing added

  • Property mode set to 100644
File size: 8.9 KB
Line 
1# This program is public domain
2"""
3    @organization: Module loader contains class Loader which uses
4    some readers to return values contained in a file readed.
5   
6"""
7import imp,os,sys
8import logging
9import os.path
10logging.basicConfig(level=logging.ERROR,
11                    format='%(asctime)s %(levelname)s %(message)s',
12                    filename='loader_log.txt',
13                    filemode='w')
14
15def _findReaders(dir):
16    # List of plugin objects
17    plugins = []
18    # Go through files in plug-in directory
19    try:
20       
21        list = os.listdir(dir)
22        for item in list:
23           
24            toks = os.path.splitext(os.path.basename(item))
25            if toks[1]=='.py' and not toks[0]=='__init__':
26                name = toks[0]
27                path = [os.path.abspath(dir)]
28                file = None
29                try:
30                    (file, path, info) = imp.find_module(name, path)
31                    module = imp.load_module( name, file, item, info )
32                    if hasattr(module, "Reader"):
33                        try:
34                            plugins.append(module.Reader())
35                        except:
36                            logging("Error accessing Reader in %s\n  %s" % (name, sys.exc_value))
37                except :
38                    logging("Error importing %s\n  %s" % (name, sys.exc_value))
39                finally:
40                    if not file==None:
41                        file.close()
42    except:
43        # Should raise and catch at a higher level and display error on status bar
44        pass   
45    return plugins
46
47
48class Loader(object):
49    """
50        Loader class extracts data from a given file.
51        This provides routines for opening files based on extension,
52        and readers built-in file extensions.
53        It uses functionalities for class Load
54        @note: For loader to operate properly each readers used should
55        contain a class name "Reader" that contains a field call ext.
56        Can be used as follow:
57        L=Loader()
58        self.assertEqual(l.__contains__('.tiff'),True)
59        #Recieves data
60        data=L.load(path)
61    """
62    #Store instance of class Load
63    __load = None
64   
65   
66    class Load(object):
67   
68        def __init__(self):
69            #Dictionary containing readers and extension as keys
70            self.readers = {}
71            #Load all readers in plugins
72            self.__setitem__()
73           
74           
75        def __setitem__(self, ext=None, reader=None):
76            """
77                __setitem__  sets in a dictionary(self.readers) a given reader
78                with a file extension that it can read.
79                @param ext: extension given of type string
80                @param reader:instance Reader class
81                @raise : ValueError will be raise if a "plugins" directory is not found
82                and the user didn't add a reader as parameter or if the user didn't
83                add a reader as a parameter and plugins directory doesn't contain
84                plugin reader.
85                if an extension is not specified and a reader does not contain a field
86                ext , a ValueError "missing extension" is raised.
87                @note: when called without parameters __setitem__ will try to load
88                readers inside a "plugins" directory
89            """
90            import os
91            if reader==None and  ext==None:#1st load
92                plugReader=None
93                if os.path.isdir('plugins'):
94                    plugReader=_findReaders('plugins')# import all module in plugins
95                if os.path.isdir('../plugins'):
96                    plugReader=_findReaders('../plugins')
97                else:
98                    if os.path.isdir('..\DataLoader\plugins'):
99                        os.chdir(os.path.abspath('..\DataLoader\plugins'))# change the current
100                        plugReader=_findReaders('plugins')
101                       
102                if plugReader !=None:
103                    for preader in plugReader:# for each modules takes list of extensions
104                        print preader,preader.ext
105                        for item in preader.ext:
106                            ext=item
107                            if ext not in self.readers:#assign extension with its reader
108                                self.readers[ext] = []
109                            self.readers[ext].insert(0,preader)
110            #Reader and extension are given
111            elif reader !=None and  ext !=None:
112                if ext not in self.readers:
113                    self.readers[ext] = []
114                self.readers[ext].insert(0,reader)
115            elif reader!=None:
116                #only reader is receive try to find a field ext
117                if reader.ext:
118                    for item in reader.ext:
119                        ext=item
120                        if ext not in self.readers:#assign extension with its reader
121                            self.readers[ext] = []
122                        self.readers[ext].insert(0,reader)
123                else:
124                    raise ValueError,"missing extension"
125            else:
126                raise ValueError,"missing reader"
127               
128           
129        def __getitem__(self, ext):
130            """
131                __getitem__ get a list of readers that can read a file with that extension
132                @param ext: file extension
133                @return self.readers[ext]:list of readers that can read a file
134                with that extension
135            """
136            return self.readers[ext]
137           
138        def __contains__(self, ext):
139            """
140                @param ext:file extension
141                @return: True or False whether there is a reader file with that extension
142            """
143            return ext in self.readers
144       
145       
146        def formats(self, name=True, ext=False):
147            """
148            Return a list of the registered formats.  If name=True then
149            named formats are returned.  If ext=True then extensions
150            are returned.
151            """
152            names = [a for a in self.readers.keys() if not a.startswith('.')]
153            exts = [a for a in self.readers.keys() if a.startswith('.')]
154            names.sort()
155            exts.sort()
156            ret = []
157            if name: ret += names
158            if ext: ret += exts
159            return ret
160           
161        def lookup(self, path):
162            """
163            Return the loader associated with the file type of path.
164            """       
165            file = os.path.basename(path)
166            idx = file.find('.')
167            ext = file[idx:] if idx >= 0 else ''
168           
169            try:
170                return self.readers[ext]
171            except:
172               
173                raise ValueError, "Unknown file type '%s'"%ext
174               
175       
176       
177        def load(self, path, format=None):
178            """
179                Call reader for the file type of path.
180                @param path: path to file to load
181                @param format: extension of file to load
182                @return Data if sucessful
183                  or None is not reader was able to read that file
184                Raises ValueError if no reader is available.
185                May raise a loader-defined exception if loader fails.
186            """
187            try:
188                os.path.isfile( os.path.abspath(path)) 
189            except:
190                raise ValueError," file  path unknown"
191           
192            if format is None:
193                try:
194                    readers = self.lookup(path)
195                except ValueError,msg:
196                    raise ValueError,str(msg)
197            else:
198                readers = self.readers[format]
199            if readers!=None:
200                for fn in readers:
201                    try:
202                        value=fn.read(path)
203                        return value
204                    except ValueError,msg:
205                        logging.error(str(msg))
206            else:
207                raise ValueError,"Loader contains no reader"
208                       
209                         
210    def __init__(self):
211        """ Create singleton instance """
212        # Check whether we already have an instance
213        if Loader.__load is None:
214            # Create and remember instance
215            Loader.__load = Loader.Load()
216            Loader.__load.__setitem__()
217        # Store instance reference as the only member in the handle
218        self.__dict__['_Loader__load'] = Loader.__load
219
220    def __getattr__(self, attr):
221        """ Delegate access to implementation """
222        return getattr(self.__load, attr)
223
224    def __setattr__(self, attr, value):
225        """ Delegate access to implementation """
226        return setattr(self.__load, attr, value)
Note: See TracBrowser for help on using the repository browser.