summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libmat2/images.py26
-rw-r--r--tests/data/dirty.ppm8
-rw-r--r--tests/test_libmat2.py29
3 files changed, 61 insertions, 2 deletions
diff --git a/libmat2/images.py b/libmat2/images.py
index 2781e05..18fe4d3 100644
--- a/libmat2/images.py
+++ b/libmat2/images.py
@@ -1,6 +1,7 @@
1import imghdr 1import imghdr
2import os 2import os
3from typing import Set, Dict, Union 3import re
4from typing import Set, Dict, Union, Any
4 5
5import cairo 6import cairo
6 7
@@ -9,10 +10,11 @@ gi.require_version('GdkPixbuf', '2.0')
9gi.require_version('Rsvg', '2.0') 10gi.require_version('Rsvg', '2.0')
10from gi.repository import GdkPixbuf, GLib, Rsvg 11from gi.repository import GdkPixbuf, GLib, Rsvg
11 12
12from . import exiftool 13from . import exiftool, abstract
13 14
14# Make pyflakes happy 15# Make pyflakes happy
15assert Set 16assert Set
17assert Any
16 18
17class SVGParser(exiftool.ExiftoolParser): 19class SVGParser(exiftool.ExiftoolParser):
18 mimetypes = {'image/svg+xml', } 20 mimetypes = {'image/svg+xml', }
@@ -138,3 +140,23 @@ class TiffParser(GdkPixbufAbstractParser):
138 'FilePermissions', 'FileSize', 'FileType', 140 'FilePermissions', 'FileSize', 'FileType',
139 'FileTypeExtension', 'ImageHeight', 'ImageSize', 141 'FileTypeExtension', 'ImageHeight', 'ImageSize',
140 'ImageWidth', 'MIMEType', 'Megapixels', 'SourceFile'} 142 'ImageWidth', 'MIMEType', 'Megapixels', 'SourceFile'}
143
144class PPMParser(abstract.AbstractParser):
145 mimetypes = {'image/x-portable-pixmap'}
146
147 def get_meta(self) -> Dict[str, Union[str, dict]]:
148 meta = {} # type: Dict[str, Union[str, Dict[Any, Any]]]
149 with open(self.filename) as f:
150 for idx, line in enumerate(f):
151 if line.lstrip().startswith('#'):
152 meta[str(idx)] = line.lstrip().rstrip()
153 return meta
154
155 def remove_all(self) -> bool:
156 with open(self.filename) as fin:
157 with open(self.output_filename, 'w') as fout:
158 for line in fin:
159 if not line.lstrip().startswith('#'):
160 line = re.sub(r"\s+", "", line, flags=re.UNICODE)
161 fout.write(line)
162 return True
diff --git a/tests/data/dirty.ppm b/tests/data/dirty.ppm
new file mode 100644
index 0000000..d658cd3
--- /dev/null
+++ b/tests/data/dirty.ppm
@@ -0,0 +1,8 @@
1P3
2# A metadata
33 2 1
41 0 1 0 1 0 0 0 1
5# And an other one
61 1 0 1 0 1 1 0 0
7 # and a final one here
8
diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py
index 3fba36a..d596ff2 100644
--- a/tests/test_libmat2.py
+++ b/tests/test_libmat2.py
@@ -113,6 +113,14 @@ class TestGetMeta(unittest.TestCase):
113 meta = p.get_meta() 113 meta = p.get_meta()
114 self.assertEqual(meta['Comment'], 'Created with GIMP') 114 self.assertEqual(meta['Comment'], 'Created with GIMP')
115 115
116 def test_ppm(self):
117 p = images.PPMParser('./tests/data/dirty.ppm')
118 meta = p.get_meta()
119 self.assertEqual(meta['1'], '# A metadata')
120 self.assertEqual(meta['4'], '# And an other one')
121 self.assertEqual(meta['6'], '# and a final one here')
122
123
116 def test_tiff(self): 124 def test_tiff(self):
117 p = images.TiffParser('./tests/data/dirty.tiff') 125 p = images.TiffParser('./tests/data/dirty.tiff')
118 meta = p.get_meta() 126 meta = p.get_meta()
@@ -887,3 +895,24 @@ class TestCleaning(unittest.TestCase):
887 895
888 p = images.SVGParser('./tests/data/weird.svg') 896 p = images.SVGParser('./tests/data/weird.svg')
889 self.assertEqual(p.get_meta()['Xmlns'], 'http://www.w3.org/1337/svg') 897 self.assertEqual(p.get_meta()['Xmlns'], 'http://www.w3.org/1337/svg')
898
899 def test_ppm(self):
900 shutil.copy('./tests/data/dirty.ppm', './tests/data/clean.ppm')
901 p = images.PPMParser('./tests/data/clean.ppm')
902
903 meta = p.get_meta()
904 print(meta)
905 self.assertEqual(meta['1'], '# A metadata')
906
907 ret = p.remove_all()
908 self.assertTrue(ret)
909
910 p = images.PPMParser('./tests/data/clean.cleaned.ppm')
911 self.assertEqual(p.get_meta(), {})
912 self.assertTrue(p.remove_all())
913
914 os.remove('./tests/data/clean.ppm')
915 os.remove('./tests/data/clean.cleaned.ppm')
916 os.remove('./tests/data/clean.cleaned.cleaned.ppm')
917
918