-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing type annotations in download.py, fetcher.py and requests_fetcher.py Update docstrings to match the new style. Signed-off-by: Teodora Sechkova <[email protected]>
- Loading branch information
Showing
3 changed files
with
13 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,13 @@ | ||
#!/usr/bin/env python | ||
|
||
# Copyright 2012 - 2017, New York University and the TUF contributors | ||
# SPDX-License-Identifier: MIT OR Apache-2.0 | ||
|
||
"""Provides a class handling the download of URL contents to a file" | ||
""" | ||
<Program Name> | ||
download.py | ||
<Started> | ||
February 21, 2012. Based on previous version by Geremy Condra. | ||
<Author> | ||
Konstantin Andrianov | ||
Vladimir Diaz <[email protected]> | ||
|
||
<Copyright> | ||
See LICENSE-MIT OR LICENSE for licensing information. | ||
<Purpose> | ||
Download metadata and target files and check their validity. The hash and | ||
length of a downloaded file has to match the hash and length supplied by the | ||
metadata of that file. | ||
""" | ||
import logging | ||
import tempfile | ||
from contextlib import contextmanager | ||
from typing import IO, Iterator | ||
from urllib import parse | ||
|
||
from tuf import exceptions | ||
|
@@ -41,14 +24,14 @@ class FileDownloader: | |
the network IO library. | ||
""" | ||
|
||
def __init__(self, fetcher): | ||
def __init__(self, fetcher: "FetcherInterface"): | ||
if fetcher is None: | ||
fetcher = RequestsFetcher() | ||
|
||
self._fetcher = fetcher | ||
|
||
@contextmanager | ||
def download_file(self, url, required_length): | ||
def download_file(self, url: str, required_length: int) -> Iterator[IO]: | ||
"""Opens a connection to 'url' and downloads the content | ||
up to 'required_length'. | ||
|
@@ -74,8 +57,7 @@ def download_file(self, url, required_length): | |
logger.debug("Downloading: %s", url) | ||
|
||
number_of_bytes_received = 0 | ||
# This is the temporary file that we will return to contain the | ||
# contents of the downloaded file. | ||
|
||
with tempfile.TemporaryFile() as temp_file: | ||
chunks = self._fetcher.fetch(url, required_length) | ||
for chunk in chunks: | ||
|
@@ -88,7 +70,7 @@ def download_file(self, url, required_length): | |
temp_file.seek(0) | ||
yield temp_file | ||
|
||
def download_bytes(self, url, required_length): | ||
def download_bytes(self, url: str, required_length: int) -> bytes: | ||
"""Download bytes from given url | ||
Returns the downloaded bytes, otherwise like download_file() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters