1 | # This program is public domain |
---|
2 | """ |
---|
3 | File extension registry. |
---|
4 | |
---|
5 | This provides routines for opening files based on extension, |
---|
6 | and registers the built-in file extensions. |
---|
7 | """ |
---|
8 | import imp,os,sys |
---|
9 | import logging |
---|
10 | import os.path |
---|
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 | (file, path, info) = imp.find_module(name, path) |
---|
28 | module = imp.load_module( name, file, item, info ) |
---|
29 | if hasattr(module, "Reader"): |
---|
30 | try: |
---|
31 | plugins.append(module.Reader()) |
---|
32 | except: |
---|
33 | log("Error accessing Reader in %s\n %s" % (name, sys.exc_value)) |
---|
34 | except : |
---|
35 | log("Error importing %s\n %s" % (name, sys.exc_value)) |
---|
36 | finally: |
---|
37 | if not file==None: |
---|
38 | file.close() |
---|
39 | except: |
---|
40 | # Should raise and catch at a higher level and display error on status bar |
---|
41 | pass |
---|
42 | return plugins |
---|
43 | class SingleLoader(object): |
---|
44 | |
---|
45 | __load = None |
---|
46 | print "hello" |
---|
47 | class Loader(object): |
---|
48 | |
---|
49 | def __init__(self): |
---|
50 | self.readers = {} |
---|
51 | self.reading=None |
---|
52 | print "in loader" |
---|
53 | |
---|
54 | def __setitem__(self, ext=None, reader=None): |
---|
55 | if reader==None and ext==None: |
---|
56 | plugReader=None |
---|
57 | if os.path.isdir('plugins'): |
---|
58 | plugReader=_findReaders('plugins')# import all module in plugins |
---|
59 | elif os.path.isdir('../plugins'): |
---|
60 | plugReader=_findReaders('../plugins') |
---|
61 | if plugReader !=None: |
---|
62 | for preader in plugReader:# for each modules takes list of extensions |
---|
63 | #print preader.ext |
---|
64 | for item in preader.ext: |
---|
65 | ext=item |
---|
66 | if ext not in self.readers:#assign extension with its reader |
---|
67 | self.readers[ext] = [] |
---|
68 | self.readers[ext].insert(0,preader) |
---|
69 | elif reader !=None and ext !=None: |
---|
70 | if ext not in self.readers: |
---|
71 | self.readers[ext] = [] |
---|
72 | self.readers[ext].insert(0,reader) |
---|
73 | elif reader!=None: |
---|
74 | for item in reader.ext: |
---|
75 | ext=item |
---|
76 | if ext not in self.readers:#assign extension with its reader |
---|
77 | self.readers[ext] = [] |
---|
78 | self.readers[ext].insert(0,reader) |
---|
79 | else: |
---|
80 | raise ValueError,"missing reader" |
---|
81 | |
---|
82 | |
---|
83 | def __getitem__(self, ext): |
---|
84 | return self.readers[ext] |
---|
85 | |
---|
86 | def __contains__(self, ext): |
---|
87 | return ext in self.readers |
---|
88 | |
---|
89 | |
---|
90 | def formats(self, name=True, ext=False): |
---|
91 | """ |
---|
92 | Return a list of the registered formats. If name=True then |
---|
93 | named formats are returned. If ext=True then extensions |
---|
94 | are returned. |
---|
95 | """ |
---|
96 | names = [a for a in self.readers.keys() if not a.startswith('.')] |
---|
97 | exts = [a for a in self.readers.keys() if a.startswith('.')] |
---|
98 | names.sort() |
---|
99 | exts.sort() |
---|
100 | ret = [] |
---|
101 | if name: ret += names |
---|
102 | if ext: ret += exts |
---|
103 | return ret |
---|
104 | |
---|
105 | def lookup(self, path): |
---|
106 | """ |
---|
107 | Return the loader associated with the file type of path. |
---|
108 | """ |
---|
109 | file = os.path.basename(path) |
---|
110 | idx = file.find('.') |
---|
111 | ext = file[idx:] if idx >= 0 else '' |
---|
112 | try: |
---|
113 | return self.readers[ext] |
---|
114 | except: |
---|
115 | #raise ValueError, "Unknown file type '%s'"%ext |
---|
116 | print "Unknown file type '%s'"%ext |
---|
117 | |
---|
118 | |
---|
119 | def getAcTReader(self,path): |
---|
120 | return self.reading |
---|
121 | |
---|
122 | def load(self, path, format=None): |
---|
123 | """ |
---|
124 | Call the loader for the file type of path. |
---|
125 | |
---|
126 | Raises ValueError if no loader is available. |
---|
127 | May raise a loader-defined exception if loader fails. |
---|
128 | """ |
---|
129 | if format is None: |
---|
130 | readers = self.lookup(path) |
---|
131 | else: |
---|
132 | readers = self.readers[format] |
---|
133 | if readers!=None: |
---|
134 | for fn in readers: |
---|
135 | try: |
---|
136 | value=fn.read(path) |
---|
137 | self.reading= fn.__class__ |
---|
138 | return value |
---|
139 | except ValueError,msg: |
---|
140 | print str(msg) |
---|
141 | def __init__(self): |
---|
142 | """ Create singleton instance """ |
---|
143 | # Check whether we already have an instance |
---|
144 | if SingleLoader.__load is None: |
---|
145 | # Create and remember instance |
---|
146 | SingleLoader.__load = SingleLoader.Loader() |
---|
147 | SingleLoader.__load.__setitem__() |
---|
148 | # Store instance reference as the only member in the handle |
---|
149 | self.__dict__['_SingleLoader__load'] = SingleLoader.__load |
---|
150 | |
---|
151 | def __getattr__(self, attr): |
---|
152 | """ Delegate access to implementation """ |
---|
153 | return getattr(self.__load, attr) |
---|
154 | |
---|
155 | def __setattr__(self, attr, value): |
---|
156 | """ Delegate access to implementation """ |
---|
157 | return setattr(self.__load, attr, value) |
---|
158 | |
---|
159 | if __name__=="__main__": |
---|
160 | l = SingleLoader() |
---|
161 | print l |
---|
162 | print "look up",l.lookup('angles_flat.png') |
---|
163 | print l.__getitem__('.tiff') |
---|
164 | #print l.__contains__('.tiff') |
---|
165 | d=SingleLoader() |
---|
166 | print d.__getitem__('.tiff') |
---|