Skip to content

Commit 9726351

Browse files
committed
Improve codecov
1 parent 50ec3b8 commit 9726351

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

hb/main.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sys import stderr
88
from threading import Thread
99
from time import sleep
10-
from typing import Dict, IO, List, Tuple
10+
from typing import Dict, IO, List, Optional, Tuple
1111

1212

1313
class Checksum():
@@ -44,21 +44,26 @@ def __init__(self, path: str, threshold: int = 200) -> None:
4444
self.filesize = self._path.stat().st_size
4545
self.threshold = threshold
4646

47-
def _progress(self, file: IO) -> None:
47+
def _progress(self, file: IO) -> Optional[Thread]:
4848
def _p(file: IO) -> None:
4949
while not file.closed:
5050
print(f"{int(file.tell() / self.filesize * 100)}%", end="\r", file=stderr)
5151
sleep(0.2)
5252
print(" ", end="\r", file=stderr) # clear the progress display
53+
thread = None
5354
if self.filesize > self.threshold * 1024 * 1024:
54-
Thread(target=_p, args=(file,)).start()
55+
thread = Thread(target=_p, args=(file,))
56+
thread.start()
57+
return thread
5558

5659
def _hashlib_compute(self, name: str) -> str:
5760
result = hashlib.new(name)
5861
with self._path.open("rb") as lines:
59-
self._progress(lines)
62+
thread = self._progress(lines)
6063
for line in lines:
6164
result.update(line)
65+
if thread:
66+
thread.join()
6267
return result.hexdigest()
6368

6469
def _zlib_compute(self, name: str) -> str:
@@ -69,9 +74,11 @@ def _zlib_compute(self, name: str) -> str:
6974
result = 0
7075
update = zlib.crc32
7176
with self._path.open("rb") as lines:
72-
self._progress(lines)
77+
thread = self._progress(lines)
7378
for line in lines:
7479
result = update(line, result)
80+
if thread:
81+
thread.join()
7582
return hex(result)[2:].zfill(8)
7683

7784
def compute(self, algorithm: str) -> str:

0 commit comments

Comments
 (0)