1"""Download files with progress indicators.
7from typing
import Iterable, Optional, Tuple
26 except (ValueError, KeyError, TypeError):
42 logged_url = redact_auth_from_url(url)
45 logged_url =
"{} ({})".format(logged_url, format_size(total_length))
47 if is_from_cache(resp):
54 elif is_from_cache(resp):
56 elif not total_length:
58 elif total_length > (40 * 1000):
63 chunks = response_chunks(resp, CONTENT_CHUNK_SIZE)
68 renderer = get_download_progress_renderer(bar_type=progress_bar, size=total_length)
74 Sanitize the "filename" value from a Content-Disposition header.
81 Parse the "filename" value from a Content-Disposition header, and
82 return the default filename if the result is empty.
85 m[
"content-type"] = content_disposition
91 return filename
or default_filename
95 """Get an ideal filename from the given HTTP response, falling back to
96 the link filename if not provided.
101 if content_disposition:
103 ext: Optional[str] = splitext(filename)[1]
117 resp =
session.get(target_url, headers=HEADERS, stream=
True)
118 raise_for_status(resp)
131 def __call__(self, link: Link, location: str) -> Tuple[str, str]:
132 """Download the file given by link into location."""
135 except NetworkConnectionError
as e:
146 with open(filepath,
"wb")
as content_file:
150 return filepath, content_type
163 self, links: Iterable[Link], location: str
164 ) -> Iterable[Tuple[Link, Tuple[str, str]]]:
165 """Download the files given by links into location."""
169 except NetworkConnectionError
as e:
172 "HTTP error %s while getting %s",
182 with open(filepath,
"wb")
as content_file:
186 yield link, (filepath, content_type)
Iterable[Tuple[Link, Tuple[str, str]]] __call__(self, Iterable[Link] links, str location)
None __init__(self, PipSession session, str progress_bar)
Tuple[str, str] __call__(self, Link link, str location)
None __init__(self, PipSession session, str progress_bar)
Optional[int] _get_http_response_size(Response resp)
str sanitize_content_filename(str filename)
str _get_http_response_filename(Response resp, Link link)
str parse_content_disposition(str content_disposition, str default_filename)
Iterable[bytes] _prepare_download(Response resp, Link link, str progress_bar)
Response _http_get_download(PipSession session, Link link)