Skip to content

Commit f91182a

Browse files
committed
Replace required_length with max_length
Rename donwload* methods argument in fetcher.py to max_length to better match its purpose. Docstring improvements. Signed-off-by: Teodora Sechkova <tsechkova@vmware.com>
1 parent 1596de2 commit f91182a

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

tuf/ngclient/fetcher.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,20 @@ def fetch(self, url: str, required_length: int) -> Iterator[bytes]:
5050
raise NotImplementedError # pragma: no cover
5151

5252
@contextmanager
53-
def download_file(self, url: str, required_length: int) -> Iterator[IO]:
53+
def download_file(self, url: str, max_length: int) -> Iterator[IO]:
5454
"""Opens a connection to 'url' and downloads the content
55-
up to 'required_length'.
55+
up to 'max_length'.
5656
5757
Args:
5858
url: a URL string that represents the location of the file.
59-
required_length: an integer value representing the length of
60-
the file or an upper boundary.
59+
max_length: an integer value representing the length of
60+
the file or an upper bound.
6161
6262
Raises:
63-
DownloadLengthMismatchError: a mismatch of observed vs expected
64-
lengths while downloading the file.
63+
DownloadLengthMismatchError: downloaded bytes exceed 'max_length'.
6564
6665
Yields:
67-
A file object that points to the contents of 'url'.
66+
A TemporaryFile object that points to the contents of 'url'.
6867
"""
6968
# 'url.replace('\\', '/')' is needed for compatibility with
7069
# Windows-based systems, because they might use back-slashes in place
@@ -78,21 +77,21 @@ def download_file(self, url: str, required_length: int) -> Iterator[IO]:
7877
number_of_bytes_received = 0
7978

8079
with tempfile.TemporaryFile() as temp_file:
81-
chunks = self.fetch(url, required_length)
80+
chunks = self.fetch(url, max_length)
8281
for chunk in chunks:
8382
temp_file.write(chunk)
8483
number_of_bytes_received += len(chunk)
85-
if number_of_bytes_received > required_length:
84+
if number_of_bytes_received > max_length:
8685
raise exceptions.DownloadLengthMismatchError(
87-
required_length, number_of_bytes_received
86+
max_length, number_of_bytes_received
8887
)
8988
temp_file.seek(0)
9089
yield temp_file
9190

92-
def download_bytes(self, url: str, required_length: int) -> bytes:
91+
def download_bytes(self, url: str, max_length: int) -> bytes:
9392
"""Download bytes from given url
9493
9594
Returns the downloaded bytes, otherwise like download_file()
9695
"""
97-
with self.download_file(url, required_length) as dl_file:
96+
with self.download_file(url, max_length) as dl_file:
9897
return dl_file.read()

0 commit comments

Comments
 (0)