Skip to content

Commit 1e18908

Browse files
committed
Fix a bug in download_and_rename method
1 parent 3c57ba9 commit 1e18908

3 files changed

Lines changed: 40 additions & 14 deletions

File tree

docs/CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ Added
1212
-----
1313
- Add support for ``Python 3.14``
1414

15+
Fixed
16+
-----
17+
- Fix a bug that prevented the downloading and renaming of
18+
a directory from a data object
19+
1520

1621
===================
1722
22.2.1 - 2025-11-19

src/resdk/resources/data.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def download(
257257
field_name: Optional[str] = None,
258258
download_dir: Optional[str] = None,
259259
show_progress: bool = True,
260+
return_dir_names: bool = False,
260261
) -> list[str]:
261262
"""Download Data object's files and directories.
262263
@@ -270,6 +271,12 @@ def download(
270271
* re.data.get(42).download(file_name='alignment7.bam')
271272
* re.data.get(42).download(field_name='bam')
272273
274+
The function returns a list of downloaded file names. In case the
275+
Data object contains directories, the user can specify to return directory names
276+
instead of file names contained in those directories via the return_dir_names argument.
277+
WARNING: If neither file_name nor field_name is specified and return_dir_names is set
278+
to True, the method will download both directories and files when the Data object
279+
contains both. However, the returned list will include only the directory names.
273280
"""
274281
if file_name and field_name:
275282
raise ValueError("Only one of file_name or field_name may be given.")
@@ -281,41 +288,52 @@ def download(
281288
files=files, download_dir=download_dir, show_progress=show_progress
282289
)
283290

284-
return file_names
291+
dir_names = self._files_dirs("dir", file_name, field_name)
292+
if dir_names and return_dir_names:
293+
asset_names = dir_names
294+
else:
295+
asset_names = file_names
296+
297+
return asset_names
285298

286299
def download_and_rename(
287300
self,
288-
custom_file_name: str,
301+
custom_asset_name: str,
289302
overwrite_existing: bool = False,
290303
field_name: Optional[str] = None,
291304
file_name: Optional[str] = None,
292305
download_dir: Optional[str] = None,
293306
):
294-
"""Download and rename a single file from the Data object."""
307+
"""Download and rename an asset from the Data object."""
295308

296309
if not field_name and not file_name:
297310
raise ValueError("Either 'file_name' or 'field_name' must be given.")
298311

299312
if download_dir is None:
300313
download_dir = os.getcwd()
301-
destination_file_path = os.path.join(download_dir, custom_file_name)
302-
if os.path.exists(destination_file_path) and not overwrite_existing:
303-
raise FileExistsError(
304-
f"File with path '{destination_file_path}' already exists. Skipping download."
314+
destination_asset_path = os.path.join(download_dir, custom_asset_name)
315+
if os.path.exists(destination_asset_path) and not overwrite_existing:
316+
logging.warning(
317+
f"File or directory with path '{destination_asset_path}' already exists. "
318+
"Skipping download."
305319
)
320+
return
306321

307-
source_file_name = self.download(
322+
source_asset_name = self.download(
308323
file_name=file_name,
309324
field_name=field_name,
310325
download_dir=download_dir,
326+
return_dir_names=True,
311327
)[0]
312328

313-
source_file_path = os.path.join(download_dir, source_file_name)
329+
source_asset_path = os.path.join(download_dir, source_asset_name)
314330

315-
logging.info(f"Renaming file '{source_file_name}' to '{custom_file_name}'.")
331+
logging.info(
332+
f"Renaming the downloaded asset from '{source_asset_name}' to '{custom_asset_name}'."
333+
)
316334
os.rename(
317-
source_file_path,
318-
destination_file_path,
335+
source_asset_path,
336+
destination_asset_path,
319337
)
320338

321339
def stdout(self) -> str:

tests/unit/test_data.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,16 @@ def test_download_ok(self, data_mock):
209209
with patch("os.rename") as mock_rename:
210210
Data.download_and_rename(
211211
data_mock,
212-
custom_file_name="text_file1.txt",
212+
custom_asset_name="text_file1.txt",
213213
field_name="txt",
214214
download_dir="/some/path/",
215215
)
216216

217217
data_mock.download.assert_called_once_with(
218-
file_name=None, field_name="txt", download_dir="/some/path/"
218+
file_name=None,
219+
field_name="txt",
220+
download_dir="/some/path/",
221+
return_dir_names=True,
219222
)
220223

221224
mock_rename.assert_called_once()

0 commit comments

Comments
 (0)