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 | """ |
---|
7 | import imp,os,sys |
---|
8 | import logging |
---|
9 | import os.path |
---|
10 | logging.basicConfig(level=logging.ERROR, |
---|
11 | format='%(asctime)s %(levelname)s %(message)s', |
---|
12 | filename='loader_log.txt', |
---|
13 | filemode='w') |
---|
14 | |
---|
15 | def _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("Error accessing Reader in %s\n %s" % (name, sys.exc_value)) |
---|
37 | except : |
---|
38 | logging.error("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 | |
---|
48 | class 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,dir=None, 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 | @param dir: directory name where plugins readers will be saved |
---|
82 | @raise : ValueError will be raise if a "plugins" directory is not found |
---|
83 | and the user didn't add a reader as parameter or if the user didn't |
---|
84 | add a reader as a parameter and plugins directory doesn't contain |
---|
85 | plugin reader. |
---|
86 | if an extension is not specified and a reader does not contain a field |
---|
87 | ext , a ValueError "missing extension" is raised. |
---|
88 | @note: when called without parameters __setitem__ will try to load |
---|
89 | readers inside a "readers" directory |
---|
90 | if call with a directory name will try find readers |
---|
91 | from that directory "dir" |
---|
92 | """ |
---|
93 | if dir==None: |
---|
94 | dir='readers' |
---|
95 | |
---|
96 | if reader==None and ext==None:#1st load |
---|
97 | plugReader=None |
---|
98 | if os.path.isdir(dir): |
---|
99 | plugReader=_findReaders(dir)# import all module in plugins |
---|
100 | if os.path.isdir('../'+dir): |
---|
101 | plugReader=_findReaders('../'+dir) |
---|
102 | else: |
---|
103 | if os.path.isdir('../DataLoader/'+dir): |
---|
104 | os.chdir(os.path.abspath('../DataLoader/'+dir))# change the current |
---|
105 | plugReader=_findReaders(dir) |
---|
106 | |
---|
107 | if plugReader !=None: |
---|
108 | for preader in plugReader:# for each modules takes list of extensions |
---|
109 | try: |
---|
110 | list=preader.ext |
---|
111 | except: |
---|
112 | raise AttributeError," %s instance has no attribute 'ext'"\ |
---|
113 | %(preader.__class__) |
---|
114 | for item in list: |
---|
115 | ext=item |
---|
116 | if ext not in self.readers:#assign extension with its reader |
---|
117 | self.readers[ext] = [] |
---|
118 | self.readers[ext].insert(0,preader) |
---|
119 | #Reader and extension are given |
---|
120 | elif reader !=None and ext !=None: |
---|
121 | if ext not in self.readers: |
---|
122 | self.readers[ext] = [] |
---|
123 | self.readers[ext].insert(0,reader) |
---|
124 | elif reader!=None: |
---|
125 | #only reader is receive try to find a field ext |
---|
126 | try: |
---|
127 | list=preader.ext |
---|
128 | except: |
---|
129 | raise AttributeError," Reader instance has no attribute 'ext'" |
---|
130 | for item in list: |
---|
131 | |
---|
132 | ext=item |
---|
133 | if ext not in self.readers:#assign extension with its reader |
---|
134 | self.readers[ext] = [] |
---|
135 | self.readers[ext].insert(0,reader) |
---|
136 | |
---|
137 | else: |
---|
138 | raise ValueError,"missing reader" |
---|
139 | |
---|
140 | |
---|
141 | def __getitem__(self, ext): |
---|
142 | """ |
---|
143 | __getitem__ get a list of readers that can read a file with that extension |
---|
144 | @param ext: file extension |
---|
145 | @return self.readers[ext]:list of readers that can read a file |
---|
146 | with that extension |
---|
147 | """ |
---|
148 | return self.readers[ext] |
---|
149 | |
---|
150 | def __contains__(self, ext): |
---|
151 | """ |
---|
152 | @param ext:file extension |
---|
153 | @return: True or False whether there is a reader file with that extension |
---|
154 | """ |
---|
155 | return ext in self.readers |
---|
156 | |
---|
157 | |
---|
158 | def formats(self, name=True, ext=False): |
---|
159 | """ |
---|
160 | Return a list of the registered formats. If name=True then |
---|
161 | named formats are returned. If ext=True then extensions |
---|
162 | are returned. |
---|
163 | """ |
---|
164 | names = [a for a in self.readers.keys() if not a.startswith('.')] |
---|
165 | exts = [a for a in self.readers.keys() if a.startswith('.')] |
---|
166 | names.sort() |
---|
167 | exts.sort() |
---|
168 | ret = [] |
---|
169 | if name: ret += names |
---|
170 | if ext: ret += exts |
---|
171 | return ret |
---|
172 | |
---|
173 | def lookup(self, path): |
---|
174 | """ |
---|
175 | Return the loader associated with the file type of path. |
---|
176 | """ |
---|
177 | file = os.path.basename(path) |
---|
178 | idx = file.find('.') |
---|
179 | ext = file[idx:] if idx >= 0 else '' |
---|
180 | |
---|
181 | try: |
---|
182 | return self.readers[ext] |
---|
183 | except: |
---|
184 | |
---|
185 | raise RuntimeError, "Unknown file type '%s'"%ext |
---|
186 | |
---|
187 | |
---|
188 | |
---|
189 | def load(self, path, format=None): |
---|
190 | """ |
---|
191 | Call reader for the file type of path. |
---|
192 | @param path: path to file to load |
---|
193 | @param format: extension of file to load |
---|
194 | @return Data if sucessful |
---|
195 | or None is not reader was able to read that file |
---|
196 | Raises ValueError if no reader is available. |
---|
197 | May raise a loader-defined exception if loader fails. |
---|
198 | """ |
---|
199 | try: |
---|
200 | os.path.isfile( os.path.abspath(path)) |
---|
201 | except: |
---|
202 | raise ValueError," file path unknown" |
---|
203 | |
---|
204 | if format is None: |
---|
205 | try: |
---|
206 | readers = self.lookup(path) |
---|
207 | except ValueError,msg: |
---|
208 | raise ValueError,str(msg) |
---|
209 | else: |
---|
210 | readers = self.readers[format] |
---|
211 | if readers!=None: |
---|
212 | for fn in readers: |
---|
213 | try: |
---|
214 | value=fn.read(path) |
---|
215 | return value |
---|
216 | except ValueError,msg: |
---|
217 | logging.error(str(msg)) |
---|
218 | else: |
---|
219 | raise ValueError,"Loader contains no reader" |
---|
220 | |
---|
221 | |
---|
222 | def __init__(self): |
---|
223 | """ Create singleton instance """ |
---|
224 | # Check whether we already have an instance |
---|
225 | if Loader.__load is None: |
---|
226 | # Create and remember instance |
---|
227 | Loader.__load = Loader.Load() |
---|
228 | Loader.__load.__setitem__() |
---|
229 | # Store instance reference as the only member in the handle |
---|
230 | self.__dict__['_Loader__load'] = Loader.__load |
---|
231 | |
---|
232 | def __getattr__(self, attr): |
---|
233 | """ Delegate access to implementation """ |
---|
234 | return getattr(self.__load, attr) |
---|
235 | |
---|
236 | def __setattr__(self, attr, value): |
---|
237 | """ Delegate access to implementation """ |
---|
238 | return setattr(self.__load, attr, value) |
---|