Skip to content

Commit

Permalink
Only use unique key for dirs if no dir_path was given
Browse files Browse the repository at this point in the history
Signed-off-by: Bram Stoeller <[email protected]>
  • Loading branch information
bramstoeller committed Mar 7, 2023
1 parent 216f1ed commit 44c764a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/power_grid_model_io/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/utils/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 44c764a

Please sign in to comment.