Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Add support for local source files #156

Merged
merged 11 commits into from
Nov 19, 2024
5 changes: 5 additions & 0 deletions bionty/base/dev/_io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import shutil
from pathlib import Path
from typing import Union

Expand Down Expand Up @@ -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()
Expand Down
21 changes: 21 additions & 0 deletions tests/dev/test_io.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import tempfile
from pathlib import Path

import pytest
Expand All @@ -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()
Loading