diff options
| author | jvoisin | 2011-07-26 20:26:20 +0200 |
|---|---|---|
| committer | jvoisin | 2011-07-26 20:26:20 +0200 |
| commit | 446cb258ce93a73e62c1a19779c9d67e0457412f (patch) | |
| tree | 0b5a74486de1053e58bd5a0cdb0c51116d3aaa36 /lib | |
| parent | 5e3e7aa2c45ec4d141c9534b2677219fa9d82185 (diff) | |
Simplification, and enhancements
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/mat.py | 56 |
1 files changed, 21 insertions, 35 deletions
| @@ -17,7 +17,6 @@ import images | |||
| 17 | import audio | 17 | import audio |
| 18 | import office | 18 | import office |
| 19 | import archive | 19 | import archive |
| 20 | import container | ||
| 21 | 20 | ||
| 22 | __version__ = '0.1' | 21 | __version__ = '0.1' |
| 23 | __author__ = 'jvoisin' | 22 | __author__ = 'jvoisin' |
| @@ -27,20 +26,20 @@ LOGGING_LEVEL = logging.DEBUG | |||
| 27 | logging.basicConfig(level=LOGGING_LEVEL) | 26 | logging.basicConfig(level=LOGGING_LEVEL) |
| 28 | 27 | ||
| 29 | STRIPPERS = { | 28 | STRIPPERS = { |
| 30 | hachoir_parser.archive.TarFile: archive.TarStripper, | 29 | 'application/x-tar': archive.TarStripper, |
| 31 | hachoir_parser.archive.gzip_parser.GzipParser: archive.GzipStripper, | 30 | 'application/x-gzip': archive.GzipStripper, |
| 32 | hachoir_parser.archive.bzip2_parser.Bzip2Parser: archive.Bzip2Stripper, | 31 | 'application/x-bzip2': archive.Bzip2Stripper, |
| 33 | hachoir_parser.archive.zip.ZipFile: archive.ZipStripper, | 32 | 'application/zip': archive.ZipStripper, |
| 34 | hachoir_parser.audio.MpegAudioFile: audio.MpegAudioStripper, | 33 | 'audio/mpeg': audio.MpegAudioStripper, |
| 35 | hachoir_parser.image.JpegFile: images.JpegStripper, | 34 | 'image/jpeg': images.JpegStripper, |
| 36 | hachoir_parser.image.PngFile: images.PngStripper, | 35 | 'image/png': images.PngStripper, |
| 37 | hachoir_parser.container.OggFile: container.OggStripper, | 36 | 'application/x-pdf ': office.PdfStripper, |
| 38 | hachoir_parser.misc.PDFDocument: office.PdfStripper, | 37 | 'application/vnd.oasis.opendocument': office.OpenDocumentStripper, |
| 39 | } | 38 | } |
| 40 | 39 | ||
| 41 | try: | 40 | try: |
| 42 | import mutagen | 41 | import mutagen |
| 43 | STRIPPERS[hachoir_parser.audio.FlacParser] = audio.FLACStripper | 42 | STRIPPERS[hachoir_parser.audio.FlacParser] = audio.FlacStripper |
| 44 | except ImportError: | 43 | except ImportError: |
| 45 | print('unable to import python-mutagen : limited audio format support') | 44 | print('unable to import python-mutagen : limited audio format support') |
| 46 | 45 | ||
| @@ -59,7 +58,7 @@ def is_secure(filename): | |||
| 59 | Prevent shell injection | 58 | Prevent shell injection |
| 60 | ''' | 59 | ''' |
| 61 | if not(os.path.isfile(filename)): # check if the file exist | 60 | if not(os.path.isfile(filename)): # check if the file exist |
| 62 | logging.error('Error: %s is not a valid file' % filename) | 61 | logging.error('%s is not a valid file' % filename) |
| 63 | return False | 62 | return False |
| 64 | else: | 63 | else: |
| 65 | return True | 64 | return True |
| @@ -75,41 +74,28 @@ def create_class_file(name, backup, add2archive): | |||
| 75 | 74 | ||
| 76 | filename = '' | 75 | filename = '' |
| 77 | realname = name | 76 | realname = name |
| 77 | |||
| 78 | try: | 78 | try: |
| 79 | filename = hachoir_core.cmd_line.unicodeFilename(name) | 79 | filename = hachoir_core.cmd_line.unicodeFilename(name) |
| 80 | except TypeError: # get rid of "decoding Unicode is not supported" | 80 | except TypeError: # get rid of "decoding Unicode is not supported" |
| 81 | filename = name | 81 | filename = name |
| 82 | |||
| 82 | parser = hachoir_parser.createParser(filename) | 83 | parser = hachoir_parser.createParser(filename) |
| 83 | if not parser: | 84 | if not parser: |
| 84 | logging.info('Unable to parse %s' % filename) | 85 | logging.info('Unable to parse %s' % filename) |
| 85 | return | 86 | return |
| 86 | 87 | ||
| 87 | editor = hachoir_editor.createEditor(parser) | 88 | editor = hachoir_editor.createEditor(parser) |
| 89 | mime = parser.mime_type | ||
| 90 | |||
| 91 | if mime.startswith('application/vnd.oasis.opendocument'): | ||
| 92 | mime = 'application/vnd.oasis.opendocument' # opendocument fileformat | ||
| 93 | |||
| 88 | try: | 94 | try: |
| 89 | '''this part is a little tricky : | 95 | stripper_class = STRIPPERS[mime] |
| 90 | stripper_class will receice the name of the class $FILETYPEStripper, | ||
| 91 | (which herits from the "file" class), based on the editor | ||
| 92 | of given file (name) | ||
| 93 | ''' | ||
| 94 | stripper_class = STRIPPERS[editor.input.__class__] | ||
| 95 | except KeyError: | 96 | except KeyError: |
| 96 | #Place for another lib than hachoir | ||
| 97 | logging.info('Don\'t have stripper for format %s' % editor.description) | 97 | logging.info('Don\'t have stripper for format %s' % editor.description) |
| 98 | return | 98 | return |
| 99 | 99 | ||
| 100 | if editor.input.__class__ == hachoir_parser.misc.PDFDocument: # pdf | 100 | return stripper_class(realname, filename, parser, editor, backup, |
| 101 | return stripper_class(filename, realname, backup) | 101 | add2archive) |
| 102 | |||
| 103 | elif editor.input.__class__ == hachoir_parser.archive.zip.ZipFile: | ||
| 104 | #zip based format | ||
| 105 | mime = mimetypes.guess_type(filename)[0] | ||
| 106 | if mime.startswith('application/vnd.oasis.opendocument'): | ||
| 107 | return office.OpenDocumentStripper(realname, filename, parser, | ||
| 108 | editor, backup, add2archive) | ||
| 109 | else: # normal zip | ||
| 110 | return stripper_class(realname, filename, parser, editor, | ||
| 111 | backup, add2archive) | ||
| 112 | |||
| 113 | else: # normal handling | ||
| 114 | return stripper_class(realname, filename, parser, editor, backup, | ||
| 115 | add2archive) | ||
