1 | #if 0 |
---|
2 | # ----------------------------------------------------------------------- |
---|
3 | # $Id: factory.py,v 1.20.2.1 2005/05/07 11:37:08 dischi Exp $ |
---|
4 | # ----------------------------------------------------------------------- |
---|
5 | # $Log: factory.py,v $ |
---|
6 | # Revision 1.20.2.1 2005/05/07 11:37:08 dischi |
---|
7 | # make sure all strings are unicode |
---|
8 | # |
---|
9 | # Revision 1.20 2004/05/29 12:30:36 dischi |
---|
10 | # add function to correct data from the different mime modules |
---|
11 | # |
---|
12 | # Revision 1.19 2004/05/28 19:32:31 dischi |
---|
13 | # remove old stuff |
---|
14 | # |
---|
15 | # Revision 1.18 2004/05/17 19:10:57 dischi |
---|
16 | # better DEBUG handling |
---|
17 | # |
---|
18 | # Revision 1.17 2004/05/02 08:28:20 dischi |
---|
19 | # dvd iso support |
---|
20 | # |
---|
21 | # Revision 1.16 2004/02/03 20:41:18 dischi |
---|
22 | # add directory support |
---|
23 | # |
---|
24 | # Revision 1.15 2004/01/31 12:25:20 dischi |
---|
25 | # better ext checking |
---|
26 | # |
---|
27 | # Revision 1.13 2003/09/22 16:24:58 the_krow |
---|
28 | # o added flac |
---|
29 | # o try-except block around ioctl since it is not avaiable in all OS |
---|
30 | # |
---|
31 | # Revision 1.12 2003/09/14 13:50:42 dischi |
---|
32 | # make it possible to scan extention based only |
---|
33 | # |
---|
34 | # Revision 1.11 2003/09/01 19:23:23 dischi |
---|
35 | # ignore case when searching the correct extention |
---|
36 | # |
---|
37 | # Revision 1.10 2003/08/30 12:16:24 dischi |
---|
38 | # special handling for directories |
---|
39 | # |
---|
40 | # Revision 1.8 2003/08/26 18:01:26 outlyer |
---|
41 | # Patch from Lars Eggert for FreeBSD support |
---|
42 | # |
---|
43 | # Revision 1.4 2003/07/04 15:34:45 outlyer |
---|
44 | # Allow 'cdda' as well as 'cd' since we get that sometimes, and make sure |
---|
45 | # we import urllib. |
---|
46 | # |
---|
47 | # Revision 1.3 2003/07/02 09:32:16 the_krow |
---|
48 | # More Keys |
---|
49 | # import traceback was missing in factory |
---|
50 | # |
---|
51 | # Revision 1.1 2003/06/30 13:17:18 the_krow |
---|
52 | # o Refactored mediainfo into factory, synchronizedobject |
---|
53 | # o Parsers now register directly at mmpython not at mmpython.mediainfo |
---|
54 | # o use mmpython.Factory() instead of mmpython.mediainfo.get_singleton() |
---|
55 | # o Bugfix in PNG parser |
---|
56 | # o Renamed disc.AudioInfo into disc.AudioDiscInfo |
---|
57 | # o Renamed disc.DataInfo into disc.DataDiscInfo |
---|
58 | # |
---|
59 | # ----------------------------------------------------------------------- |
---|
60 | # MMPython - Media Metadata for Python |
---|
61 | # Copyright (C) 2003 Thomas Schueppel, Dirk Meyer |
---|
62 | # |
---|
63 | # This program is free software; you can redistribute it and/or modify |
---|
64 | # it under the terms of the GNU General Public License as published by |
---|
65 | # the Free Software Foundation; either version 2 of the License, or |
---|
66 | # (at your option) any later version. |
---|
67 | # |
---|
68 | # This program is distributed in the hope that it will be useful, but |
---|
69 | # WITHOUT ANY WARRANTY; without even the implied warranty of MER- |
---|
70 | # CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
---|
71 | # Public License for more details. |
---|
72 | # |
---|
73 | # You should have received a copy of the GNU General Public License along |
---|
74 | # with this program; if not, write to the Free Software Foundation, Inc., |
---|
75 | # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
76 | # |
---|
77 | # ----------------------------------------------------------------------- |
---|
78 | #endif |
---|
79 | |
---|
80 | import mediainfo |
---|
81 | import stat |
---|
82 | import os |
---|
83 | import urlparse |
---|
84 | import traceback |
---|
85 | import urllib |
---|
86 | |
---|
87 | DEBUG = 0 |
---|
88 | |
---|
89 | try: |
---|
90 | DEBUG = int(os.environ['MMPYTHON_DEBUG']) |
---|
91 | except: |
---|
92 | pass |
---|
93 | |
---|
94 | |
---|
95 | |
---|
96 | def isurl(url): |
---|
97 | return url.find('://') > 0 |
---|
98 | |
---|
99 | class Factory: |
---|
100 | """ |
---|
101 | Abstract Factory for the creation of MediaInfo instances. The different Methods |
---|
102 | create MediaInfo objects by parsing the given medium. |
---|
103 | """ |
---|
104 | def __init__(self): |
---|
105 | self.extmap = {} |
---|
106 | self.mimemap = {} |
---|
107 | self.types = [] |
---|
108 | self.device_types = [] |
---|
109 | self.directory_types = [] |
---|
110 | self.stream_types = [] |
---|
111 | |
---|
112 | def create_from_file(self, file, ext_only=0): |
---|
113 | """ |
---|
114 | create based on the file stream 'file |
---|
115 | """ |
---|
116 | # Check extension as a hint |
---|
117 | for e in self.extmap.keys(): |
---|
118 | if DEBUG > 1: print "trying ext %s" % e |
---|
119 | if file.name.lower().endswith(e.lower()): |
---|
120 | if DEBUG == 1: print "trying ext %s" % e |
---|
121 | try: |
---|
122 | file.seek(0,0) |
---|
123 | t = self.extmap[e][3](file) |
---|
124 | if t.valid: return t |
---|
125 | except: |
---|
126 | if DEBUG: |
---|
127 | traceback.print_exc() |
---|
128 | |
---|
129 | # no searching on all types |
---|
130 | if ext_only: |
---|
131 | return None |
---|
132 | |
---|
133 | if DEBUG: |
---|
134 | print "No Type found by Extension. Trying all" |
---|
135 | |
---|
136 | for e in self.types: |
---|
137 | if DEBUG: print "Trying %s" % e[0] |
---|
138 | try: |
---|
139 | file.seek(0,0) |
---|
140 | t = e[3](file) |
---|
141 | if t.valid: |
---|
142 | if DEBUG: print 'found' |
---|
143 | return t |
---|
144 | except: |
---|
145 | if DEBUG: |
---|
146 | traceback.print_exc() |
---|
147 | if DEBUG: print 'not found' |
---|
148 | return None |
---|
149 | |
---|
150 | |
---|
151 | def create_from_url(self,url): |
---|
152 | """ |
---|
153 | Create information for urls. This includes file:// and cd:// |
---|
154 | """ |
---|
155 | split = urlparse.urlsplit(url) |
---|
156 | scheme = split[0] |
---|
157 | |
---|
158 | if scheme == 'file': |
---|
159 | (scheme, location, path, query, fragment) = split |
---|
160 | return self.create_from_filename(location+path) |
---|
161 | |
---|
162 | elif scheme == 'cdda': |
---|
163 | r = self.create_from_filename(split[4]) |
---|
164 | if r: |
---|
165 | r.url = url |
---|
166 | return r |
---|
167 | |
---|
168 | elif scheme == 'http': |
---|
169 | # Quick Hack for webradio support |
---|
170 | # We will need some more soffisticated and generic construction |
---|
171 | # method for this. Perhaps move file.open stuff into __init__ |
---|
172 | # instead of doing it here... |
---|
173 | for e in self.stream_types: |
---|
174 | if DEBUG: print 'Trying %s' % e[0] |
---|
175 | t = e[3](url) |
---|
176 | if t.valid: |
---|
177 | t.url = url |
---|
178 | return t |
---|
179 | |
---|
180 | else: |
---|
181 | (scheme, location, path, query, fragment) = split |
---|
182 | uhandle = urllib.urlopen(url) |
---|
183 | mime = uhandle.info().gettype() |
---|
184 | print "Trying %s" % mime |
---|
185 | if self.mimemap.has_key(mime): |
---|
186 | t = self.mimemap[mime][3](file) |
---|
187 | if t.valid: return t |
---|
188 | # XXX Todo: Try other types |
---|
189 | pass |
---|
190 | |
---|
191 | |
---|
192 | def create_from_filename(self, filename, ext_only=0): |
---|
193 | """ |
---|
194 | Create information for the given filename |
---|
195 | """ |
---|
196 | if os.path.isdir(filename): |
---|
197 | return None |
---|
198 | if os.path.isfile(filename): |
---|
199 | try: |
---|
200 | f = open(filename,'rb') |
---|
201 | except IOError: |
---|
202 | print 'IOError reading %s' % filename |
---|
203 | return None |
---|
204 | r = self.create_from_file(f, ext_only) |
---|
205 | f.close() |
---|
206 | if r: |
---|
207 | r.correct_data() |
---|
208 | r.url = 'file://%s' % os.path.abspath(filename) |
---|
209 | return r |
---|
210 | return None |
---|
211 | |
---|
212 | |
---|
213 | def create_from_device(self,devicename): |
---|
214 | """ |
---|
215 | Create information from the device. Currently only rom drives |
---|
216 | are supported. |
---|
217 | """ |
---|
218 | for e in self.device_types: |
---|
219 | if DEBUG: print 'Trying %s' % e[0] |
---|
220 | t = e[3](devicename) |
---|
221 | if t.valid: |
---|
222 | t.url = 'file://%s' % os.path.abspath(devicename) |
---|
223 | return t |
---|
224 | return None |
---|
225 | |
---|
226 | |
---|
227 | def create_from_directory(self, dirname): |
---|
228 | """ |
---|
229 | Create information from the directory. |
---|
230 | """ |
---|
231 | for e in self.directory_types: |
---|
232 | if DEBUG: print 'Trying %s' % e[0] |
---|
233 | t = e[3](dirname) |
---|
234 | if t.valid: |
---|
235 | return t |
---|
236 | return None |
---|
237 | |
---|
238 | |
---|
239 | def create(self, name, ext_only=0): |
---|
240 | """ |
---|
241 | Global 'create' function. This function calls the different |
---|
242 | 'create_from_'-functions. |
---|
243 | """ |
---|
244 | try: |
---|
245 | if isurl(name): |
---|
246 | return self.create_from_url(name) |
---|
247 | if not os.path.exists(name): |
---|
248 | return None |
---|
249 | try: |
---|
250 | if (os.uname()[0] == 'FreeBSD' and \ |
---|
251 | stat.S_ISCHR(os.stat(name)[stat.ST_MODE])) \ |
---|
252 | or stat.S_ISBLK(os.stat(name)[stat.ST_MODE]): |
---|
253 | return self.create_from_device(name) |
---|
254 | except AttributeError: |
---|
255 | pass |
---|
256 | if os.path.isdir(name): |
---|
257 | return self.create_from_directory(name) |
---|
258 | return self.create_from_filename(name, ext_only) |
---|
259 | except: |
---|
260 | print 'mmpython.create error:' |
---|
261 | traceback.print_exc() |
---|
262 | print |
---|
263 | print 'Please report this bug to the Freevo mailing list' |
---|
264 | return None |
---|
265 | |
---|
266 | |
---|
267 | |
---|
268 | def register(self,mimetype,extensions,type,c): |
---|
269 | """ |
---|
270 | register the parser to mmpython |
---|
271 | """ |
---|
272 | if DEBUG > 0: |
---|
273 | print "%s registered" % mimetype |
---|
274 | tuple = (mimetype,extensions,type,c) |
---|
275 | |
---|
276 | if extensions == mediainfo.EXTENSION_DEVICE: |
---|
277 | self.device_types.append(tuple) |
---|
278 | elif extensions == mediainfo.EXTENSION_DIRECTORY: |
---|
279 | self.directory_types.append(tuple) |
---|
280 | elif extensions == mediainfo.EXTENSION_STREAM: |
---|
281 | self.stream_types.append(tuple) |
---|
282 | else: |
---|
283 | self.types.append(tuple) |
---|
284 | for e in extensions: |
---|
285 | self.extmap[e] = tuple |
---|
286 | self.mimemap[mimetype] = tuple |
---|
287 | |
---|
288 | |
---|
289 | def get(self, mimetype, extensions): |
---|
290 | """ |
---|
291 | return the object for mimetype/extensions or None |
---|
292 | """ |
---|
293 | if extensions == mediainfo.EXTENSION_DEVICE: |
---|
294 | l = self.device_types |
---|
295 | elif extensions == mediainfo.EXTENSION_DIRECTORY: |
---|
296 | l = self.directory_types |
---|
297 | elif extensions == mediainfo.EXTENSION_STREAM: |
---|
298 | l = self.stream_types |
---|
299 | else: |
---|
300 | l = self.types |
---|
301 | |
---|
302 | for info in l: |
---|
303 | if info[0] == mimetype and info[1] == extensions: |
---|
304 | return info[3] |
---|
305 | |
---|
306 | return None |
---|