diff options
| author | jvoisin | 2011-06-20 01:25:33 +0200 |
|---|---|---|
| committer | jvoisin | 2011-06-20 01:25:33 +0200 |
| commit | 52f2fedd5d73807d42ba5c397c3e4c5348b47a47 (patch) | |
| tree | 09070e23428f3f72e4a95da8df33520adacbf01f /lib/mat.py | |
| parent | de5917e5f01374bb1a647f49ae85283241a2bea9 (diff) | |
Introduction of a nice separation of functions/class in differents files
Diffstat (limited to 'lib/mat.py')
| -rw-r--r--[-rwxr-xr-x] | lib/mat.py | 99 |
1 files changed, 5 insertions, 94 deletions
diff --git a/lib/mat.py b/lib/mat.py index 48b83fb..5641c62 100755..100644 --- a/lib/mat.py +++ b/lib/mat.py | |||
| @@ -3,111 +3,22 @@ | |||
| 3 | ''' | 3 | ''' |
| 4 | Metadata anonymisation toolkit library | 4 | Metadata anonymisation toolkit library |
| 5 | ''' | 5 | ''' |
| 6 | |||
| 6 | import sys | 7 | import sys |
| 7 | import os | 8 | import os |
| 8 | 9 | ||
| 9 | import hachoir_core.error | ||
| 10 | import hachoir_core.field | ||
| 11 | import hachoir_core.cmd_line | 10 | import hachoir_core.cmd_line |
| 12 | import hachoir_parser | 11 | import hachoir_parser |
| 13 | import hachoir_metadata | ||
| 14 | import hachoir_parser.image | ||
| 15 | |||
| 16 | sys.path.append('..') | ||
| 17 | import hachoir_editor | 12 | import hachoir_editor |
| 18 | 13 | ||
| 14 | import images | ||
| 15 | |||
| 19 | __version__ = "0.1" | 16 | __version__ = "0.1" |
| 20 | __author__ = "jvoisin" | 17 | __author__ = "jvoisin" |
| 21 | 18 | ||
| 22 | POSTFIX = ".cleaned" | ||
| 23 | |||
| 24 | class file(): | ||
| 25 | def __init__(self, realname, filename, parser, editor): | ||
| 26 | self.meta = {} | ||
| 27 | self.filename = filename | ||
| 28 | self.realname = realname | ||
| 29 | self.parser = parser | ||
| 30 | self.editor = editor | ||
| 31 | self.meta = self.__fill_meta() | ||
| 32 | |||
| 33 | def __fill_meta(self): | ||
| 34 | metadata = {} | ||
| 35 | try: | ||
| 36 | meta = hachoir_metadata.extractMetadata(self.parser) | ||
| 37 | except hachoir_core.error.HachoirError, err: | ||
| 38 | print("Metadata extraction error: %s" % err) | ||
| 39 | |||
| 40 | if not meta: | ||
| 41 | print("Unable to extract metadata from the file %s" % self.filename) | ||
| 42 | sys.exit(1) | ||
| 43 | |||
| 44 | for title in meta: | ||
| 45 | #fixme i'm so dirty | ||
| 46 | if title.values != []: #if the field is not empty | ||
| 47 | value = "" | ||
| 48 | for item in title.values: | ||
| 49 | value = item.text | ||
| 50 | metadata[title.key] = value | ||
| 51 | return metadata | ||
| 52 | |||
| 53 | def is_clean(self): | ||
| 54 | ''' | ||
| 55 | Check if the file is clean from harmful metadatas | ||
| 56 | ''' | ||
| 57 | for field in self.editor: | ||
| 58 | if self._should_remove(field): | ||
| 59 | return False | ||
| 60 | return True | ||
| 61 | |||
| 62 | def remove_all(self): | ||
| 63 | ''' | ||
| 64 | Remove all the files that are compromizing | ||
| 65 | ''' | ||
| 66 | for field in self.editor: | ||
| 67 | if self._should_remove(field): | ||
| 68 | self._remove(field) | ||
| 69 | hachoir_core.field.writeIntoFile(self.editor, self.filename + POSTFIX) | ||
| 70 | |||
| 71 | def _remove(self, field): | ||
| 72 | ''' | ||
| 73 | Remove the given field | ||
| 74 | ''' | ||
| 75 | del self.editor[field.name] | ||
| 76 | |||
| 77 | |||
| 78 | def get_meta(self): | ||
| 79 | ''' | ||
| 80 | return a dict with all the meta of the file | ||
| 81 | ''' | ||
| 82 | #am I useless ? | ||
| 83 | return self.meta | ||
| 84 | |||
| 85 | def _should_remove(self, key): | ||
| 86 | ''' | ||
| 87 | return True if the field is compromizing | ||
| 88 | abstract method | ||
| 89 | ''' | ||
| 90 | raise NotImplementedError() | ||
| 91 | |||
| 92 | class JpegStripper(file): | ||
| 93 | def _should_remove(self, field): | ||
| 94 | if field.name.startswith('comment'): | ||
| 95 | return True | ||
| 96 | elif field.name in ("photoshop", "exif", "adobe"): | ||
| 97 | return True | ||
| 98 | else: | ||
| 99 | return False | ||
| 100 | |||
| 101 | class PngStripper(file): | ||
| 102 | def _should_remove(self, field): | ||
| 103 | if field.name in ('comment'): | ||
| 104 | return True | ||
| 105 | else: | ||
| 106 | return False | ||
| 107 | |||
| 108 | strippers = { | 19 | strippers = { |
| 109 | hachoir_parser.image.JpegFile: JpegStripper, | 20 | hachoir_parser.image.JpegFile: images.JpegStripper, |
| 110 | hachoir_parser.image.PngFile: PngStripper, | 21 | hachoir_parser.image.PngFile: images.PngStripper, |
| 111 | } | 22 | } |
| 112 | 23 | ||
| 113 | def create_class_file(name): | 24 | def create_class_file(name): |
