diff options
| author | jvoisin | 2019-09-01 09:28:46 -0700 |
|---|---|---|
| committer | jvoisin | 2019-09-01 09:28:46 -0700 |
| commit | 397a18b0cc6453c9d72ce2cd110f3724ac809313 (patch) | |
| tree | e87174f3cbeeb4be071bf5deb2fc55a62f757ec1 | |
| parent | fc924239febb3f186585d9ea6c263e1cb7dc690d (diff) | |
Add support for ppm
| -rw-r--r-- | libmat2/images.py | 26 | ||||
| -rw-r--r-- | tests/data/dirty.ppm | 8 | ||||
| -rw-r--r-- | tests/test_libmat2.py | 29 |
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 @@ | |||
| 1 | import imghdr | 1 | import imghdr |
| 2 | import os | 2 | import os |
| 3 | from typing import Set, Dict, Union | 3 | import re |
| 4 | from typing import Set, Dict, Union, Any | ||
| 4 | 5 | ||
| 5 | import cairo | 6 | import cairo |
| 6 | 7 | ||
| @@ -9,10 +10,11 @@ gi.require_version('GdkPixbuf', '2.0') | |||
| 9 | gi.require_version('Rsvg', '2.0') | 10 | gi.require_version('Rsvg', '2.0') |
| 10 | from gi.repository import GdkPixbuf, GLib, Rsvg | 11 | from gi.repository import GdkPixbuf, GLib, Rsvg |
| 11 | 12 | ||
| 12 | from . import exiftool | 13 | from . import exiftool, abstract |
| 13 | 14 | ||
| 14 | # Make pyflakes happy | 15 | # Make pyflakes happy |
| 15 | assert Set | 16 | assert Set |
| 17 | assert Any | ||
| 16 | 18 | ||
| 17 | class SVGParser(exiftool.ExiftoolParser): | 19 | class 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 | |||
| 144 | class 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 @@ | |||
| 1 | P3 | ||
| 2 | # A metadata | ||
| 3 | 3 2 1 | ||
| 4 | 1 0 1 0 1 0 0 0 1 | ||
| 5 | # And an other one | ||
| 6 | 1 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 | |||
