From c2b5909eff090ebb3f19ab88308f0cc7b913157e Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Wed, 29 Dec 2021 15:24:34 +0100 Subject: ImageHash: gain a name property Instead of retroactively attaching a name to an ImageHash, autogenerate it via a property. Doing so also simplifies static type checking. --- dedup/image.py | 6 ++++++ importpkg.py | 10 ++-------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/dedup/image.py b/dedup/image.py index 2e64e6b..91321f4 100644 --- a/dedup/image.py +++ b/dedup/image.py @@ -69,9 +69,14 @@ class ImageHash: self.content.seek(pos) return "%s%8.8x%8.8x" % (hashobj.hexdigest(), width, height) + @property + def name(self): + return self.name_prefix + self.hashobj.name + class PNGHash(ImageHash): """A hash on the contents of a PNG image.""" + name_prefix = "png_" def detect(self): if self.content.tell() < 33: # header + IHDR @@ -86,6 +91,7 @@ class PNGHash(ImageHash): class GIFHash(ImageHash): """A hash on the contents of the first frame of a GIF image.""" + name_prefix = "gif_" def detect(self): if self.content.tell() < 10: # magic + logical dimension diff --git a/importpkg.py b/importpkg.py index 4693401..6988c1d 100755 --- a/importpkg.py +++ b/importpkg.py @@ -31,16 +31,10 @@ def gziphash(): return HashBlacklistContent(hashobj, boring_content) def pnghash(): - hashobj = PNGHash(hashlib.sha512()) - hashobj = SuppressingHash(hashobj, (ValueError,)) - hashobj.name = "png_sha512" - return hashobj + return SuppressingHash(PNGHash(hashlib.sha512()), (ValueError,)) def gifhash(): - hashobj = GIFHash(hashlib.sha512()) - hashobj = SuppressingHash(hashobj, (ValueError,)) - hashobj.name = "gif_sha512" - return hashobj + return SuppressingHash(GIFHash(hashlib.sha512()), (ValueError,)) class ProcessingFinished(Exception): pass -- cgit v1.2.3