Skip to content

Commit

Permalink
fix for URL adaptor (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyCMWF authored Dec 3, 2024
1 parent 396d63f commit 296891a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cads_adaptors/tools/url_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def requests_to_urls(
yield {"url": url, "req": req}


def try_download(urls: List[str], context: Context, **kwargs) -> List[str]:
def try_download(
urls: List[str], context: Context, server_suggested_filename=False, **kwargs
) -> List[str]:
# Ensure that URLs are unique to prevent downloading the same file multiple times
urls = sorted(set(urls))

Expand All @@ -48,6 +50,9 @@ def try_download(urls: List[str], context: Context, **kwargs) -> List[str]:
kwargs = {"timeout": 3, "maximum_retries": 1, "retry_after": 1, **kwargs}
for url in urls:
path = urllib.parse.urlparse(url).path.lstrip("/")
if server_suggested_filename:
path = os.path.join(os.path.dirname(path), multiurl.Downloader(url).title())

dir = os.path.dirname(path)
if dir:
os.makedirs(dir, exist_ok=True)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_20_url_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,27 @@ def test_downloaders(tmp_path, monkeypatch, urls, expected_nfiles):
assert len(paths) == expected_nfiles


def test_download_with_server_suggested_filename(tmp_path, monkeypatch):
monkeypatch.chdir(tmp_path) # try_download generates files in the working dir
urls = ["https://gerb.oma.be/c3s/data/ceres-ebaf/tcdr/v4.2/toa_lw_all_mon/2000/07"]
paths_false = url_tools.try_download(
urls, context=url_tools.Context(), server_suggested_filename=False
)
assert len(paths_false) == 1
assert os.path.basename(paths_false[0]) == "07"

paths_true = url_tools.try_download(
urls, context=url_tools.Context(), server_suggested_filename=True
)
assert len(paths_true) == 1
assert (
os.path.basename(paths_true[0])
== "data_312a_Lot1_ceres-ebaf_tcdr_v4.2_toa_lw_all_mon_2000_07.nc"
)

assert os.path.dirname(paths_false[0]) == os.path.dirname(paths_true[0])


@pytest.mark.parametrize(
"anon",
(
Expand Down

0 comments on commit 296891a

Please sign in to comment.