From 6309d68b7b5c054980eeac70c5b6941a5e83b35b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Gia=20Phong?= Date: Tue, 23 Jun 2020 00:46:42 +0700 Subject: [PATCH] Correct mode and clean up --- lazip.py | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/lazip.py b/lazip.py index a96428b..16b7b19 100644 --- a/lazip.py +++ b/lazip.py @@ -18,7 +18,7 @@ """Lazy ZIP over HTTP""" -__version__ = '0.0.3' +__version__ = '0.0.4' __all__ = ['Filazy', 'Lazip'] from abc import abstractmethod @@ -33,16 +33,6 @@ from requests import Session from requests.models import CONTENT_CHUNK_SIZE, Response -def init_range(stop: int, size: int) -> Iterator[Tuple[int, int]]: - """Return an iterator of intervals to fetch a file reversedly.""" - start = stop - size - while start > 0: - yield start, stop-1 - stop = start - start -= size - yield 0, stop-1 - - class ReadOnlyBinaryIOWrapper(IO[bytes]): """Wrapper for a read-only binary I/O.""" @@ -51,8 +41,8 @@ class ReadOnlyBinaryIOWrapper(IO[bytes]): @property def mode(self) -> str: - """Opening mode, which is always w+b.""" - return self.file.mode + """Opening mode, which is always rb.""" + return 'rb' @property def name(self) -> str: @@ -259,12 +249,12 @@ class Lazip(Filazy): def __post_init__(self) -> None: """Check and download until the file is a valid ZIP.""" + end = self.length - 1 if not self.accept_ranges: - end = self.length - 1 self.ensure(0, end) self.left, self.right = [0], [end] return - for start, end in init_range(self.length, self.chunk_size): + for start in reversed(range(0, end, self.chunk_size)): self.ensure(start, end) try: ZipFile(self)