1
- #!/usr/bin/env python
2
-
3
1
# Copyright 2012 - 2017, New York University and the TUF contributors
4
2
# SPDX-License-Identifier: MIT OR Apache-2.0
5
3
4
+ """Provides a class handling the download of URL contents to a file"
6
5
"""
7
- <Program Name>
8
- download.py
9
-
10
- <Started>
11
- February 21, 2012. Based on previous version by Geremy Condra.
12
-
13
- <Author>
14
- Konstantin Andrianov
15
-
16
6
17
- <Copyright>
18
- See LICENSE-MIT OR LICENSE for licensing information.
19
-
20
- <Purpose>
21
- Download metadata and target files and check their validity. The hash and
22
- length of a downloaded file has to match the hash and length supplied by the
23
- metadata of that file.
24
- """
25
7
import logging
26
8
import tempfile
27
9
from contextlib import contextmanager
10
+ from typing import IO , Iterator
28
11
from urllib import parse
29
12
30
13
from tuf import exceptions
@@ -41,14 +24,14 @@ class FileDownloader:
41
24
the network IO library.
42
25
"""
43
26
44
- def __init__ (self , fetcher ):
27
+ def __init__ (self , fetcher : "FetcherInterface" ):
45
28
if fetcher is None :
46
29
fetcher = RequestsFetcher ()
47
30
48
31
self ._fetcher = fetcher
49
32
50
33
@contextmanager
51
- def download_file (self , url , required_length ) :
34
+ def download_file (self , url : str , required_length : int ) -> Iterator [ IO ] :
52
35
"""Opens a connection to 'url' and downloads the content
53
36
up to 'required_length'.
54
37
@@ -74,8 +57,7 @@ def download_file(self, url, required_length):
74
57
logger .debug ("Downloading: %s" , url )
75
58
76
59
number_of_bytes_received = 0
77
- # This is the temporary file that we will return to contain the
78
- # contents of the downloaded file.
60
+
79
61
with tempfile .TemporaryFile () as temp_file :
80
62
chunks = self ._fetcher .fetch (url , required_length )
81
63
for chunk in chunks :
@@ -88,7 +70,7 @@ def download_file(self, url, required_length):
88
70
temp_file .seek (0 )
89
71
yield temp_file
90
72
91
- def download_bytes (self , url , required_length ) :
73
+ def download_bytes (self , url : str , required_length : int ) -> bytes :
92
74
"""Download bytes from given url
93
75
94
76
Returns the downloaded bytes, otherwise like download_file()
0 commit comments