22
22
23
23
def _get_http_response_size (resp : Response ) -> Optional [int ]:
24
24
try :
25
- return int (resp .headers [' content-length' ])
25
+ return int (resp .headers [" content-length" ])
26
26
except (ValueError , KeyError , TypeError ):
27
27
return None
28
28
29
29
30
30
def _prepare_download (
31
31
resp : Response ,
32
32
link : Link ,
33
- progress_bar : str
33
+ progress_bar : str ,
34
34
) -> Iterable [bytes ]:
35
35
total_length = _get_http_response_size (resp )
36
36
@@ -42,7 +42,7 @@ def _prepare_download(
42
42
logged_url = redact_auth_from_url (url )
43
43
44
44
if total_length :
45
- logged_url = ' {} ({})' .format (logged_url , format_size (total_length ))
45
+ logged_url = " {} ({})" .format (logged_url , format_size (total_length ))
46
46
47
47
if is_from_cache (resp ):
48
48
logger .info ("Using cached %s" , logged_url )
@@ -65,9 +65,7 @@ def _prepare_download(
65
65
if not show_progress :
66
66
return chunks
67
67
68
- return DownloadProgressProvider (
69
- progress_bar , max = total_length
70
- )(chunks )
68
+ return DownloadProgressProvider (progress_bar , max = total_length )(chunks )
71
69
72
70
73
71
def sanitize_content_filename (filename : str ) -> str :
@@ -83,7 +81,7 @@ def parse_content_disposition(content_disposition: str, default_filename: str) -
83
81
return the default filename if the result is empty.
84
82
"""
85
83
_type , params = cgi .parse_header (content_disposition )
86
- filename = params .get (' filename' )
84
+ filename = params .get (" filename" )
87
85
if filename :
88
86
# We need to sanitize the filename to prevent directory traversal
89
87
# in case the filename contains ".." path parts.
@@ -97,14 +95,12 @@ def _get_http_response_filename(resp: Response, link: Link) -> str:
97
95
"""
98
96
filename = link .filename # fallback
99
97
# Have a look at the Content-Disposition header for a better guess
100
- content_disposition = resp .headers .get (' content-disposition' )
98
+ content_disposition = resp .headers .get (" content-disposition" )
101
99
if content_disposition :
102
100
filename = parse_content_disposition (content_disposition , filename )
103
101
ext : Optional [str ] = splitext (filename )[1 ]
104
102
if not ext :
105
- ext = mimetypes .guess_extension (
106
- resp .headers .get ('content-type' , '' )
107
- )
103
+ ext = mimetypes .guess_extension (resp .headers .get ("content-type" , "" ))
108
104
if ext :
109
105
filename += ext
110
106
if not ext and link .url != resp .url :
@@ -115,7 +111,7 @@ def _get_http_response_filename(resp: Response, link: Link) -> str:
115
111
116
112
117
113
def _http_get_download (session : PipSession , link : Link ) -> Response :
118
- target_url = link .url .split ('#' , 1 )[0 ]
114
+ target_url = link .url .split ("#" , 1 )[0 ]
119
115
resp = session .get (target_url , headers = HEADERS , stream = True )
120
116
raise_for_status (resp )
121
117
return resp
@@ -145,15 +141,14 @@ def __call__(self, link: Link, location: str) -> Tuple[str, str]:
145
141
filepath = os .path .join (location , filename )
146
142
147
143
chunks = _prepare_download (resp , link , self ._progress_bar )
148
- with open (filepath , 'wb' ) as content_file :
144
+ with open (filepath , "wb" ) as content_file :
149
145
for chunk in chunks :
150
146
content_file .write (chunk )
151
- content_type = resp .headers .get (' Content-Type' , '' )
147
+ content_type = resp .headers .get (" Content-Type" , "" )
152
148
return filepath , content_type
153
149
154
150
155
151
class BatchDownloader :
156
-
157
152
def __init__ (
158
153
self ,
159
154
session : PipSession ,
@@ -173,16 +168,17 @@ def __call__(
173
168
assert e .response is not None
174
169
logger .critical (
175
170
"HTTP error %s while getting %s" ,
176
- e .response .status_code , link ,
171
+ e .response .status_code ,
172
+ link ,
177
173
)
178
174
raise
179
175
180
176
filename = _get_http_response_filename (resp , link )
181
177
filepath = os .path .join (location , filename )
182
178
183
179
chunks = _prepare_download (resp , link , self ._progress_bar )
184
- with open (filepath , 'wb' ) as content_file :
180
+ with open (filepath , "wb" ) as content_file :
185
181
for chunk in chunks :
186
182
content_file .write (chunk )
187
- content_type = resp .headers .get (' Content-Type' , '' )
183
+ content_type = resp .headers .get (" Content-Type" , "" )
188
184
yield link , (filepath , content_type )
0 commit comments