1- #!/usr/bin/env python
2-
31# Copyright 2012 - 2017, New York University and the TUF contributors
42# SPDX-License-Identifier: MIT OR Apache-2.0
53
4+ """Provides a class handling the download of URL contents to a file"
65"""
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- Vladimir Diaz <vladimir.v.diaz@gmail.com>
166
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- """
257import logging
268import tempfile
279from contextlib import contextmanager
10+ from typing import IO , Iterator
2811from urllib import parse
2912
3013from tuf import exceptions
@@ -41,14 +24,14 @@ class FileDownloader:
4124 the network IO library.
4225 """
4326
44- def __init__ (self , fetcher ):
27+ def __init__ (self , fetcher : "FetcherInterface" ):
4528 if fetcher is None :
4629 fetcher = RequestsFetcher ()
4730
4831 self ._fetcher = fetcher
4932
5033 @contextmanager
51- def download_file (self , url , required_length ) :
34+ def download_file (self , url : str , required_length : int ) -> Iterator [ IO ] :
5235 """Opens a connection to 'url' and downloads the content
5336 up to 'required_length'.
5437
@@ -74,8 +57,7 @@ def download_file(self, url, required_length):
7457 logger .debug ("Downloading: %s" , url )
7558
7659 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+
7961 with tempfile .TemporaryFile () as temp_file :
8062 chunks = self ._fetcher .fetch (url , required_length )
8163 for chunk in chunks :
@@ -88,7 +70,7 @@ def download_file(self, url, required_length):
8870 temp_file .seek (0 )
8971 yield temp_file
9072
91- def download_bytes (self , url , required_length ) :
73+ def download_bytes (self , url : str , required_length : int ) -> bytes :
9274 """Download bytes from given url
9375
9476 Returns the downloaded bytes, otherwise like download_file()
0 commit comments