Skip to content

Commit

Permalink
✨Add support for local source files (#156)
Browse files Browse the repository at this point in the history
Signed-off-by: zethson <[email protected]>
Co-authored-by: Lukas Heumos <[email protected]>
  • Loading branch information
mossjacob and Zethson authored Nov 19, 2024
1 parent 6f472da commit 790fd1e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
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()

0 comments on commit 790fd1e

Please sign in to comment.