diff options
| author | jvoisin | 2011-06-07 18:40:44 +0200 |
|---|---|---|
| committer | jvoisin | 2011-06-07 18:40:44 +0200 |
| commit | f7082a21d6511c5069fbb9ff186ce22f3e22fed7 (patch) | |
| tree | 93322a3220e50c1a32583ba197afec870298767c /lib/mat.py | |
First commit
Diffstat (limited to 'lib/mat.py')
| -rw-r--r-- | lib/mat.py | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/lib/mat.py b/lib/mat.py new file mode 100644 index 0000000..d22c9ab --- /dev/null +++ b/lib/mat.py | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | import hachoir_core.error | ||
| 2 | import hachoir_core.cmd_line | ||
| 3 | import hachoir_parser | ||
| 4 | import hachoir_metadata | ||
| 5 | |||
| 6 | from strippers import * | ||
| 7 | |||
| 8 | from hachoir_editor import (createEditor, | ||
| 9 | NewFieldSet, EditableInteger, EditableBytes) | ||
| 10 | |||
| 11 | import hachoir_editor | ||
| 12 | |||
| 13 | import sys | ||
| 14 | |||
| 15 | __version__ = "0.1" | ||
| 16 | __author__ = "jvoisin" | ||
| 17 | |||
| 18 | |||
| 19 | class file(): | ||
| 20 | def __init__(self, filename): | ||
| 21 | self.metadata = {} | ||
| 22 | self.clean = False | ||
| 23 | self.editor = createEditor(self.parser) | ||
| 24 | self.filename = filename | ||
| 25 | self.filename, self.realname = hachoir_core.cmd_line.unicodeFilename( | ||
| 26 | self.filename), self.filename | ||
| 27 | self.parser = hachoir_parser.createParser(self.filename, self.realname) | ||
| 28 | |||
| 29 | if not self.parser: | ||
| 30 | print("Unable to parse file : sorry") | ||
| 31 | sys.exit(1) | ||
| 32 | |||
| 33 | try: | ||
| 34 | self.meta = hachoir_metadata.extractMetadata(self.parser) | ||
| 35 | except hachoir_core.error.HachoirError, err: | ||
| 36 | print "Metadata extraction error: %s" % unicode(err) | ||
| 37 | self.data = None | ||
| 38 | |||
| 39 | if not self.meta: | ||
| 40 | print "Unable to extract metadata" | ||
| 41 | sys.exit(1) | ||
| 42 | |||
| 43 | def is_clean(self): | ||
| 44 | ''' | ||
| 45 | Return true if the file is clean from any compromizing meta | ||
| 46 | ''' | ||
| 47 | return self.clean | ||
| 48 | |||
| 49 | def remove_all(self): | ||
| 50 | ''' | ||
| 51 | Remove all the files that are compromizing | ||
| 52 | ''' | ||
| 53 | stripEditor(self.editor, self.realname, level, not(values.quiet)) | ||
| 54 | for key, field in metadata: | ||
| 55 | if should_remove(key): | ||
| 56 | remove(self, key) | ||
| 57 | |||
| 58 | def remove(self, field): | ||
| 59 | ''' | ||
| 60 | Remove the given file | ||
| 61 | ''' | ||
| 62 | del editor[field] | ||
| 63 | return True | ||
| 64 | |||
| 65 | |||
| 66 | def get_meta(self): | ||
| 67 | '''return a dict with all the meta of the file''' | ||
| 68 | #FIXME : sooooooooooo dirty ! | ||
| 69 | for title in self.meta: | ||
| 70 | if title.values != []: #if the field is not empty | ||
| 71 | value = "" | ||
| 72 | for item in title.values: | ||
| 73 | value = item.text | ||
| 74 | self.metadata[title.key] = value | ||
| 75 | return self.metadata | ||
| 76 | |||
| 77 | def should_remove(self, field): | ||
| 78 | ''' | ||
| 79 | return True if the field is compromizing | ||
| 80 | abstract method | ||
| 81 | ''' | ||
| 82 | raise NotImplementedError() | ||
| 83 | |||
| 84 | def stripEditor(editor, filename, realname, level, verbose): | ||
| 85 | ''' | ||
| 86 | Assign a stripper to an editor | ||
| 87 | ''' | ||
| 88 | cls = editor.input.__class__ | ||
| 89 | try: | ||
| 90 | stripper_cls = strippers[cls] | ||
| 91 | except KeyError: | ||
| 92 | print "Don't have stripper for file type: %s" % editor.description | ||
| 93 | return False | ||
| 94 | stripper = stripper_cls(editor, level, verbose) | ||
| 95 | |||
| 96 | if stripper(): | ||
| 97 | output = FileOutputStream(filename, realname) | ||
| 98 | editor.writeInto(output) | ||
| 99 | |||
| 100 | else: | ||
| 101 | print _("Stripper doesn't touch the file") | ||
| 102 | return True | ||
| 103 | |||
| 104 | file(sys.argv[1]).get_meta() | ||
