diff options
Diffstat (limited to 'libmat/parser.py')
| -rw-r--r-- | libmat/parser.py | 48 |
1 files changed, 24 insertions, 24 deletions
diff --git a/libmat/parser.py b/libmat/parser.py index 1765da8..eed3140 100644 --- a/libmat/parser.py +++ b/libmat/parser.py | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | ''' Parent class of all parser | 1 | """ Parent class of all parser |
| 2 | ''' | 2 | """ |
| 3 | 3 | ||
| 4 | import os | 4 | import os |
| 5 | import shutil | 5 | import shutil |
| @@ -22,8 +22,8 @@ FIELD = object() | |||
| 22 | 22 | ||
| 23 | 23 | ||
| 24 | class GenericParser(object): | 24 | class GenericParser(object): |
| 25 | ''' Parent class of all parsers | 25 | """ Parent class of all parsers |
| 26 | ''' | 26 | """ |
| 27 | def __init__(self, filename, parser, mime, backup, is_writable, **kwargs): | 27 | def __init__(self, filename, parser, mime, backup, is_writable, **kwargs): |
| 28 | self.filename = '' | 28 | self.filename = '' |
| 29 | self.parser = parser | 29 | self.parser = parser |
| @@ -40,15 +40,15 @@ class GenericParser(object): | |||
| 40 | self.output = hachoir_core.cmd_line.unicodeFilename(output) | 40 | self.output = hachoir_core.cmd_line.unicodeFilename(output) |
| 41 | 41 | ||
| 42 | def __del__(self): | 42 | def __del__(self): |
| 43 | ''' Remove tempfile if it was not used | 43 | """ Remove tempfile if it was not used |
| 44 | ''' | 44 | """ |
| 45 | if os.path.exists(self.output): | 45 | if os.path.exists(self.output): |
| 46 | mat.secure_remove(self.output) | 46 | mat.secure_remove(self.output) |
| 47 | 47 | ||
| 48 | def is_clean(self): | 48 | def is_clean(self): |
| 49 | ''' | 49 | """ |
| 50 | Check if the file is clean from harmful metadatas | 50 | Check if the file is clean from harmful metadatas |
| 51 | ''' | 51 | """ |
| 52 | for field in self.editor: | 52 | for field in self.editor: |
| 53 | if self._should_remove(field): | 53 | if self._should_remove(field): |
| 54 | return self._is_clean(self.editor) | 54 | return self._is_clean(self.editor) |
| @@ -65,16 +65,16 @@ class GenericParser(object): | |||
| 65 | return True | 65 | return True |
| 66 | 66 | ||
| 67 | def remove_all(self): | 67 | def remove_all(self): |
| 68 | ''' Remove all compromising fields | 68 | """ Remove all compromising fields |
| 69 | ''' | 69 | """ |
| 70 | state = self._remove_all(self.editor) | 70 | state = self._remove_all(self.editor) |
| 71 | hachoir_core.field.writeIntoFile(self.editor, self.output) | 71 | hachoir_core.field.writeIntoFile(self.editor, self.output) |
| 72 | self.do_backup() | 72 | self.do_backup() |
| 73 | return state | 73 | return state |
| 74 | 74 | ||
| 75 | def _remove_all(self, fieldset): | 75 | def _remove_all(self, fieldset): |
| 76 | ''' Recursive way to handle tree metadatas | 76 | """ Recursive way to handle tree metadatas |
| 77 | ''' | 77 | """ |
| 78 | try: | 78 | try: |
| 79 | for field in fieldset: | 79 | for field in fieldset: |
| 80 | remove = self._should_remove(field) | 80 | remove = self._should_remove(field) |
| @@ -87,20 +87,20 @@ class GenericParser(object): | |||
| 87 | return False | 87 | return False |
| 88 | 88 | ||
| 89 | def _remove(self, fieldset, field): | 89 | def _remove(self, fieldset, field): |
| 90 | ''' Delete the given field | 90 | """ Delete the given field |
| 91 | ''' | 91 | """ |
| 92 | del fieldset[field] | 92 | del fieldset[field] |
| 93 | 93 | ||
| 94 | def get_meta(self): | 94 | def get_meta(self): |
| 95 | ''' Return a dict with all the meta of the file | 95 | """ Return a dict with all the meta of the file |
| 96 | ''' | 96 | """ |
| 97 | metadata = {} | 97 | metadata = {} |
| 98 | self._get_meta(self.editor, metadata) | 98 | self._get_meta(self.editor, metadata) |
| 99 | return metadata | 99 | return metadata |
| 100 | 100 | ||
| 101 | def _get_meta(self, fieldset, metadata): | 101 | def _get_meta(self, fieldset, metadata): |
| 102 | ''' Recursive way to handle tree metadatas | 102 | """ Recursive way to handle tree metadatas |
| 103 | ''' | 103 | """ |
| 104 | for field in fieldset: | 104 | for field in fieldset: |
| 105 | remove = self._should_remove(field) | 105 | remove = self._should_remove(field) |
| 106 | if remove: | 106 | if remove: |
| @@ -112,22 +112,22 @@ class GenericParser(object): | |||
| 112 | self._get_meta(field, None) | 112 | self._get_meta(field, None) |
| 113 | 113 | ||
| 114 | def _should_remove(self, key): | 114 | def _should_remove(self, key): |
| 115 | ''' Return True if the field is compromising | 115 | """ Return True if the field is compromising |
| 116 | abstract method | 116 | abstract method |
| 117 | ''' | 117 | """ |
| 118 | raise NotImplementedError | 118 | raise NotImplementedError |
| 119 | 119 | ||
| 120 | def create_backup_copy(self): | 120 | def create_backup_copy(self): |
| 121 | ''' Create a backup copy | 121 | """ Create a backup copy |
| 122 | ''' | 122 | """ |
| 123 | shutil.copy2(self.filename, self.filename + '.bak') | 123 | shutil.copy2(self.filename, self.filename + '.bak') |
| 124 | 124 | ||
| 125 | def do_backup(self): | 125 | def do_backup(self): |
| 126 | ''' Keep a backup of the file if asked. | 126 | """ Keep a backup of the file if asked. |
| 127 | 127 | ||
| 128 | The process of double-renaming is not very elegant, | 128 | The process of double-renaming is not very elegant, |
| 129 | but it greatly simplify new strippers implementation. | 129 | but it greatly simplify new strippers implementation. |
| 130 | ''' | 130 | """ |
| 131 | if self.backup: | 131 | if self.backup: |
| 132 | shutil.move(self.filename, self.filename + '.bak') | 132 | shutil.move(self.filename, self.filename + '.bak') |
| 133 | else: | 133 | else: |
