From d9e1e6bf7c457f40217cc218fdd265b20e9a1bb0 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 25 Jan 2021 14:50:39 -0800 Subject: [PATCH] Add bytes_per_second to hash_file. --- voussoirkit/spinal.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/voussoirkit/spinal.py b/voussoirkit/spinal.py index 127aa63..908bbcc 100644 --- a/voussoirkit/spinal.py +++ b/voussoirkit/spinal.py @@ -546,10 +546,14 @@ def hash_file( path, hash_class, *, + bytes_per_second=None, callback_progress=None, chunk_size=CHUNK_SIZE, ): ''' + hash_class: + Should be a hashlib class or a callable that returns an instance of one. + callback_progress: A function that takes three parameters: path object, bytes ingested so far, bytes total @@ -557,21 +561,29 @@ def hash_file( path = pathclass.Path(path) path.assert_is_file() hasher = hash_class() + + bytes_per_second = limiter_or_none(bytes_per_second) + callback_progress = callback_progress or do_nothing + checked_bytes = 0 file_size = path.size - callback_progress = callback_progress or do_nothing - handle = path.open('rb') with handle: while True: chunk = handle.read(chunk_size) if not chunk: break + + this_size = len(chunk) hasher.update(chunk) - checked_bytes += len(chunk) + + checked_bytes += this_size callback_progress(path, checked_bytes, file_size) + if bytes_per_second is not None: + bytes_per_second.limit(this_size) + return hasher def is_xor(*args):