From d95d1c95330b25d04c36b678add5540b318c5eb7 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 9 Sep 2020 14:34:50 -0700 Subject: [PATCH] Add matches_handle, matches_file. --- voussoirkit/quickid.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/voussoirkit/quickid.py b/voussoirkit/quickid.py index b985bb6..81c663d 100644 --- a/voussoirkit/quickid.py +++ b/voussoirkit/quickid.py @@ -37,6 +37,24 @@ def equal_file(filename1, filename2, *args, **kwargs): with open(filename1, 'rb') as handle1, open(filename2, 'rb') as handle2: return equal_handle(handle1, handle2, *args, **kwargs) +def matches_handle(handle, other_id): + (other_size, hashtype, chunk_size, other_hash) = other_id.split('_') + other_size = int(other_size) + chunk_size = int(chunk_size) + + this_size = handle.seek(0, SEEK_END) + handle.seek(0) + if this_size != other_size: + return False + + this_id = quickid_handle(handle, hashtype=hashtype, chunk_size=chunk_size) + return this_id == other_id + +def matches_file(filename, other_id): + filename = pathclass.Path(filename).absolute_path + with open(filename, 'rb') as handle: + return matches_handle(handle, other_id) + def quickid_handle(handle, hashtype='md5', chunk_size=None): if chunk_size is None: chunk_size = CHUNK_SIZE