@@ -29,15 +29,15 @@ class FetcherInterface:
29
29
__metaclass__ = abc .ABCMeta
30
30
31
31
@abc .abstractmethod
32
- def fetch (self , url : str , required_length : int ) -> Iterator [bytes ]:
32
+ def fetch (self , url : str , max_length : int ) -> Iterator [bytes ]:
33
33
"""Fetches the contents of HTTP/HTTPS url from a remote server.
34
34
35
- Ensures the length of the downloaded data is up to 'required_length '.
35
+ Ensures the length of the downloaded data is up to 'max_length '.
36
36
37
37
Arguments:
38
38
url: A URL string that represents a file location.
39
- required_length : An integer value representing the file length in
40
- bytes.
39
+ max_length : An integer value representing the maximum
40
+ number of bytes to be downloaded .
41
41
42
42
Raises:
43
43
tuf.exceptions.SlowRetrievalError: A timeout occurs while receiving
@@ -50,21 +50,20 @@ def fetch(self, url: str, required_length: int) -> Iterator[bytes]:
50
50
raise NotImplementedError # pragma: no cover
51
51
52
52
@contextmanager
53
- def download_file (self , url : str , required_length : int ) -> Iterator [IO ]:
53
+ def download_file (self , url : str , max_length : int ) -> Iterator [IO ]:
54
54
"""Opens a connection to 'url' and downloads the content
55
- up to 'required_length '.
55
+ up to 'max_length '.
56
56
57
57
Args:
58
58
url: a URL string that represents the location of the file.
59
- required_length : an integer value representing the length of
60
- the file or an upper boundary .
59
+ max_length : an integer value representing the length of
60
+ the file or an upper bound .
61
61
62
62
Raises:
63
- DownloadLengthMismatchError: a mismatch of observed vs expected
64
- lengths while downloading the file.
63
+ DownloadLengthMismatchError: downloaded bytes exceed 'max_length'.
65
64
66
65
Yields:
67
- A file object that points to the contents of 'url'.
66
+ A TemporaryFile object that points to the contents of 'url'.
68
67
"""
69
68
# 'url.replace('\\', '/')' is needed for compatibility with
70
69
# Windows-based systems, because they might use back-slashes in place
@@ -78,21 +77,21 @@ def download_file(self, url: str, required_length: int) -> Iterator[IO]:
78
77
number_of_bytes_received = 0
79
78
80
79
with tempfile .TemporaryFile () as temp_file :
81
- chunks = self .fetch (url , required_length )
80
+ chunks = self .fetch (url , max_length )
82
81
for chunk in chunks :
83
82
temp_file .write (chunk )
84
83
number_of_bytes_received += len (chunk )
85
- if number_of_bytes_received > required_length :
84
+ if number_of_bytes_received > max_length :
86
85
raise exceptions .DownloadLengthMismatchError (
87
- required_length , number_of_bytes_received
86
+ max_length , number_of_bytes_received
88
87
)
89
88
temp_file .seek (0 )
90
89
yield temp_file
91
90
92
- def download_bytes (self , url : str , required_length : int ) -> bytes :
91
+ def download_bytes (self , url : str , max_length : int ) -> bytes :
93
92
"""Download bytes from given url
94
93
95
94
Returns the downloaded bytes, otherwise like download_file()
96
95
"""
97
- with self .download_file (url , required_length ) as dl_file :
96
+ with self .download_file (url , max_length ) as dl_file :
98
97
return dl_file .read ()
0 commit comments