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

Fix bug for save_as for content type and add unittest #2213

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions idmtools_core/idmtools/assets/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ def save_as(self, dest: str, force: bool = False): # noqa: F811
if self.absolute_path is not None:
self.download_generator_hook = partial(file_content_to_generator, self.absolute_path)
elif self.content:
self.download_generator_hook = partial(content_generator, self.content)
self.download_generator_hook = partial(content_generator, self.bytes)
else:
raise ValueError("Asset has no content or absolute path")

Expand All @@ -438,7 +438,7 @@ def save_md5_checksum(self):
Returns:
None
"""
asset = Asset(filename=f"{self.filename}.md5", content=f"{self.filename}:md5:{self.checksum}".encode())
asset = Asset(filename=f"{self.filename}.md5", content=f"{self.filename}:md5:{self.checksum}")
if asset.checksum is None:
asset.calculate_checksum()
asset.save_as(os.path.curdir, force=True)
Expand Down
9 changes: 6 additions & 3 deletions idmtools_core/idmtools/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
from io import BytesIO
from os import DirEntry
from typing import Iterable, Generator, List
from typing import Iterable, Generator, List, Union


def scan_directory(basedir: str, recursive: bool = True, ignore_directories: List[str] = None) -> Iterable[DirEntry]:
Expand Down Expand Up @@ -49,7 +49,7 @@ def file_content_to_generator(absolute_path, chunk_size=128) -> Generator[bytear
yield res


def content_generator(content, chunk_size=128) -> Generator[bytearray, None, None]:
def content_generator(content: Union[str, bytes], chunk_size=128) -> Generator[bytearray, None, None]:
"""
Create a generator from file contents in chunks(useful for streaming binary data and piping).

Expand All @@ -60,7 +60,10 @@ def content_generator(content, chunk_size=128) -> Generator[bytearray, None, Non
Returns:
Generator that return bytes in chunks of size chunk_size
"""
content_io = BytesIO(content)
if isinstance(content, str):
content_io = BytesIO(content.encode())
else:
content_io = BytesIO(content)
while True:
chunk = content_io.read(chunk_size)
if chunk == b'':
Expand Down
9 changes: 9 additions & 0 deletions idmtools_core/tests/test_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,15 @@ def test_file_content_to_generator_empty_file(self, mock_file):
chunks = list(generator)
self.assertEqual(len(chunks), 0)

def test_save_as(self):
# Initialize the asset object
asset = Asset(filename="example.txt", content="example content")
expected_content = f"{asset.content}"
asset.save_as("example.txt", force=True)
with open(os.path.join(os.path.curdir, "example.txt"), "r") as f:
assert f.read() == expected_content
os.remove(os.path.join(os.path.curdir, "example.txt"))


if __name__ == '__main__':
unittest.main()