Skip to content

Commit d4b9552

Browse files
authored
added yandex.disk support and test file (#6667)
Fixes #6666 /black ### Description Updated download_url to support internally yandex.disk hosting ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [x] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: BlackyI <[email protected]>
1 parent 59470e4 commit d4b9552

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

monai/apps/utils.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from __future__ import annotations
1313

1414
import hashlib
15+
import json
1516
import logging
1617
import os
1718
import shutil
@@ -24,7 +25,7 @@
2425
from typing import TYPE_CHECKING, Any
2526
from urllib.error import ContentTooShortError, HTTPError, URLError
2627
from urllib.parse import urlparse
27-
from urllib.request import urlretrieve
28+
from urllib.request import urlopen, urlretrieve
2829

2930
from monai.config.type_definitions import PathLike
3031
from monai.utils import look_up_option, min_version, optional_import
@@ -203,6 +204,17 @@ def download_url(
203204
if not has_gdown:
204205
raise RuntimeError("To download files from Google Drive, please install the gdown dependency.")
205206
gdown.download(url, f"{tmp_name}", quiet=not progress, **gdown_kwargs)
207+
elif urlparse(url).netloc == "cloud-api.yandex.net":
208+
with urlopen(url) as response:
209+
code = response.getcode()
210+
if code == 200:
211+
download_url = json.loads(response.read())["href"]
212+
_download_with_progress(download_url, tmp_name, progress=progress)
213+
else:
214+
raise RuntimeError(
215+
f"Download of file from {download_url}, received from {url} "
216+
+ f" to {filepath} failed due to network issue or denied permission."
217+
)
206218
else:
207219
_download_with_progress(url, tmp_name, progress=progress)
208220
if not tmp_name.exists():

tests/test_download_url_yandex.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) MONAI Consortium
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
12+
from __future__ import annotations
13+
14+
import os
15+
import tempfile
16+
import unittest
17+
from urllib.error import HTTPError
18+
19+
from monai.apps.utils import download_url
20+
21+
YANDEX_MODEL_URL = (
22+
"https://cloud-api.yandex.net/v1/disk/public/resources/download?"
23+
"public_key=https%3A%2F%2Fdisk.yandex.ru%2Fd%2Fxs0gzlj2_irgWA"
24+
)
25+
YANDEX_MODEL_FLAWED_URL = (
26+
"https://cloud-api.yandex.net/v1/disk/public/resources/download?"
27+
"public_key=https%3A%2F%2Fdisk.yandex.ru%2Fd%2Fxs0gzlj2_irgWA-url-with-error"
28+
)
29+
30+
31+
class TestDownloadUrlYandex(unittest.TestCase):
32+
def test_verify(self):
33+
with tempfile.TemporaryDirectory() as tempdir:
34+
download_url(url=YANDEX_MODEL_URL, filepath=os.path.join(tempdir, "model.pt"))
35+
36+
def test_verify_error(self):
37+
with tempfile.TemporaryDirectory() as tempdir:
38+
with self.assertRaises(HTTPError):
39+
download_url(url=YANDEX_MODEL_FLAWED_URL, filepath=os.path.join(tempdir, "model.pt"))
40+
41+
42+
if __name__ == "__main__":
43+
unittest.main()

0 commit comments

Comments
 (0)