diff options
| author | jvoisin | 2018-04-01 12:30:00 +0200 |
|---|---|---|
| committer | jvoisin | 2018-04-01 12:30:00 +0200 |
| commit | 27beda354d8b78c1716e659273c180d4ddfb144b (patch) | |
| tree | 5420d509f2538f742405771d8433e81ce2984148 | |
| parent | 711347c87f189a4fd1bd425144934016b79f099c (diff) | |
Move every image-related parser into a single file
| -rw-r--r-- | src/images.py (renamed from src/images_pixbuf.py) | 23 | ||||
| -rw-r--r-- | src/png.py | 27 | ||||
| -rw-r--r-- | tests/test_libmat2.py | 20 |
3 files changed, 33 insertions, 37 deletions
diff --git a/src/images_pixbuf.py b/src/images.py index 8eeffbe..560886a 100644 --- a/src/images_pixbuf.py +++ b/src/images.py | |||
| @@ -2,12 +2,35 @@ import subprocess | |||
| 2 | import json | 2 | import json |
| 3 | import os | 3 | import os |
| 4 | 4 | ||
| 5 | import cairo | ||
| 6 | |||
| 5 | import gi | 7 | import gi |
| 6 | gi.require_version('GdkPixbuf', '2.0') | 8 | gi.require_version('GdkPixbuf', '2.0') |
| 7 | from gi.repository import GdkPixbuf | 9 | from gi.repository import GdkPixbuf |
| 8 | 10 | ||
| 9 | from . import abstract | 11 | from . import abstract |
| 10 | 12 | ||
| 13 | class PNGParser(abstract.AbstractParser): | ||
| 14 | mimetypes = {'image/png', } | ||
| 15 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', | ||
| 16 | 'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate', | ||
| 17 | "FileInodeChangeDate", 'FilePermissions', 'FileType', | ||
| 18 | 'FileTypeExtension', 'MIMEType', 'ImageWidth', 'BitDepth', 'ColorType', | ||
| 19 | 'Compression', 'Filter', 'Interlace', 'BackgroundColor', 'ImageSize', | ||
| 20 | 'Megapixels', 'ImageHeight'} | ||
| 21 | |||
| 22 | def get_meta(self): | ||
| 23 | out = subprocess.check_output(['exiftool', '-json', self.filename]) | ||
| 24 | meta = json.loads(out.decode('utf-8'))[0] | ||
| 25 | for key in self.meta_whitelist: | ||
| 26 | meta.pop(key, None) | ||
| 27 | return meta | ||
| 28 | |||
| 29 | def remove_all(self): | ||
| 30 | surface = cairo.ImageSurface.create_from_png(self.filename) | ||
| 31 | surface.write_to_png(self.output_filename) | ||
| 32 | return True | ||
| 33 | |||
| 11 | class GdkPixbufAbstractParser(abstract.AbstractParser): | 34 | class GdkPixbufAbstractParser(abstract.AbstractParser): |
| 12 | def get_meta(self): | 35 | def get_meta(self): |
| 13 | out = subprocess.check_output(['exiftool', '-json', self.filename]) | 36 | out = subprocess.check_output(['exiftool', '-json', self.filename]) |
diff --git a/src/png.py b/src/png.py deleted file mode 100644 index 377682e..0000000 --- a/src/png.py +++ /dev/null | |||
| @@ -1,27 +0,0 @@ | |||
| 1 | import subprocess | ||
| 2 | import json | ||
| 3 | |||
| 4 | import cairo | ||
| 5 | |||
| 6 | from . import abstract | ||
| 7 | |||
| 8 | class PNGParser(abstract.AbstractParser): | ||
| 9 | mimetypes = {'image/png', } | ||
| 10 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', | ||
| 11 | 'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate', | ||
| 12 | "FileInodeChangeDate", 'FilePermissions', 'FileType', | ||
| 13 | 'FileTypeExtension', 'MIMEType', 'ImageWidth', 'BitDepth', 'ColorType', | ||
| 14 | 'Compression', 'Filter', 'Interlace', 'BackgroundColor', 'ImageSize', | ||
| 15 | 'Megapixels', 'ImageHeight'} | ||
| 16 | |||
| 17 | def get_meta(self): | ||
| 18 | out = subprocess.check_output(['exiftool', '-json', self.filename]) | ||
| 19 | meta = json.loads(out.decode('utf-8'))[0] | ||
| 20 | for key in self.meta_whitelist: | ||
| 21 | meta.pop(key, None) | ||
| 22 | return meta | ||
| 23 | |||
| 24 | def remove_all(self): | ||
| 25 | surface = cairo.ImageSurface.create_from_png(self.filename) | ||
| 26 | surface.write_to_png(self.output_filename) | ||
| 27 | return True | ||
diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py index 5b7dfb1..8aa91f0 100644 --- a/tests/test_libmat2.py +++ b/tests/test_libmat2.py | |||
| @@ -6,7 +6,7 @@ import os | |||
| 6 | import zipfile | 6 | import zipfile |
| 7 | import tempfile | 7 | import tempfile |
| 8 | 8 | ||
| 9 | from src import pdf, png, images_pixbuf, audio, office, parser_factory | 9 | from src import pdf, images, audio, office, parser_factory |
| 10 | 10 | ||
| 11 | class TestGetMeta(unittest.TestCase): | 11 | class TestGetMeta(unittest.TestCase): |
| 12 | def test_pdf(self): | 12 | def test_pdf(self): |
| @@ -16,18 +16,18 @@ class TestGetMeta(unittest.TestCase): | |||
| 16 | self.assertEqual(meta['creator'], "'Certified by IEEE PDFeXpress at 03/19/2016 2:56:07 AM'") | 16 | self.assertEqual(meta['creator'], "'Certified by IEEE PDFeXpress at 03/19/2016 2:56:07 AM'") |
| 17 | 17 | ||
| 18 | def test_png(self): | 18 | def test_png(self): |
| 19 | p = png.PNGParser('./tests/data/dirty.png') | 19 | p = images.PNGParser('./tests/data/dirty.png') |
| 20 | meta = p.get_meta() | 20 | meta = p.get_meta() |
| 21 | self.assertEqual(meta['Comment'], 'This is a comment, be careful!') | 21 | self.assertEqual(meta['Comment'], 'This is a comment, be careful!') |
| 22 | self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25") | 22 | self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25") |
| 23 | 23 | ||
| 24 | def test_jpg(self): | 24 | def test_jpg(self): |
| 25 | p = images_pixbuf.JPGParser('./tests/data/dirty.jpg') | 25 | p = images.JPGParser('./tests/data/dirty.jpg') |
| 26 | meta = p.get_meta() | 26 | meta = p.get_meta() |
| 27 | self.assertEqual(meta['Comment'], 'Created with GIMP') | 27 | self.assertEqual(meta['Comment'], 'Created with GIMP') |
| 28 | 28 | ||
| 29 | def test_tiff(self): | 29 | def test_tiff(self): |
| 30 | p = images_pixbuf.JPGParser('./tests/data/dirty.tiff') | 30 | p = images.JPGParser('./tests/data/dirty.tiff') |
| 31 | meta = p.get_meta() | 31 | meta = p.get_meta() |
| 32 | self.assertEqual(meta['Make'], 'OLYMPUS IMAGING CORP.') | 32 | self.assertEqual(meta['Make'], 'OLYMPUS IMAGING CORP.') |
| 33 | self.assertEqual(meta['Model'], 'C7070WZ') | 33 | self.assertEqual(meta['Model'], 'C7070WZ') |
| @@ -144,7 +144,7 @@ class TestCleaning(unittest.TestCase): | |||
| 144 | 144 | ||
| 145 | def test_png(self): | 145 | def test_png(self): |
| 146 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.png') | 146 | shutil.copy('./tests/data/dirty.png', './tests/data/clean.png') |
| 147 | p = png.PNGParser('./tests/data/clean.png') | 147 | p = images.PNGParser('./tests/data/clean.png') |
| 148 | 148 | ||
| 149 | meta = p.get_meta() | 149 | meta = p.get_meta() |
| 150 | self.assertEqual(meta['Comment'], 'This is a comment, be careful!') | 150 | self.assertEqual(meta['Comment'], 'This is a comment, be careful!') |
| @@ -152,14 +152,14 @@ class TestCleaning(unittest.TestCase): | |||
| 152 | ret = p.remove_all() | 152 | ret = p.remove_all() |
| 153 | self.assertTrue(ret) | 153 | self.assertTrue(ret) |
| 154 | 154 | ||
| 155 | p = png.PNGParser('./tests/data/clean.png.cleaned') | 155 | p = images.PNGParser('./tests/data/clean.png.cleaned') |
| 156 | self.assertEqual(p.get_meta(), {}) | 156 | self.assertEqual(p.get_meta(), {}) |
| 157 | 157 | ||
| 158 | os.remove('./tests/data/clean.png') | 158 | os.remove('./tests/data/clean.png') |
| 159 | 159 | ||
| 160 | def test_jpg(self): | 160 | def test_jpg(self): |
| 161 | shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg') | 161 | shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg') |
| 162 | p = images_pixbuf.JPGParser('./tests/data/clean.jpg') | 162 | p = images.JPGParser('./tests/data/clean.jpg') |
| 163 | 163 | ||
| 164 | meta = p.get_meta() | 164 | meta = p.get_meta() |
| 165 | self.assertEqual(meta['Comment'], 'Created with GIMP') | 165 | self.assertEqual(meta['Comment'], 'Created with GIMP') |
| @@ -167,7 +167,7 @@ class TestCleaning(unittest.TestCase): | |||
| 167 | ret = p.remove_all() | 167 | ret = p.remove_all() |
| 168 | self.assertTrue(ret) | 168 | self.assertTrue(ret) |
| 169 | 169 | ||
| 170 | p = images_pixbuf.JPGParser('./tests/data/clean.jpg.cleaned') | 170 | p = images.JPGParser('./tests/data/clean.jpg.cleaned') |
| 171 | self.assertEqual(p.get_meta(), {}) | 171 | self.assertEqual(p.get_meta(), {}) |
| 172 | 172 | ||
| 173 | os.remove('./tests/data/clean.jpg') | 173 | os.remove('./tests/data/clean.jpg') |
| @@ -250,7 +250,7 @@ class TestCleaning(unittest.TestCase): | |||
| 250 | 250 | ||
| 251 | def test_tiff(self): | 251 | def test_tiff(self): |
| 252 | shutil.copy('./tests/data/dirty.tiff', './tests/data/clean.tiff') | 252 | shutil.copy('./tests/data/dirty.tiff', './tests/data/clean.tiff') |
| 253 | p = images_pixbuf.TiffParser('./tests/data/clean.tiff') | 253 | p = images.TiffParser('./tests/data/clean.tiff') |
| 254 | 254 | ||
| 255 | meta = p.get_meta() | 255 | meta = p.get_meta() |
| 256 | self.assertEqual(meta['Model'], 'C7070WZ') | 256 | self.assertEqual(meta['Model'], 'C7070WZ') |
| @@ -258,7 +258,7 @@ class TestCleaning(unittest.TestCase): | |||
| 258 | ret = p.remove_all() | 258 | ret = p.remove_all() |
| 259 | self.assertTrue(ret) | 259 | self.assertTrue(ret) |
| 260 | 260 | ||
| 261 | p = images_pixbuf.TiffParser('./tests/data/clean.tiff.cleaned') | 261 | p = images.TiffParser('./tests/data/clean.tiff.cleaned') |
| 262 | self.assertEqual(p.get_meta(), {}) | 262 | self.assertEqual(p.get_meta(), {}) |
| 263 | 263 | ||
| 264 | os.remove('./tests/data/clean.tiff') | 264 | os.remove('./tests/data/clean.tiff') |
