16 # You should have received a copy of the GNU General Public License along |
16 # You should have received a copy of the GNU General Public License along |
17 # with this program; if not, write to the Free Software Foundation, Inc., |
17 # with this program; if not, write to the Free Software Foundation, Inc., |
18 # 51 Franklin Street, 5th Floor, Boston, MA, 02111. |
18 # 51 Franklin Street, 5th Floor, Boston, MA, 02111. |
19 |
19 |
20 import errno |
20 import errno |
|
21 import logging |
21 import mimetypes |
22 import mimetypes |
22 import optparse |
23 import optparse |
23 import os |
24 import os |
24 import subprocess |
25 import subprocess |
25 import sys |
26 import sys |
246 return BOMB |
247 return BOMB |
247 |
248 |
248 |
249 |
249 class MatchHandler(object): |
250 class MatchHandler(object): |
250 def __init__(self, extractor, contents): |
251 def __init__(self, extractor, contents): |
|
252 self.logger = logging.getLogger('x-log') |
251 self.extractor = extractor |
253 self.extractor = extractor |
252 self.contents = contents |
254 self.contents = contents |
253 self.directory = extractor.basename() |
255 self.directory = extractor.basename() |
254 |
256 |
255 def extract(self, directory='.'): |
257 def extract(self, directory='.'): |
280 except OSError, error: |
282 except OSError, error: |
281 if error.errno == errno.EEXIST: |
283 if error.errno == errno.EEXIST: |
282 continue |
284 continue |
283 raise ValueError("could not make extraction directory %s: %s" % |
285 raise ValueError("could not make extraction directory %s: %s" % |
284 (error.filename, error.strerror)) |
286 (error.filename, error.strerror)) |
285 ## if suffix != '': |
287 if suffix != '': |
286 ## self.show_error("extracted to %s" % (directory,)) |
288 self.logger.warning("%s: extracted to %s", |
|
289 extractor.filename, self.directory) |
287 break |
290 break |
288 else: |
291 else: |
289 raise ValueError("all good names for an extraction directory taken") |
292 raise ValueError("all good names for an extraction directory taken") |
290 |
293 |
291 def extract(self): |
294 def extract(self): |
314 MATCHING_DIRECTORY: MatchHandler} |
317 MATCHING_DIRECTORY: MatchHandler} |
315 |
318 |
316 class ExtractorApplication(object): |
319 class ExtractorApplication(object): |
317 def __init__(self, arguments): |
320 def __init__(self, arguments): |
318 self.parse_options(arguments) |
321 self.parse_options(arguments) |
|
322 self.setup_logger() |
319 self.successes = [] |
323 self.successes = [] |
320 self.failures = [] |
324 self.failures = [] |
321 |
325 |
322 def parse_options(self, arguments): |
326 def parse_options(self, arguments): |
323 parser = optparse.OptionParser( |
327 parser = optparse.OptionParser( |
331 self.options, filenames = parser.parse_args(arguments) |
335 self.options, filenames = parser.parse_args(arguments) |
332 if not filenames: |
336 if not filenames: |
333 parser.error("you did not list any archives") |
337 parser.error("you did not list any archives") |
334 self.archives = {os.path.realpath(os.curdir): filenames} |
338 self.archives = {os.path.realpath(os.curdir): filenames} |
335 |
339 |
336 def show_error(self, message): |
340 def setup_logger(self): |
337 print >>sys.stderr, "%s: %s" % (self.current_filename, message) |
341 self.logger = logging.getLogger('x-log') |
|
342 handler = logging.StreamHandler() |
|
343 # Switch here. |
|
344 handler.setLevel(logging.WARNING) |
|
345 formatter = logging.Formatter("x: %(levelname)s: %(message)s") |
|
346 handler.setFormatter(formatter) |
|
347 self.logger.addHandler(handler) |
338 |
348 |
339 def get_extractor(self): |
349 def get_extractor(self): |
340 mimetype, encoding = mimetypes.guess_type(self.current_filename) |
350 mimetype, encoding = mimetypes.guess_type(self.current_filename) |
341 try: |
351 try: |
342 extractor = extractor_map[mimetype] |
352 extractor = extractor_map[mimetype] |
361 self.archives.setdefault(directory, []).append(basename) |
371 self.archives.setdefault(directory, []).append(basename) |
362 |
372 |
363 def report(self, function, *args): |
373 def report(self, function, *args): |
364 error = function(*args) |
374 error = function(*args) |
365 if error: |
375 if error: |
366 self.show_error(error) |
376 self.logger.error("%s: %s", self.current_filename, error) |
367 return False |
377 return False |
368 return True |
378 return True |
369 |
379 |
370 def run(self): |
380 def run(self): |
371 while self.archives: |
381 while self.archives: |