summaryrefslogtreecommitdiff
path: root/mat.py
diff options
context:
space:
mode:
authorjvoisin2011-06-18 02:13:37 +0200
committerjvoisin2011-06-18 02:13:37 +0200
commitb1dab4c844e2fae4909eca47c2b42698d12c55fc (patch)
tree939c9060927e9ea88dfdf93ea832167b21602300 /mat.py
parentb1b4df83b071adb63d9aa3c981094d9d6edb345c (diff)
Hachoir is so ugly : I am able to display meta, but not to remove them (because I didn't manage to find an elegant/efficient way to find the "path" to a field). So I remove a whole "Rootfield" (exif, author, comment, ...). It's efficient, but not very elegant.
Diffstat (limited to 'mat.py')
-rwxr-xr-xmat.py44
1 files changed, 17 insertions, 27 deletions
diff --git a/mat.py b/mat.py
index f59bdce..200fc04 100755
--- a/mat.py
+++ b/mat.py
@@ -18,6 +18,7 @@ import hachoir_parser.image
18__version__ = "0.1" 18__version__ = "0.1"
19__author__ = "jvoisin" 19__author__ = "jvoisin"
20 20
21POSTFIX = ".cleaned"
21 22
22class file(): 23class file():
23 def __init__(self, realname, filename, parser, editor): 24 def __init__(self, realname, filename, parser, editor):
@@ -52,8 +53,8 @@ class file():
52 ''' 53 '''
53 Check if the file is clean from harmful metadatas 54 Check if the file is clean from harmful metadatas
54 ''' 55 '''
55 for key, field in self.meta.iteritems(): 56 for field in self.editor:
56 if self._should_remove(key): 57 if self._should_remove(field):
57 return False 58 return False
58 return True 59 return True
59 60
@@ -61,37 +62,25 @@ class file():
61 ''' 62 '''
62 Remove all the files that are compromizing 63 Remove all the files that are compromizing
63 ''' 64 '''
64 for key, field in self.meta.iteritems(): 65 for field in self.editor:
65 if self._should_remove(key): 66 if self._should_remove(field):
66 self._remove(key) 67 self._remove(field)
67 hachoir_core.field.writeIntoFile(self.editor, self.filename + "cleaned") 68 hachoir_core.field.writeIntoFile(self.editor, self.filename + POSTFIX)
68 69
69 70 def _remove(self, field):
70 def _remove(self, key):
71 ''' 71 '''
72 Remove the given field 72 Remove the given field
73 ''' 73 '''
74 del self.editor[key] 74 del self.editor[field.name]
75 75
76 76
77 def get_meta(self): 77 def get_meta(self):
78 ''' 78 '''
79 return a dict with all the meta of the file 79 return a dict with all the meta of the file
80 ''' 80 '''
81 #am I useless ?
81 return self.meta 82 return self.meta
82 83
83 def get_harmful(self):
84 '''
85 return a dict with all the harmful meta of the file
86 '''
87 harmful = {}
88 for key, value in self.meta.iteritems():
89 if self._should_remove(key):
90 harmful[key] = value
91 return harmful
92
93
94
95 def _should_remove(self, key): 84 def _should_remove(self, key):
96 ''' 85 '''
97 return True if the field is compromizing 86 return True if the field is compromizing
@@ -100,19 +89,20 @@ class file():
100 raise NotImplementedError() 89 raise NotImplementedError()
101 90
102class JpegStripper(file): 91class JpegStripper(file):
103 def _should_remove(self, key): 92 def _should_remove(self, field):
104 if key in ('comment', 'author'): 93 if field.name.startswith('comment'):
94 return True
95 elif field.name in ("photoshop", "exif", "adobe"):
105 return True 96 return True
106 else: 97 else:
107 return False 98 return False
108 99
109class PngStripper(file): 100class PngStripper(file):
110 def _should_remove(self, key): 101 def _should_remove(self, field):
111 if key in ('comment', 'author'): 102 if field.name in ('comment'):
112 return True 103 return True
113 else: 104 else:
114 return False 105 return False
115 return False
116 106
117strippers = { 107strippers = {
118 hachoir_parser.image.JpegFile: JpegStripper, 108 hachoir_parser.image.JpegFile: JpegStripper,
@@ -131,7 +121,7 @@ def create_class_file(name):
131 filename = "" 121 filename = ""
132 realname = name 122 realname = name
133 filename = hachoir_core.cmd_line.unicodeFilename(name) 123 filename = hachoir_core.cmd_line.unicodeFilename(name)
134 parser = hachoir_parser.createParser(filename, realname) 124 parser = hachoir_parser.createParser(filename)
135 if not parser: 125 if not parser:
136 print("Unable to parse the file %s : sorry" % filename) 126 print("Unable to parse the file %s : sorry" % filename)
137 sys.exit(1) 127 sys.exit(1)