From 27beda354d8b78c1716e659273c180d4ddfb144b Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 1 Apr 2018 12:30:00 +0200 Subject: [PATCH] Move every image-related parser into a single file --- src/{images_pixbuf.py => images.py} | 23 +++++++++++++++++++++++ src/png.py | 27 --------------------------- tests/test_libmat2.py | 20 ++++++++++---------- 3 files changed, 33 insertions(+), 37 deletions(-) rename src/{images_pixbuf.py => images.py} (68%) delete mode 100644 src/png.py diff --git a/src/images_pixbuf.py b/src/images.py similarity index 68% rename from src/images_pixbuf.py rename to src/images.py index 8eeffbe..560886a 100644 --- a/src/images_pixbuf.py +++ b/src/images.py @@ -2,12 +2,35 @@ import subprocess import json import os +import cairo + import gi gi.require_version('GdkPixbuf', '2.0') from gi.repository import GdkPixbuf from . import abstract +class PNGParser(abstract.AbstractParser): + mimetypes = {'image/png', } + meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', + 'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate', + "FileInodeChangeDate", 'FilePermissions', 'FileType', + 'FileTypeExtension', 'MIMEType', 'ImageWidth', 'BitDepth', 'ColorType', + 'Compression', 'Filter', 'Interlace', 'BackgroundColor', 'ImageSize', + 'Megapixels', 'ImageHeight'} + + def get_meta(self): + out = subprocess.check_output(['exiftool', '-json', self.filename]) + meta = json.loads(out.decode('utf-8'))[0] + for key in self.meta_whitelist: + meta.pop(key, None) + return meta + + def remove_all(self): + surface = cairo.ImageSurface.create_from_png(self.filename) + surface.write_to_png(self.output_filename) + return True + class GdkPixbufAbstractParser(abstract.AbstractParser): def get_meta(self): 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 @@ -import subprocess -import json - -import cairo - -from . import abstract - -class PNGParser(abstract.AbstractParser): - mimetypes = {'image/png', } - meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', - 'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate', - "FileInodeChangeDate", 'FilePermissions', 'FileType', - 'FileTypeExtension', 'MIMEType', 'ImageWidth', 'BitDepth', 'ColorType', - 'Compression', 'Filter', 'Interlace', 'BackgroundColor', 'ImageSize', - 'Megapixels', 'ImageHeight'} - - def get_meta(self): - out = subprocess.check_output(['exiftool', '-json', self.filename]) - meta = json.loads(out.decode('utf-8'))[0] - for key in self.meta_whitelist: - meta.pop(key, None) - return meta - - def remove_all(self): - surface = cairo.ImageSurface.create_from_png(self.filename) - surface.write_to_png(self.output_filename) - 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 import zipfile import tempfile -from src import pdf, png, images_pixbuf, audio, office, parser_factory +from src import pdf, images, audio, office, parser_factory class TestGetMeta(unittest.TestCase): def test_pdf(self): @@ -16,18 +16,18 @@ class TestGetMeta(unittest.TestCase): self.assertEqual(meta['creator'], "'Certified by IEEE PDFeXpress at 03/19/2016 2:56:07 AM'") def test_png(self): - p = png.PNGParser('./tests/data/dirty.png') + p = images.PNGParser('./tests/data/dirty.png') meta = p.get_meta() self.assertEqual(meta['Comment'], 'This is a comment, be careful!') self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25") def test_jpg(self): - p = images_pixbuf.JPGParser('./tests/data/dirty.jpg') + p = images.JPGParser('./tests/data/dirty.jpg') meta = p.get_meta() self.assertEqual(meta['Comment'], 'Created with GIMP') def test_tiff(self): - p = images_pixbuf.JPGParser('./tests/data/dirty.tiff') + p = images.JPGParser('./tests/data/dirty.tiff') meta = p.get_meta() self.assertEqual(meta['Make'], 'OLYMPUS IMAGING CORP.') self.assertEqual(meta['Model'], 'C7070WZ') @@ -144,7 +144,7 @@ class TestCleaning(unittest.TestCase): def test_png(self): shutil.copy('./tests/data/dirty.png', './tests/data/clean.png') - p = png.PNGParser('./tests/data/clean.png') + p = images.PNGParser('./tests/data/clean.png') meta = p.get_meta() self.assertEqual(meta['Comment'], 'This is a comment, be careful!') @@ -152,14 +152,14 @@ class TestCleaning(unittest.TestCase): ret = p.remove_all() self.assertTrue(ret) - p = png.PNGParser('./tests/data/clean.png.cleaned') + p = images.PNGParser('./tests/data/clean.png.cleaned') self.assertEqual(p.get_meta(), {}) os.remove('./tests/data/clean.png') def test_jpg(self): shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg') - p = images_pixbuf.JPGParser('./tests/data/clean.jpg') + p = images.JPGParser('./tests/data/clean.jpg') meta = p.get_meta() self.assertEqual(meta['Comment'], 'Created with GIMP') @@ -167,7 +167,7 @@ class TestCleaning(unittest.TestCase): ret = p.remove_all() self.assertTrue(ret) - p = images_pixbuf.JPGParser('./tests/data/clean.jpg.cleaned') + p = images.JPGParser('./tests/data/clean.jpg.cleaned') self.assertEqual(p.get_meta(), {}) os.remove('./tests/data/clean.jpg') @@ -250,7 +250,7 @@ class TestCleaning(unittest.TestCase): def test_tiff(self): shutil.copy('./tests/data/dirty.tiff', './tests/data/clean.tiff') - p = images_pixbuf.TiffParser('./tests/data/clean.tiff') + p = images.TiffParser('./tests/data/clean.tiff') meta = p.get_meta() self.assertEqual(meta['Model'], 'C7070WZ') @@ -258,7 +258,7 @@ class TestCleaning(unittest.TestCase): ret = p.remove_all() self.assertTrue(ret) - p = images_pixbuf.TiffParser('./tests/data/clean.tiff.cleaned') + p = images.TiffParser('./tests/data/clean.tiff.cleaned') self.assertEqual(p.get_meta(), {}) os.remove('./tests/data/clean.tiff')