From e7386b7483f164863891b89ea959c6d640835a99 Mon Sep 17 00:00:00 2001 From: Bram Stoeller Date: Tue, 7 Mar 2023 11:01:16 +0100 Subject: [PATCH] Only use unique key for dirs if no dir_path was given Signed-off-by: Bram Stoeller --- src/power_grid_model_io/utils/download.py | 13 +++++++------ tests/unit/utils/test_download.py | 8 ++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/power_grid_model_io/utils/download.py b/src/power_grid_model_io/utils/download.py index 782fd8e6..631a9ffc 100644 --- a/src/power_grid_model_io/utils/download.py +++ b/src/power_grid_model_io/utils/download.py @@ -214,12 +214,8 @@ def get_download_path( unique_key: A unique string that can be used to generate a filename (e.g. a url). """ - # If no dir_path is given, use the system's designated folder for temporary files. - if dir_path is None: - dir_path = Path(tempfile.gettempdir()) - # If no specific download path was given, we need to generate a unique key (based on the given unique key) - if file_name is None or unique_key is not None: + if dir_path is None or file_name is None: if unique_key is None: raise ValueError("Supply a unique key in order to auto generate a download path.") @@ -233,7 +229,12 @@ def get_download_path( file_name = Path(f"{unique_key}.download") # Otherwise, use the unique key as a sub directory else: - dir_path /= unique_key + assert dir_path is None # sanity check + dir_path = Path(tempfile.gettempdir()) / unique_key + + # If no dir_path is given, use the system's designated folder for temporary files. + if dir_path is None: + dir_path = Path(tempfile.gettempdir()) # Combine the two paths assert file_name is not None diff --git a/tests/unit/utils/test_download.py b/tests/unit/utils/test_download.py index 370eba49..98d74eb3 100644 --- a/tests/unit/utils/test_download.py +++ b/tests/unit/utils/test_download.py @@ -366,6 +366,14 @@ def test_get_download_path(temp_dir: Path): assert path == temp_dir / "file_name.zip" +def test_get_download_path__ignore_unique_key(temp_dir: Path): + # Act + path = get_download_path(dir_path=temp_dir, file_name="file_name.zip", unique_key="foo") + + # Assert + assert path == temp_dir / "file_name.zip" + + def test_get_download_path__auto_dir(): # Act path = get_download_path(file_name="file_name.zip", unique_key="foo")