From 0adeecef42f12e5c74c39a1586b63b153263f12e Mon Sep 17 00:00:00 2001 From: Marcel Levstek <62072754+marcellevstek@users.noreply.github.com> Date: Mon, 21 Oct 2024 13:16:49 +0200 Subject: [PATCH] fixup! Add functionality for setting custom names --- src/resdk/resources/data.py | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/resdk/resources/data.py b/src/resdk/resources/data.py index ccbcea9d..fd96c9e5 100644 --- a/src/resdk/resources/data.py +++ b/src/resdk/resources/data.py @@ -17,12 +17,6 @@ from .sample import Sample from .utils import flatten_field, parse_resolwe_datetime -logging.basicConfig( - level=logging.INFO, - format="%(asctime)s - %(levelname)s - %(message)s", - datefmt="%d-%b-%y %H:%M:%S", -) - class Data(BaseResolweResource): """Resolwe Data resource. @@ -366,39 +360,37 @@ def download( def download_and_rename( self, custom_file_name: str, + overwrite_existing: bool = False, field_name: Optional[str] = None, file_name: Optional[str] = None, download_dir: Optional[str] = None, ): """Download and rename a single file from the Data object.""" + if not field_name and not file_name: + raise ValueError("Either 'file_name' or 'field_name' must be provided.") + if download_dir is None: download_dir = os.getcwd() - - new_file_path = os.path.join(download_dir, custom_file_name) - - if os.path.exists(new_file_path): + destination_file_path = os.path.join(download_dir, custom_file_name) + if os.path.exists(destination_file_path) and not overwrite_existing: logging.warning( - f"File with path '{new_file_path}' already exists. Skipping download." + f"File with path '{destination_file_path}' already exists. Skipping download." ) return - file_names = self.download( + source_file_name = self.download( file_name=file_name, field_name=field_name, download_dir=download_dir, - ) - if len(file_names) != 1: - raise ValueError( - f"Expected one file to be downloaded, but got {len(file_names)}" - ) - og_file_name = file_names[0] - og_file_path = os.path.join(download_dir, og_file_name) + )[0] + + source_file_path = os.path.join(download_dir, source_file_name) - logging.info(f"Renaming file '{og_file_name}' to '{custom_file_name}'.") + logging.info(f"Renaming file '{source_file_name}' to '{custom_file_name}'.") os.rename( - og_file_path, - new_file_path, + source_file_path, + destination_file_path, ) def stdout(self):