Skip to content

Commit 0eff004

Browse files
committed
Replace required_length with max_length
Rename required_length argument in fetcher to max_length to better match its purpose. Docstring improvements. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 1596de2 commit 0eff004

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

tuf/ngclient/_internal/requests_fetcher.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ def __init__(self):
5353
self.chunk_size: int = 400000 # bytes
5454
self.sleep_before_round: Optional[int] = None
5555

56-
def fetch(self, url: str, required_length: int) -> Iterator[bytes]:
56+
def fetch(self, url: str, max_length: int) -> Iterator[bytes]:
5757
"""Fetches the contents of HTTP/HTTPS url from a remote server.
5858
59-
Ensures the length of the downloaded data is up to 'required_length'.
59+
Ensures the length of the downloaded data is up to 'max_length'.
6060
6161
Arguments:
6262
url: A URL string that represents a file location.
63-
required_length: An integer value representing the file length in
64-
bytes.
63+
max_length: An integer value representing the maximum
64+
number of bytes to be downloaded.
6565
6666
Raises:
6767
exceptions.SlowRetrievalError: A timeout occurs while receiving
@@ -90,10 +90,10 @@ def fetch(self, url: str, required_length: int) -> Iterator[bytes]:
9090
status = e.response.status_code
9191
raise exceptions.FetcherHTTPError(str(e), status)
9292

93-
return self._chunks(response, required_length)
93+
return self._chunks(response, max_length)
9494

9595
def _chunks(
96-
self, response: "requests.Response", required_length: int
96+
self, response: "requests.Response", max_length: int
9797
) -> Iterator[bytes]:
9898
"""A generator function to be returned by fetch. This way the
9999
caller of fetch can differentiate between connection and actual data
@@ -113,7 +113,7 @@ def _chunks(
113113

114114
read_amount = min(
115115
self.chunk_size,
116-
required_length - bytes_received,
116+
max_length - bytes_received,
117117
)
118118

119119
# NOTE: This may not handle some servers adding a
@@ -131,13 +131,13 @@ def _chunks(
131131

132132
yield data
133133

134-
if bytes_received >= required_length:
134+
if bytes_received >= max_length:
135135
break
136136

137137
logger.debug(
138138
"Downloaded %d out of %d bytes",
139139
bytes_received,
140-
required_length,
140+
max_length,
141141
)
142142

143143
except urllib3.exceptions.ReadTimeoutError as e:

tuf/ngclient/fetcher.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ class FetcherInterface:
2929
__metaclass__ = abc.ABCMeta
3030

3131
@abc.abstractmethod
32-
def fetch(self, url: str, required_length: int) -> Iterator[bytes]:
32+
def fetch(self, url: str, max_length: int) -> Iterator[bytes]:
3333
"""Fetches the contents of HTTP/HTTPS url from a remote server.
3434
35-
Ensures the length of the downloaded data is up to 'required_length'.
35+
Ensures the length of the downloaded data is up to 'max_length'.
3636
3737
Arguments:
3838
url: A URL string that represents a file location.
39-
required_length: An integer value representing the file length in
40-
bytes.
39+
max_length: An integer value representing the maximum
40+
number of bytes to be downloaded.
4141
4242
Raises:
4343
tuf.exceptions.SlowRetrievalError: A timeout occurs while receiving
@@ -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)