1
0
mirror of synced 2024-11-25 18:54:22 +01:00

Move every image-related parser into a single file

This commit is contained in:
jvoisin 2018-04-01 12:30:00 +02:00
parent 711347c87f
commit 27beda354d
3 changed files with 33 additions and 37 deletions

View File

@ -2,12 +2,35 @@ import subprocess
import json import json
import os import os
import cairo
import gi import gi
gi.require_version('GdkPixbuf', '2.0') gi.require_version('GdkPixbuf', '2.0')
from gi.repository import GdkPixbuf from gi.repository import GdkPixbuf
from . import abstract 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): class GdkPixbufAbstractParser(abstract.AbstractParser):
def get_meta(self): def get_meta(self):
out = subprocess.check_output(['exiftool', '-json', self.filename]) out = subprocess.check_output(['exiftool', '-json', self.filename])

View File

@ -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

View File

@ -6,7 +6,7 @@ import os
import zipfile import zipfile
import tempfile 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): class TestGetMeta(unittest.TestCase):
def test_pdf(self): 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'") self.assertEqual(meta['creator'], "'Certified by IEEE PDFeXpress at 03/19/2016 2:56:07 AM'")
def test_png(self): def test_png(self):
p = png.PNGParser('./tests/data/dirty.png') p = images.PNGParser('./tests/data/dirty.png')
meta = p.get_meta() meta = p.get_meta()
self.assertEqual(meta['Comment'], 'This is a comment, be careful!') self.assertEqual(meta['Comment'], 'This is a comment, be careful!')
self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25") self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25")
def test_jpg(self): def test_jpg(self):
p = images_pixbuf.JPGParser('./tests/data/dirty.jpg') p = images.JPGParser('./tests/data/dirty.jpg')
meta = p.get_meta() meta = p.get_meta()
self.assertEqual(meta['Comment'], 'Created with GIMP') self.assertEqual(meta['Comment'], 'Created with GIMP')
def test_tiff(self): def test_tiff(self):
p = images_pixbuf.JPGParser('./tests/data/dirty.tiff') p = images.JPGParser('./tests/data/dirty.tiff')
meta = p.get_meta() meta = p.get_meta()
self.assertEqual(meta['Make'], 'OLYMPUS IMAGING CORP.') self.assertEqual(meta['Make'], 'OLYMPUS IMAGING CORP.')
self.assertEqual(meta['Model'], 'C7070WZ') self.assertEqual(meta['Model'], 'C7070WZ')
@ -144,7 +144,7 @@ class TestCleaning(unittest.TestCase):
def test_png(self): def test_png(self):
shutil.copy('./tests/data/dirty.png', './tests/data/clean.png') 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() meta = p.get_meta()
self.assertEqual(meta['Comment'], 'This is a comment, be careful!') self.assertEqual(meta['Comment'], 'This is a comment, be careful!')
@ -152,14 +152,14 @@ class TestCleaning(unittest.TestCase):
ret = p.remove_all() ret = p.remove_all()
self.assertTrue(ret) self.assertTrue(ret)
p = png.PNGParser('./tests/data/clean.png.cleaned') p = images.PNGParser('./tests/data/clean.png.cleaned')
self.assertEqual(p.get_meta(), {}) self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.png') os.remove('./tests/data/clean.png')
def test_jpg(self): def test_jpg(self):
shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg') 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() meta = p.get_meta()
self.assertEqual(meta['Comment'], 'Created with GIMP') self.assertEqual(meta['Comment'], 'Created with GIMP')
@ -167,7 +167,7 @@ class TestCleaning(unittest.TestCase):
ret = p.remove_all() ret = p.remove_all()
self.assertTrue(ret) 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(), {}) self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.jpg') os.remove('./tests/data/clean.jpg')
@ -250,7 +250,7 @@ class TestCleaning(unittest.TestCase):
def test_tiff(self): def test_tiff(self):
shutil.copy('./tests/data/dirty.tiff', './tests/data/clean.tiff') 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() meta = p.get_meta()
self.assertEqual(meta['Model'], 'C7070WZ') self.assertEqual(meta['Model'], 'C7070WZ')
@ -258,7 +258,7 @@ class TestCleaning(unittest.TestCase):
ret = p.remove_all() ret = p.remove_all()
self.assertTrue(ret) 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(), {}) self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.tiff') os.remove('./tests/data/clean.tiff')