[a916ccc] | 1 | # This program is public domain |
---|
| 2 | """ |
---|
[d22da51] | 3 | @organization: Module loader contains class Loader which uses |
---|
| 4 | some readers to return values contained in a file readed. |
---|
| 5 | |
---|
[a916ccc] | 6 | """ |
---|
| 7 | import imp,os,sys |
---|
| 8 | import logging |
---|
| 9 | import os.path |
---|
[d22da51] | 10 | logging.basicConfig(level=logging.ERROR, |
---|
| 11 | format='%(asctime)s %(levelname)s %(message)s', |
---|
| 12 | filename='loader_log.txt', |
---|
| 13 | filemode='w') |
---|
| 14 | |
---|
[a916ccc] | 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: |
---|
[d22da51] | 36 | logging("Error accessing Reader in %s\n %s" % (name, sys.exc_value)) |
---|
[a916ccc] | 37 | except : |
---|
[d22da51] | 38 | logging("Error importing %s\n %s" % (name, sys.exc_value)) |
---|
[a916ccc] | 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 |
---|
[d22da51] | 46 | |
---|
| 47 | |
---|
[1b0b3ca] | 48 | class Loader(object): |
---|
[d22da51] | 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 |
---|
[a916ccc] | 63 | __load = None |
---|
[d22da51] | 64 | |
---|
| 65 | |
---|
[1b0b3ca] | 66 | class Load(object): |
---|
[a916ccc] | 67 | |
---|
| 68 | def __init__(self): |
---|
[d22da51] | 69 | #Dictionary containing readers and extension as keys |
---|
[a916ccc] | 70 | self.readers = {} |
---|
[d22da51] | 71 | #Load all readers in plugins |
---|
[1b0b3ca] | 72 | self.__setitem__() |
---|
[d22da51] | 73 | |
---|
[a916ccc] | 74 | |
---|
| 75 | def __setitem__(self, ext=None, reader=None): |
---|
[d22da51] | 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 | """ |
---|
[1b0b3ca] | 90 | import os |
---|
| 91 | if reader==None and ext==None:#1st load |
---|
[a916ccc] | 92 | plugReader=None |
---|
| 93 | if os.path.isdir('plugins'): |
---|
| 94 | plugReader=_findReaders('plugins')# import all module in plugins |
---|
[1b0b3ca] | 95 | if os.path.isdir('../plugins'): |
---|
[a916ccc] | 96 | plugReader=_findReaders('../plugins') |
---|
[1b0b3ca] | 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 | |
---|
[a916ccc] | 102 | if plugReader !=None: |
---|
| 103 | for preader in plugReader:# for each modules takes list of extensions |
---|
[1b0b3ca] | 104 | print preader,preader.ext |
---|
[a916ccc] | 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) |
---|
[d22da51] | 110 | #Reader and extension are given |
---|
[a916ccc] | 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: |
---|
[d22da51] | 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" |
---|
[a916ccc] | 125 | else: |
---|
| 126 | raise ValueError,"missing reader" |
---|
[d22da51] | 127 | |
---|
[a916ccc] | 128 | |
---|
| 129 | def __getitem__(self, ext): |
---|
[d22da51] | 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 | """ |
---|
[a916ccc] | 136 | return self.readers[ext] |
---|
| 137 | |
---|
| 138 | def __contains__(self, ext): |
---|
[d22da51] | 139 | """ |
---|
| 140 | @param ext:file extension |
---|
| 141 | @return: True or False whether there is a reader file with that extension |
---|
| 142 | """ |
---|
[a916ccc] | 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 '' |
---|
[d22da51] | 168 | |
---|
[a916ccc] | 169 | try: |
---|
| 170 | return self.readers[ext] |
---|
| 171 | except: |
---|
[d22da51] | 172 | |
---|
| 173 | raise ValueError, "Unknown file type '%s'"%ext |
---|
| 174 | |
---|
| 175 | |
---|
[a916ccc] | 176 | |
---|
| 177 | def load(self, path, format=None): |
---|
| 178 | """ |
---|
[d22da51] | 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. |
---|
[a916ccc] | 186 | """ |
---|
[1b0b3ca] | 187 | try: |
---|
| 188 | os.path.isfile( os.path.abspath(path)) |
---|
| 189 | except: |
---|
[d22da51] | 190 | raise ValueError," file path unknown" |
---|
| 191 | |
---|
[a916ccc] | 192 | if format is None: |
---|
[d22da51] | 193 | try: |
---|
| 194 | readers = self.lookup(path) |
---|
| 195 | except ValueError,msg: |
---|
| 196 | raise ValueError,str(msg) |
---|
[a916ccc] | 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: |
---|
[d22da51] | 205 | logging.error(str(msg)) |
---|
| 206 | else: |
---|
| 207 | raise ValueError,"Loader contains no reader" |
---|
| 208 | |
---|
| 209 | |
---|
[a916ccc] | 210 | def __init__(self): |
---|
| 211 | """ Create singleton instance """ |
---|
| 212 | # Check whether we already have an instance |
---|
[1b0b3ca] | 213 | if Loader.__load is None: |
---|
[a916ccc] | 214 | # Create and remember instance |
---|
[1b0b3ca] | 215 | Loader.__load = Loader.Load() |
---|
| 216 | Loader.__load.__setitem__() |
---|
[a916ccc] | 217 | # Store instance reference as the only member in the handle |
---|
[1b0b3ca] | 218 | self.__dict__['_Loader__load'] = Loader.__load |
---|
[a916ccc] | 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) |
---|