Skip to content

Commit 5159950

Browse files
committed
refactor: Rename downloader functions and add comments
1 parent 27d3622 commit 5159950

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/pip/_internal/network/download.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def _get_http_response_etag_or_last_modified(resp: Response) -> str | None:
4040
return resp.headers.get("etag", resp.headers.get("last-modified"))
4141

4242

43-
def _prepare_download(
43+
def _log_download(
4444
resp: Response,
4545
link: Link,
4646
progress_bar: str,
@@ -175,13 +175,13 @@ def __init__(
175175
def batch(
176176
self, links: Iterable[Link], location: str
177177
) -> Iterable[tuple[Link, tuple[str, str]]]:
178-
"""Download the files given by links into location."""
178+
"""Convenience method to download multiple links."""
179179
for link in links:
180180
filepath, content_type = self(link, location)
181181
yield link, (filepath, content_type)
182182

183183
def __call__(self, link: Link, location: str) -> tuple[str, str]:
184-
"""Download the file given by link into location."""
184+
"""Download a link and save it under location."""
185185
resp = self._http_get(link)
186186
download_size = _get_http_response_size(resp)
187187

@@ -190,14 +190,14 @@ def __call__(self, link: Link, location: str) -> tuple[str, str]:
190190
download = _FileDownload(link, content_file, download_size)
191191
self._process_response(download, resp)
192192
if download.is_incomplete():
193-
self._attempt_resume(download, resp)
193+
self._attempt_resumes_or_redownloads(download, resp)
194194

195195
content_type = resp.headers.get("Content-Type", "")
196196
return filepath, content_type
197197

198198
def _process_response(self, download: _FileDownload, resp: Response) -> None:
199-
"""Process the response and write the chunks to the file."""
200-
chunks = _prepare_download(
199+
"""Download and save chunks from a response."""
200+
chunks = _log_download(
201201
resp,
202202
download.link,
203203
self._progress_bar,
@@ -214,8 +214,10 @@ def _process_response(self, download: _FileDownload, resp: Response) -> None:
214214

215215
logger.warning("Connection timed out while downloading.")
216216

217-
def _attempt_resume(self, download: _FileDownload, resp: Response) -> None:
218-
"""Attempt to resume the download if connection was dropped."""
217+
def _attempt_resumes_or_redownloads(
218+
self, download: _FileDownload, resp: Response
219+
) -> None:
220+
"""Attempt to resume/restart the download if connection was dropped."""
219221

220222
while download.reattempts < self._resume_retries and download.is_incomplete():
221223
assert download.size is not None
@@ -250,6 +252,8 @@ def _http_get_resume(
250252
self, download: _FileDownload, should_match: Response
251253
) -> Response:
252254
"""Issue a HTTP range request to resume the download."""
255+
# To better understand the download resumption logic, see the mdn web docs:
256+
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Range_requests
253257
headers = HEADERS.copy()
254258
headers["Range"] = f"bytes={download.bytes_received}-"
255259
# If possible, use a conditional range request to avoid corrupted

tests/unit/test_network_download.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pip._internal.network.download import (
1313
Downloader,
1414
_get_http_response_size,
15-
_prepare_download,
15+
_log_download,
1616
parse_content_disposition,
1717
sanitize_content_filename,
1818
)
@@ -76,7 +76,7 @@
7676
),
7777
],
7878
)
79-
def test_prepare_download__log(
79+
def test_log_download(
8080
caplog: pytest.LogCaptureFixture,
8181
url: str,
8282
headers: dict[str, str],
@@ -92,7 +92,7 @@ def test_prepare_download__log(
9292
resp.from_cache = from_cache
9393
link = Link(url)
9494
total_length = _get_http_response_size(resp)
95-
_prepare_download(
95+
_log_download(
9696
resp,
9797
link,
9898
progress_bar="on",

0 commit comments

Comments
 (0)