diff options
| -rw-r--r-- | libmat2/images.py | 12 | ||||
| -rw-r--r-- | tests/test_lightweigh_cleaning.py | 19 |
2 files changed, 27 insertions, 4 deletions
diff --git a/libmat2/images.py b/libmat2/images.py index 03cecd3..153a83d 100644 --- a/libmat2/images.py +++ b/libmat2/images.py | |||
| @@ -6,7 +6,7 @@ import cairo | |||
| 6 | 6 | ||
| 7 | import gi | 7 | import gi |
| 8 | gi.require_version('GdkPixbuf', '2.0') | 8 | gi.require_version('GdkPixbuf', '2.0') |
| 9 | from gi.repository import GdkPixbuf | 9 | from gi.repository import GdkPixbuf, GLib |
| 10 | 10 | ||
| 11 | from . import exiftool | 11 | from . import exiftool |
| 12 | 12 | ||
| @@ -50,15 +50,21 @@ class GdkPixbufAbstractParser(exiftool.ExiftoolParser): | |||
| 50 | 50 | ||
| 51 | def __init__(self, filename): | 51 | def __init__(self, filename): |
| 52 | super().__init__(filename) | 52 | super().__init__(filename) |
| 53 | if imghdr.what(filename) != self._type: # better safe than sorry | 53 | # we can't use imghdr here because of https://bugs.python.org/issue28591 |
| 54 | try: | ||
| 55 | GdkPixbuf.Pixbuf.new_from_file(self.filename) | ||
| 56 | except GLib.GError: | ||
| 54 | raise ValueError | 57 | raise ValueError |
| 55 | 58 | ||
| 56 | def remove_all(self) -> bool: | 59 | def remove_all(self) -> bool: |
| 60 | if self.lightweight_cleaning: | ||
| 61 | return self._lightweight_cleanup() | ||
| 62 | |||
| 57 | _, extension = os.path.splitext(self.filename) | 63 | _, extension = os.path.splitext(self.filename) |
| 58 | pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) | 64 | pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) |
| 59 | if extension.lower() == '.jpg': | 65 | if extension.lower() == '.jpg': |
| 60 | extension = '.jpeg' # gdk is picky | 66 | extension = '.jpeg' # gdk is picky |
| 61 | pixbuf.savev(self.output_filename, extension[1:], [], []) | 67 | pixbuf.savev(self.output_filename, type=extension[1:], option_keys=[], option_values=[]) |
| 62 | return True | 68 | return True |
| 63 | 69 | ||
| 64 | 70 | ||
diff --git a/tests/test_lightweigh_cleaning.py b/tests/test_lightweigh_cleaning.py index 7af31ad..5fd7ad6 100644 --- a/tests/test_lightweigh_cleaning.py +++ b/tests/test_lightweigh_cleaning.py | |||
| @@ -4,7 +4,7 @@ import unittest | |||
| 4 | import shutil | 4 | import shutil |
| 5 | import os | 5 | import os |
| 6 | 6 | ||
| 7 | from libmat2 import pdf, images | 7 | from libmat2 import pdf, images, torrent |
| 8 | 8 | ||
| 9 | class TestLightWeightCleaning(unittest.TestCase): | 9 | class TestLightWeightCleaning(unittest.TestCase): |
| 10 | def test_pdf(self): | 10 | def test_pdf(self): |
| @@ -63,3 +63,20 @@ class TestLightWeightCleaning(unittest.TestCase): | |||
| 63 | 63 | ||
| 64 | os.remove('./tests/data/clean.jpg') | 64 | os.remove('./tests/data/clean.jpg') |
| 65 | os.remove('./tests/data/clean.cleaned.jpg') | 65 | os.remove('./tests/data/clean.cleaned.jpg') |
| 66 | |||
| 67 | def test_torrent(self): | ||
| 68 | shutil.copy('./tests/data/dirty.torrent', './tests/data/clean.torrent') | ||
| 69 | p = torrent.TorrentParser('./tests/data/clean.torrent') | ||
| 70 | |||
| 71 | meta = p.get_meta() | ||
| 72 | self.assertEqual(meta['created by'], b'mktorrent 1.0') | ||
| 73 | |||
| 74 | p.lightweight_cleaning = True | ||
| 75 | ret = p.remove_all() | ||
| 76 | self.assertTrue(ret) | ||
| 77 | |||
| 78 | p = torrent.TorrentParser('./tests/data/clean.cleaned.torrent') | ||
| 79 | self.assertEqual(p.get_meta(), {}) | ||
| 80 | |||
| 81 | os.remove('./tests/data/clean.torrent') | ||
| 82 | os.remove('./tests/data/clean.cleaned.torrent') | ||
