Skip to content

Commit

Permalink
add get method
Browse files Browse the repository at this point in the history
  • Loading branch information
sellth committed Nov 27, 2023
1 parent 41c8e12 commit 5df1d7e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
30 changes: 30 additions & 0 deletions cubi_tk/irods_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,33 @@ def chksum(self):
logger.error("Problem during iRODS checksumming.")
logger.error(self.get_irods_error(e))

def get(self):
"""Download files from SODAR."""
self.__jobs = [
attrs.evolve(job, bytes=self.session.data_objects.get(job.path_remote).size)
for job in self.__jobs
]
self.__total_bytes = sum([job.bytes for job in self.__jobs])
# Double tqdm for currently transferred file info
# TODO: add more parenthesis after python 3.10
with tqdm(
total=self.__total_bytes,
unit="B",
unit_scale=True,
unit_divisor=1024,
position=1,
) as t, tqdm(total=0, position=0, bar_format="{desc}", leave=False) as file_log:
for n, job in enumerate(self.__jobs):
file_log.set_description_str(
f"File [{n + 1}/{len(self.__jobs)}]: {Path(job.path_local).name}"
)
try:
self.session.data_objects.get(job.path_remote, job.path_local)
t.update(job.bytes)
except FileNotFoundError: # pragma: no cover
raise
except Exception as e: # pragma: no cover
logger.error(f"Problem during transfer of {job.path_remote}")
logger.error(self.get_irods_error(e))
sys.exit(1)
t.clear()
18 changes: 17 additions & 1 deletion tests/test_irods_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
def test_transfer_job_bytes(fs):
fs.create_file("test_file", st_size=123)
assert TransferJob("test_file", "remote/path").bytes == 123
assert TransferJob("no_file.no", "remote/path").bytes == -1


@patch("cubi_tk.irods_common.iRODSSession")
Expand Down Expand Up @@ -90,7 +91,9 @@ def test_get_irods_sessions(mocksession):
def jobs():
return (
TransferJob(path_local="myfile.csv", path_remote="dest_dir/myfile.csv", bytes=123),
TransferJob(path_local="folder/file.csv", path_remote="dest_dir/folder/file.csv", bytes=1024),
TransferJob(
path_local="folder/file.csv", path_remote="dest_dir/folder/file.csv", bytes=1024
),
)


Expand Down Expand Up @@ -151,3 +154,16 @@ def test_irods_transfer_chksum(itransfer):
assert mock_data_object.chksum.call_count == len(itransfer.destinations)
for path in itransfer.destinations:
mockget.assert_any_call(path)


def test_irods_transfer_get(itransfer, jobs, fs):
with patch.object(itransfer.session.data_objects, "get") as mockget:
mockget.return_value.size = 111
itransfer.get()

for job in jobs:
# size check
mockget.assert_any_call(job.path_remote)
# download
mockget.assert_any_call(job.path_remote, job.path_local)
assert itransfer.size == 222

0 comments on commit 5df1d7e

Please sign in to comment.