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 | #print "in findplugings",path |
---|
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 Loader(object): |
---|
44 | |
---|
45 | __load = None |
---|
46 | class Load(object): |
---|
47 | |
---|
48 | def __init__(self): |
---|
49 | self.readers = {} |
---|
50 | self.reading=None |
---|
51 | self.__setitem__() |
---|
52 | #print "in loader",self.readers |
---|
53 | |
---|
54 | def __setitem__(self, ext=None, reader=None): |
---|
55 | import os |
---|
56 | if reader==None and ext==None:#1st load |
---|
57 | plugReader=None |
---|
58 | if os.path.isdir('plugins'): |
---|
59 | #print "plugreader in pluging",plugReader |
---|
60 | plugReader=_findReaders('plugins')# import all module in plugins |
---|
61 | if os.path.isdir('../plugins'): |
---|
62 | #print "plugreader in Dataloader pluging",plugReader |
---|
63 | plugReader=_findReaders('../plugins') |
---|
64 | else: |
---|
65 | if os.path.isdir('..\DataLoader\plugins'): |
---|
66 | os.chdir(os.path.abspath('..\DataLoader\plugins'))# change the current |
---|
67 | #directory to dataLoader\plugins |
---|
68 | plugReader=_findReaders('plugins') |
---|
69 | |
---|
70 | if plugReader !=None: |
---|
71 | for preader in plugReader:# for each modules takes list of extensions |
---|
72 | print preader,preader.ext |
---|
73 | for item in preader.ext: |
---|
74 | ext=item |
---|
75 | if ext not in self.readers:#assign extension with its reader |
---|
76 | self.readers[ext] = [] |
---|
77 | self.readers[ext].insert(0,preader) |
---|
78 | |
---|
79 | #print "plugreader",plugReader |
---|
80 | #print "In setitem",self.readers |
---|
81 | elif reader !=None and ext !=None: |
---|
82 | if ext not in self.readers: |
---|
83 | self.readers[ext] = [] |
---|
84 | self.readers[ext].insert(0,reader) |
---|
85 | elif reader!=None: |
---|
86 | for item in reader.ext: |
---|
87 | ext=item |
---|
88 | if ext not in self.readers:#assign extension with its reader |
---|
89 | self.readers[ext] = [] |
---|
90 | self.readers[ext].insert(0,reader) |
---|
91 | else: |
---|
92 | raise ValueError,"missing reader" |
---|
93 | |
---|
94 | |
---|
95 | def __getitem__(self, ext): |
---|
96 | return self.readers[ext] |
---|
97 | |
---|
98 | def __contains__(self, ext): |
---|
99 | return ext in self.readers |
---|
100 | |
---|
101 | |
---|
102 | def formats(self, name=True, ext=False): |
---|
103 | """ |
---|
104 | Return a list of the registered formats. If name=True then |
---|
105 | named formats are returned. If ext=True then extensions |
---|
106 | are returned. |
---|
107 | """ |
---|
108 | names = [a for a in self.readers.keys() if not a.startswith('.')] |
---|
109 | exts = [a for a in self.readers.keys() if a.startswith('.')] |
---|
110 | names.sort() |
---|
111 | exts.sort() |
---|
112 | ret = [] |
---|
113 | if name: ret += names |
---|
114 | if ext: ret += exts |
---|
115 | return ret |
---|
116 | |
---|
117 | def lookup(self, path): |
---|
118 | """ |
---|
119 | Return the loader associated with the file type of path. |
---|
120 | """ |
---|
121 | file = os.path.basename(path) |
---|
122 | idx = file.find('.') |
---|
123 | ext = file[idx:] if idx >= 0 else '' |
---|
124 | #print "exten",ext |
---|
125 | #print self.readers |
---|
126 | try: |
---|
127 | return self.readers[ext] |
---|
128 | except: |
---|
129 | #raise ValueError, "Unknown file type '%s'"%ext |
---|
130 | print "Unknown file type '%s'"%ext |
---|
131 | |
---|
132 | |
---|
133 | def getAcTReader(self,path): |
---|
134 | return self.reading |
---|
135 | |
---|
136 | def load(self, path, format=None): |
---|
137 | """ |
---|
138 | Call the loader for the file type of path. |
---|
139 | |
---|
140 | Raises ValueError if no loader is available. |
---|
141 | May raise a loader-defined exception if loader fails. |
---|
142 | """ |
---|
143 | try: |
---|
144 | #print "in load",os.path.abspath(path) |
---|
145 | os.path.isfile( os.path.abspath(path)) |
---|
146 | #print "went here in load check path" |
---|
147 | except: |
---|
148 | raise ValueError," file doesn't exist" |
---|
149 | if format is None: |
---|
150 | readers = self.lookup(path) |
---|
151 | else: |
---|
152 | readers = self.readers[format] |
---|
153 | if readers!=None: |
---|
154 | |
---|
155 | for fn in readers: |
---|
156 | try: |
---|
157 | #print "path",os.getcwd( ) |
---|
158 | value=fn.read(path) |
---|
159 | self.reading= fn.__class__ |
---|
160 | return value |
---|
161 | except ValueError,msg: |
---|
162 | print str(msg) |
---|
163 | def __init__(self): |
---|
164 | """ Create singleton instance """ |
---|
165 | # Check whether we already have an instance |
---|
166 | if Loader.__load is None: |
---|
167 | # Create and remember instance |
---|
168 | Loader.__load = Loader.Load() |
---|
169 | Loader.__load.__setitem__() |
---|
170 | # Store instance reference as the only member in the handle |
---|
171 | self.__dict__['_Loader__load'] = Loader.__load |
---|
172 | |
---|
173 | def __getattr__(self, attr): |
---|
174 | """ Delegate access to implementation """ |
---|
175 | return getattr(self.__load, attr) |
---|
176 | |
---|
177 | def __setattr__(self, attr, value): |
---|
178 | """ Delegate access to implementation """ |
---|
179 | return setattr(self.__load, attr, value) |
---|