diff options
Diffstat (limited to 'mat.py')
| -rw-r--r-- | mat.py | 117 |
1 files changed, 117 insertions, 0 deletions
| @@ -0,0 +1,117 @@ | |||
| 1 | import hachoir_core.error | ||
| 2 | import hachoir_core.cmd_line | ||
| 3 | import hachoir_parser | ||
| 4 | import hachoir_metadata | ||
| 5 | import hachoir_editor | ||
| 6 | |||
| 7 | import sys | ||
| 8 | import os | ||
| 9 | import hachoir_parser.image | ||
| 10 | |||
| 11 | __version__ = "0.1" | ||
| 12 | __author__ = "jvoisin" | ||
| 13 | |||
| 14 | |||
| 15 | class file(): | ||
| 16 | def __init__(self, realname, filename, parser, editor): | ||
| 17 | self.meta = {} | ||
| 18 | self.clean = False | ||
| 19 | self.filename = filename | ||
| 20 | self.realname = realname | ||
| 21 | self.parser = parser | ||
| 22 | self.editor = editor | ||
| 23 | self.meta = self.__fill_meta() | ||
| 24 | |||
| 25 | def __fill_meta(self): | ||
| 26 | metadata = {} | ||
| 27 | try: | ||
| 28 | meta = hachoir_metadata.extractMetadata(self.parser) | ||
| 29 | except hachoir_core.error.HachoirError, err: | ||
| 30 | print("Metadata extraction error: %s" % err) | ||
| 31 | |||
| 32 | if not meta: | ||
| 33 | print("Unable to extract metadata from the file %s" % self.filename) | ||
| 34 | sys.exit(1) | ||
| 35 | |||
| 36 | for title in meta: | ||
| 37 | #fixme i'm so dirty | ||
| 38 | if title.values != []: #if the field is not empty | ||
| 39 | value = "" | ||
| 40 | for item in title.values: | ||
| 41 | value = item.text | ||
| 42 | metadata[title.key] = value | ||
| 43 | return metadata | ||
| 44 | |||
| 45 | def is_clean(self): | ||
| 46 | ''' | ||
| 47 | Return true if the file is clean from any compromizing meta | ||
| 48 | ''' | ||
| 49 | return self.clean | ||
| 50 | |||
| 51 | def remove_all(self): | ||
| 52 | ''' | ||
| 53 | Remove all the files that are compromizing | ||
| 54 | ''' | ||
| 55 | for key, field in self.meta.iteritems(): | ||
| 56 | if self._should_remove(key): | ||
| 57 | print "BLEH" #DEBUG | ||
| 58 | #__remove(self, key) | ||
| 59 | #self.clean = True | ||
| 60 | |||
| 61 | def __remove(self, field): | ||
| 62 | ''' | ||
| 63 | Remove the given file | ||
| 64 | ''' | ||
| 65 | del self.editor[field] | ||
| 66 | |||
| 67 | |||
| 68 | def get_meta(self): | ||
| 69 | ''' | ||
| 70 | return a dict with all the meta of the file | ||
| 71 | ''' | ||
| 72 | return self.meta | ||
| 73 | |||
| 74 | def _should_remove(self, field): | ||
| 75 | ''' | ||
| 76 | return True if the field is compromizing | ||
| 77 | abstract method | ||
| 78 | ''' | ||
| 79 | raise NotImplementedError() | ||
| 80 | |||
| 81 | class JpegStripper(file): | ||
| 82 | def _should_remove(self, field): | ||
| 83 | return False | ||
| 84 | |||
| 85 | strippers = { | ||
| 86 | hachoir_parser.image.JpegFile: JpegStripper, | ||
| 87 | } | ||
| 88 | |||
| 89 | def create_class_file(name): | ||
| 90 | ''' | ||
| 91 | return a $FILETYPEStripper() class, | ||
| 92 | corresponding to the filetype of the given file | ||
| 93 | ''' | ||
| 94 | if not(os.path.isfile(name)): #check if the file exist | ||
| 95 | print("Error: %s is not a valid file" % name) | ||
| 96 | sys.exit(1) | ||
| 97 | |||
| 98 | filename = "" | ||
| 99 | realname = name | ||
| 100 | filename = hachoir_core.cmd_line.unicodeFilename(name) | ||
| 101 | parser = hachoir_parser.createParser(filename, realname) | ||
| 102 | if not parser: | ||
| 103 | print("Unable to parse the file %s : sorry" % filename) | ||
| 104 | sys.exit(1) | ||
| 105 | |||
| 106 | editor = hachoir_editor.createEditor(parser) | ||
| 107 | try: | ||
| 108 | '''this part is a little tricky : | ||
| 109 | stripper_class will receice the name of the class $FILETYPEStripper, | ||
| 110 | (which herits from the "file" class), based on the editor | ||
| 111 | of given file (name) | ||
| 112 | ''' | ||
| 113 | stripper_class = strippers[editor.input.__class__] | ||
| 114 | except KeyError: | ||
| 115 | print("Don't have stripper for file type: %s" % editor.description) | ||
| 116 | sys.exit(1) | ||
| 117 | return stripper_class(realname, filename, parser, editor) | ||
