[aa749ac] | 1 | # This program is public domain |
---|
| 2 | """ |
---|
| 3 | File extension registry. |
---|
[bb03739] | 4 | |
---|
[aa749ac] | 5 | This provides routines for opening files based on extension, |
---|
| 6 | and registers the built-in file extensions. |
---|
| 7 | """ |
---|
[6d19988] | 8 | import imp,os,sys |
---|
[aa749ac] | 9 | import logging |
---|
| 10 | import os.path |
---|
[6d19988] | 11 | def _findReaders(dir): |
---|
| 12 | # List of plugin objects |
---|
| 13 | plugins = [] |
---|
| 14 | # Go through files in plug-in directory |
---|
| 15 | try: |
---|
| 16 | |
---|
| 17 | list = os.listdir(dir) |
---|
| 18 | for item in list: |
---|
| 19 | |
---|
| 20 | toks = os.path.splitext(os.path.basename(item)) |
---|
| 21 | if toks[1]=='.py' and not toks[0]=='__init__': |
---|
| 22 | name = toks[0] |
---|
| 23 | path = [os.path.abspath(dir)] |
---|
| 24 | file = None |
---|
| 25 | |
---|
| 26 | try: |
---|
| 27 | print"name",name |
---|
| 28 | print "path",path |
---|
| 29 | print imp.find_module(name, path) |
---|
| 30 | (file, path, info) = imp.find_module(name, path) |
---|
| 31 | print"file",file |
---|
| 32 | print "path", path |
---|
| 33 | print "info",info |
---|
| 34 | print"hasattr",imp.load_module( name, file, item, info ) |
---|
| 35 | module = imp.load_module( name, file, item, info ) |
---|
| 36 | print"module", module |
---|
| 37 | |
---|
| 38 | if hasattr(module, "Reader"): |
---|
| 39 | print "went here" |
---|
| 40 | try: |
---|
| 41 | plugins.append(module.Reader()) |
---|
| 42 | except: |
---|
| 43 | log("Error accessing Reader in %s\n %s" % (name, sys.exc_value)) |
---|
| 44 | except : |
---|
| 45 | print"Error importing %s\n %s" % (name,sys.exc_value) |
---|
| 46 | log("Error importing %s\n %s" % (name, sys.exc_value)) |
---|
| 47 | finally: |
---|
| 48 | if not file==None: |
---|
| 49 | file.close() |
---|
| 50 | except: |
---|
| 51 | # Should raise and catch at a higher level and display error on status bar |
---|
| 52 | pass |
---|
| 53 | return plugins |
---|
[aa749ac] | 54 | class Loader(object): |
---|
| 55 | """ |
---|
| 56 | Associate a file loader with an extension. |
---|
| 57 | |
---|
| 58 | Note that there may be multiple readers for the same extension. |
---|
| 59 | |
---|
| 60 | Example: |
---|
| 61 | |
---|
| 62 | registry = Loader() |
---|
| 63 | |
---|
| 64 | # Add an association by setting an element |
---|
| 65 | registry['.zip'] = unzip |
---|
| 66 | |
---|
| 67 | # Multiple extensions for one loader |
---|
| 68 | registry['.tgz'] = untar |
---|
| 69 | registry['.tar.gz'] = untar |
---|
| 70 | |
---|
| 71 | # Multiple readers for one extension |
---|
| 72 | registry['.cx'] = cx1 |
---|
| 73 | registry['.cx'] = cx2 |
---|
| 74 | registry['.cx'] = cx3 |
---|
| 75 | |
---|
| 76 | # Can also register a format name for explicit control from caller |
---|
| 77 | registry['cx3'] = cx3 |
---|
| 78 | |
---|
| 79 | # Retrieve readers for a file name |
---|
| 80 | registry.lookup('hello.cx') -> [cx3,cx2,cx1] |
---|
| 81 | |
---|
| 82 | # Run loader on a filename |
---|
| 83 | registry.load('hello.cx') -> |
---|
| 84 | try: |
---|
| 85 | return cx3('hello.cx') |
---|
| 86 | except: |
---|
| 87 | try: |
---|
| 88 | return cx2('hello.cx') |
---|
| 89 | except: |
---|
| 90 | return cx1('hello.cx') |
---|
| 91 | |
---|
| 92 | # Load in a specific format ignoring extension |
---|
| 93 | registry.load('hello.cx',format='cx3') -> |
---|
| 94 | return cx3('hello.cx') |
---|
| 95 | """ |
---|
[11a0319] | 96 | def __init__(self): |
---|
[aa749ac] | 97 | self.readers = {} |
---|
| 98 | self.reading=None |
---|
| 99 | |
---|
| 100 | |
---|
[6d19988] | 101 | def __setitem__(self, ext=None, reader=None): |
---|
| 102 | if reader==None: |
---|
| 103 | print os.getcwd() |
---|
| 104 | print os.path.isdir('plugins') |
---|
| 105 | print "absolute path : ",os.path.abspath('plugins') |
---|
| 106 | plugReader=None |
---|
| 107 | if os.path.isdir('plugins'): |
---|
| 108 | print "went here" |
---|
| 109 | plugReader=_findReaders('plugins')# import all module in plugins |
---|
| 110 | elif os.path.isdir('../plugins'): |
---|
| 111 | plugReader=_findReaders('../plugins') |
---|
| 112 | if plugReader !=None: |
---|
| 113 | print "this is plugreader",plugReader |
---|
| 114 | for preader in plugReader:# for each modules takes list of extensions |
---|
| 115 | #print preader.ext |
---|
| 116 | for item in preader.ext: |
---|
| 117 | ext=item |
---|
| 118 | if ext not in self.readers:#assign extension with its reader |
---|
| 119 | self.readers[ext] = [] |
---|
| 120 | self.readers[ext].insert(0,preader) |
---|
| 121 | print "extension",ext |
---|
| 122 | print "readers",self.readers |
---|
| 123 | else: |
---|
| 124 | if ext not in self.readers: |
---|
| 125 | self.readers[ext] = [] |
---|
| 126 | self.readers[ext].insert(0,reader) |
---|
[aa749ac] | 127 | |
---|
| 128 | |
---|
| 129 | def __getitem__(self, ext): |
---|
| 130 | return self.readers[ext] |
---|
[6d19988] | 131 | |
---|
[aa749ac] | 132 | |
---|
| 133 | |
---|
| 134 | def __contains__(self, ext): |
---|
| 135 | return ext in self.readers |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | def formats(self, name=True, ext=False): |
---|
| 139 | """ |
---|
| 140 | Return a list of the registered formats. If name=True then |
---|
| 141 | named formats are returned. If ext=True then extensions |
---|
| 142 | are returned. |
---|
[11a0319] | 143 | """ |
---|
[aa749ac] | 144 | names = [a for a in self.readers.keys() if not a.startswith('.')] |
---|
| 145 | exts = [a for a in self.readers.keys() if a.startswith('.')] |
---|
| 146 | names.sort() |
---|
| 147 | exts.sort() |
---|
| 148 | ret = [] |
---|
| 149 | if name: ret += names |
---|
| 150 | if ext: ret += exts |
---|
| 151 | return ret |
---|
[11a0319] | 152 | |
---|
[aa749ac] | 153 | def lookup(self, path): |
---|
| 154 | """ |
---|
| 155 | Return the loader associated with the file type of path. |
---|
| 156 | """ |
---|
| 157 | file = os.path.basename(path) |
---|
| 158 | idx = file.find('.') |
---|
| 159 | ext = file[idx:] if idx >= 0 else '' |
---|
| 160 | try: |
---|
| 161 | return self.readers[ext] |
---|
| 162 | except: |
---|
| 163 | #raise ValueError, "Unknown file type '%s'"%ext |
---|
| 164 | print "Unknown file type '%s'"%ext |
---|
| 165 | |
---|
| 166 | |
---|
| 167 | def getAcTReader(self,path): |
---|
| 168 | return self.reading |
---|
[11a0319] | 169 | |
---|
[aa749ac] | 170 | def load(self, path, format=None): |
---|
| 171 | """ |
---|
| 172 | Call the loader for the file type of path. |
---|
| 173 | |
---|
| 174 | Raises ValueError if no loader is available. |
---|
| 175 | May raise a loader-defined exception if loader fails. |
---|
| 176 | """ |
---|
| 177 | if format is None: |
---|
| 178 | readers = self.lookup(path) |
---|
| 179 | else: |
---|
| 180 | readers = self.readers[format] |
---|
| 181 | if readers!=None: |
---|
| 182 | for fn in readers: |
---|
| 183 | try: |
---|
| 184 | value=fn.read(path) |
---|
| 185 | self.reading= fn.__class__ |
---|
| 186 | return value |
---|
| 187 | except ValueError,msg: |
---|
| 188 | print str(msg) |
---|
[6d19988] | 189 | if __name__=="__main__": |
---|
| 190 | l=Loader() |
---|
| 191 | l.__setitem__() |
---|
| 192 | print "look up",l.lookup('angles_flat.png') |
---|
| 193 | print l.__getitem__('.tiff') |
---|
| 194 | print l.__contains__('.tiff') |
---|
| 195 | |
---|