Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
pip._internal.network.download Namespace Reference

Data Structures

class  BatchDownloader
 
class  Downloader
 

Functions

Optional[int] _get_http_response_size (Response resp)
 
Iterable[bytes] _prepare_download (Response resp, Link link, str progress_bar)
 
str sanitize_content_filename (str filename)
 
str parse_content_disposition (str content_disposition, str default_filename)
 
str _get_http_response_filename (Response resp, Link link)
 
Response _http_get_download (PipSession session, Link link)
 

Variables

 logger = logging.getLogger(__name__)
 

Detailed Description

Download files with progress indicators.

Function Documentation

◆ _get_http_response_filename()

str _get_http_response_filename ( Response  resp,
Link  link 
)
protected
Get an ideal filename from the given HTTP response, falling back to
the link filename if not provided.

Definition at line 94 of file download.py.

94def _get_http_response_filename(resp: Response, link: Link) -> str:
95 """Get an ideal filename from the given HTTP response, falling back to
96 the link filename if not provided.
97 """
98 filename = link.filename # fallback
99 # Have a look at the Content-Disposition header for a better guess
100 content_disposition = resp.headers.get("content-disposition")
101 if content_disposition:
102 filename = parse_content_disposition(content_disposition, filename)
103 ext: Optional[str] = splitext(filename)[1]
104 if not ext:
105 ext = mimetypes.guess_extension(resp.headers.get("content-type", ""))
106 if ext:
107 filename += ext
108 if not ext and link.url != resp.url:
109 ext = os.path.splitext(resp.url)[1]
110 if ext:
111 filename += ext
112 return filename
113
114
for i

References i, and pip._internal.network.download.parse_content_disposition().

Referenced by BatchDownloader.__call__(), and Downloader.__call__().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _get_http_response_size()

Optional[int] _get_http_response_size ( Response  resp)
protected

Definition at line 23 of file download.py.

23def _get_http_response_size(resp: Response) -> Optional[int]:
24 try:
25 return int(resp.headers["content-length"])
26 except (ValueError, KeyError, TypeError):
27 return None
28
29

References i.

Referenced by pip._internal.network.download._prepare_download().

Here is the caller graph for this function:

◆ _http_get_download()

Response _http_get_download ( PipSession  session,
Link  link 
)
protected

Definition at line 115 of file download.py.

115def _http_get_download(session: PipSession, link: Link) -> Response:
116 target_url = link.url.split("#", 1)[0]
117 resp = session.get(target_url, headers=HEADERS, stream=True)
118 raise_for_status(resp)
119 return resp
120
121

References i.

Referenced by BatchDownloader.__call__(), and Downloader.__call__().

Here is the caller graph for this function:

◆ _prepare_download()

Iterable[bytes] _prepare_download ( Response  resp,
Link  link,
str  progress_bar 
)
protected

Definition at line 30 of file download.py.

34) -> Iterable[bytes]:
35 total_length = _get_http_response_size(resp)
36
38 url = link.show_url
39 else:
41
42 logged_url = redact_auth_from_url(url)
43
44 if total_length:
45 logged_url = "{} ({})".format(logged_url, format_size(total_length))
46
47 if is_from_cache(resp):
48 logger.info("Using cached %s", logged_url)
49 else:
50 logger.info("Downloading %s", logged_url)
51
53 show_progress = False
54 elif is_from_cache(resp):
55 show_progress = False
56 elif not total_length:
57 show_progress = True
58 elif total_length > (40 * 1000):
59 show_progress = True
60 else:
61 show_progress = False
62
63 chunks = response_chunks(resp, CONTENT_CHUNK_SIZE)
64
65 if not show_progress:
66 return chunks
67
68 renderer = get_download_progress_renderer(bar_type=progress_bar, size=total_length)
69 return renderer(chunks)
70
71

References pip._internal.network.download._get_http_response_size(), and i.

Referenced by BatchDownloader.__call__(), and Downloader.__call__().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parse_content_disposition()

str parse_content_disposition ( str  content_disposition,
str  default_filename 
)
Parse the "filename" value from a Content-Disposition header, and
return the default filename if the result is empty.

Definition at line 79 of file download.py.

79def parse_content_disposition(content_disposition: str, default_filename: str) -> str:
80 """
81 Parse the "filename" value from a Content-Disposition header, and
82 return the default filename if the result is empty.
83 """
85 m["content-type"] = content_disposition
86 filename = m.get_param("filename")
87 if filename:
88 # We need to sanitize the filename to prevent directory traversal
89 # in case the filename contains ".." path parts.
90 filename = sanitize_content_filename(str(filename))
91 return filename or default_filename
92
93

References i, and pip._internal.network.download.sanitize_content_filename().

Referenced by pip._internal.network.download._get_http_response_filename().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sanitize_content_filename()

str sanitize_content_filename ( str  filename)
Sanitize the "filename" value from a Content-Disposition header.

Definition at line 72 of file download.py.

72def sanitize_content_filename(filename: str) -> str:
73 """
74 Sanitize the "filename" value from a Content-Disposition header.
75 """
76 return os.path.basename(filename)
77
78

References i.

Referenced by pip._internal.network.download.parse_content_disposition().

Here is the caller graph for this function:

Variable Documentation

◆ logger

logger = logging.getLogger(__name__)

Definition at line 20 of file download.py.