diff --git a/bionty/base/dev/_io.py b/bionty/base/dev/_io.py index 3588580..6b6be32 100644 --- a/bionty/base/dev/_io.py +++ b/bionty/base/dev/_io.py @@ -1,4 +1,5 @@ import os +import shutil from pathlib import Path from typing import Union @@ -48,6 +49,10 @@ def url_download( Raises: HttpError: If the request response is not 200 and OK. """ + if url.startswith("file://"): + url = url.split("file://")[-1] + shutil.copy(url, localpath) + return localpath try: response = requests.get(url, stream=True, allow_redirects=True, **kwargs) response.raise_for_status() diff --git a/tests/dev/test_io.py b/tests/dev/test_io.py index affe938..a2b961d 100644 --- a/tests/dev/test_io.py +++ b/tests/dev/test_io.py @@ -1,3 +1,4 @@ +import tempfile from pathlib import Path import pytest @@ -20,3 +21,23 @@ def test_url_download(local): downloaded_path = Path(url_download(url=url, localpath=localpath)) assert downloaded_path.exists() + + +def test_local_file(): + with tempfile.TemporaryDirectory() as temp_dir: + local_file = Path(temp_dir) / "test.txt" + target_file = Path(temp_dir) / "downloaded.txt" + test_content = "temporary file" + + local_file.write_text(test_content) + assert local_file.exists(), "Test file was not created" + + downloaded_path = Path( + url_download(url=f"file://{local_file}", localpath=target_file) + ) + + assert downloaded_path.exists(), "Downloaded file not found" + assert downloaded_path.read_text() == test_content, "Content mismatch" + + if downloaded_path.exists(): + downloaded_path.unlink()