diff options
| author | jvoisin | 2015-12-02 17:07:19 +0100 |
|---|---|---|
| committer | jvoisin | 2015-12-02 17:22:45 +0100 |
| commit | 80ece3001895ea13d50915a5215fd47e313bab4c (patch) | |
| tree | c5ede43867c5d7fe2af4178b34b0e6dc219f6aac /libmat/parser.py | |
| parent | 3cf80e8b5d6faf410e9ad3aad77f23cf6418a587 (diff) | |
Remove hachoir from MAT.
This (huge) commit removes completely hachoir from MAT.
Audio files are now processed with mutagen, and images
with exiftool, since the main python imaging library (PIL)
isn't super-great to deal with metadata (and damaged/non-standard
files).
Package maintainer should change the dependencies to reflect this.
Diffstat (limited to 'libmat/parser.py')
| -rw-r--r-- | libmat/parser.py | 78 |
1 files changed, 4 insertions, 74 deletions
diff --git a/libmat/parser.py b/libmat/parser.py index 8e10ae9..2a82a25 100644 --- a/libmat/parser.py +++ b/libmat/parser.py | |||
| @@ -5,8 +5,6 @@ import os | |||
| 5 | import shutil | 5 | import shutil |
| 6 | import tempfile | 6 | import tempfile |
| 7 | 7 | ||
| 8 | import hachoir_core | ||
| 9 | import hachoir_editor | ||
| 10 | 8 | ||
| 11 | import mat | 9 | import mat |
| 12 | 10 | ||
| @@ -24,19 +22,14 @@ FIELD = object() | |||
| 24 | class GenericParser(object): | 22 | class GenericParser(object): |
| 25 | """ Parent class of all parsers | 23 | """ Parent class of all parsers |
| 26 | """ | 24 | """ |
| 27 | def __init__(self, filename, parser, mime, backup, is_writable, **kwargs): | 25 | def __init__(self, filename, mime, backup, is_writable, **kwargs): |
| 28 | self.filename = '' | 26 | self.filename = '' |
| 29 | self.parser = parser | ||
| 30 | self.mime = mime | 27 | self.mime = mime |
| 31 | self.backup = backup | 28 | self.backup = backup |
| 32 | self.is_writable = is_writable | 29 | self.is_writable = is_writable |
| 33 | self.editor = hachoir_editor.createEditor(parser) | 30 | self.filename = filename |
| 34 | try: | ||
| 35 | self.filename = hachoir_core.cmd_line.unicodeFilename(filename) | ||
| 36 | except TypeError: # get rid of "decoding Unicode is not supported" | ||
| 37 | self.filename = filename | ||
| 38 | self.basename = os.path.basename(filename) | 31 | self.basename = os.path.basename(filename) |
| 39 | self.output = hachoir_core.cmd_line.unicodeFilename(tempfile.mkstemp()[1]) | 32 | self.output = tempfile.mkstemp()[1] |
| 40 | 33 | ||
| 41 | def __del__(self): | 34 | def __del__(self): |
| 42 | """ Remove tempfile if it was not used | 35 | """ Remove tempfile if it was not used |
| @@ -48,74 +41,11 @@ class GenericParser(object): | |||
| 48 | """ | 41 | """ |
| 49 | Check if the file is clean from harmful metadatas | 42 | Check if the file is clean from harmful metadatas |
| 50 | """ | 43 | """ |
| 51 | for field in self.editor: | 44 | raise NotImplementedError |
| 52 | if self._should_remove(field): | ||
| 53 | return self._is_clean(self.editor) | ||
| 54 | return True | ||
| 55 | |||
| 56 | def _is_clean(self, fieldset): | ||
| 57 | """ Helper method of the `is_clean` one """ | ||
| 58 | for field in fieldset: | ||
| 59 | remove = self._should_remove(field) | ||
| 60 | if remove is True: | ||
| 61 | return False | ||
| 62 | if remove is FIELD: | ||
| 63 | if not self._is_clean(field): | ||
| 64 | return False | ||
| 65 | return True | ||
| 66 | 45 | ||
| 67 | def remove_all(self): | 46 | def remove_all(self): |
| 68 | """ Remove all compromising fields | 47 | """ Remove all compromising fields |
| 69 | """ | 48 | """ |
| 70 | state = self._remove_all(self.editor) | ||
| 71 | hachoir_core.field.writeIntoFile(self.editor, self.output) | ||
| 72 | self.do_backup() | ||
| 73 | return state | ||
| 74 | |||
| 75 | def _remove_all(self, fieldset): | ||
| 76 | """ Recursive way to handle tree metadatas | ||
| 77 | """ | ||
| 78 | try: | ||
| 79 | for field in fieldset: | ||
| 80 | remove = self._should_remove(field) | ||
| 81 | if remove is True: | ||
| 82 | self._remove(fieldset, field.name) | ||
| 83 | if remove is FIELD: | ||
| 84 | self._remove_all(field) | ||
| 85 | return True | ||
| 86 | except: | ||
| 87 | return False | ||
| 88 | |||
| 89 | @staticmethod | ||
| 90 | def _remove(fieldset, field): | ||
| 91 | """ Delete the given field | ||
| 92 | """ | ||
| 93 | del fieldset[field] | ||
| 94 | |||
| 95 | def get_meta(self): | ||
| 96 | """ Return a dict with all the meta of the file | ||
| 97 | """ | ||
| 98 | metadata = {} | ||
| 99 | self._get_meta(self.editor, metadata) | ||
| 100 | return metadata | ||
| 101 | |||
| 102 | def _get_meta(self, fieldset, metadata): | ||
| 103 | """ Recursive way to handle tree metadatas | ||
| 104 | """ | ||
| 105 | for field in fieldset: | ||
| 106 | remove = self._should_remove(field) | ||
| 107 | if remove: | ||
| 108 | try: | ||
| 109 | metadata[field.name] = field.value | ||
| 110 | except: | ||
| 111 | metadata[field.name] = 'harmful content' | ||
| 112 | if remove is FIELD: | ||
| 113 | self._get_meta(field, None) | ||
| 114 | |||
| 115 | def _should_remove(self, key): | ||
| 116 | """ Return True if the field is compromising | ||
| 117 | abstract method | ||
| 118 | """ | ||
| 119 | raise NotImplementedError | 49 | raise NotImplementedError |
| 120 | 50 | ||
| 121 | def create_backup_copy(self): | 51 | def create_backup_copy(self): |
