1
0
mirror of synced 2024-11-05 17:08:52 +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 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])

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 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')