Skip to content

Commit a0930d3

Browse files
committed
Use unique key as a sub diretory if both a file name and unique key are supplied
Signed-off-by: Bram Stoeller <bram.stoeller@alliander.com>
1 parent 94f2c36 commit a0930d3

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

src/power_grid_model_io/utils/download.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,26 @@ def get_download_path(
214214
unique_key: A unique string that can be used to generate a filename (e.g. a url).
215215
"""
216216

217-
# If no file_name is given, generate a file name
218-
if file_name is None:
217+
# If no dir_path is given, use the system's designated folder for temporary files.
218+
if dir_path is None:
219+
dir_path = Path(tempfile.gettempdir())
220+
221+
# If no specific download path was given, we need to generate a unique key (based on the given unique key)
222+
if file_name is None or unique_key is not None:
219223
if unique_key is None:
220-
raise ValueError("Supply data in order to auto generate a download path.")
224+
raise ValueError("Supply a unique key in order to auto generate a download path.")
221225

222226
sha256 = hashlib.sha256()
223227
sha256.update(unique_key.encode())
224-
hash_str = base64.b64encode(sha256.digest()).decode("ascii")
225-
hash_str = hash_str.replace("/", "_").replace("+", "-").rstrip("=")
226-
file_name = Path(f"{hash_str}.download")
228+
unique_key = base64.b64encode(sha256.digest()).decode("ascii")
229+
unique_key = unique_key.replace("/", "_").replace("+", "-").rstrip("=")
227230

228-
# If no dir_path is given, use the system's designated folder for temporary files
229-
elif dir_path is None:
230-
dir_path = Path(tempfile.gettempdir())
231+
# If no file name was given, use the unique key as a file name
232+
if file_name is None:
233+
file_name = Path(f"{unique_key}.download")
234+
# Otherwise, use the unique key as a sub directory
235+
else:
236+
dir_path /= unique_key
231237

232238
# Combine the two paths
233239
assert file_name is not None

tests/unit/utils/test_download.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
Response = namedtuple("Response", ["status", "headers"])
2424

25+
# The base64 representation of the sha256 hash of "foo" is LCa0a2j/xo/5m0U8HTBBNBNCLXBkg7+g+YpeiGJm564=
26+
# The / and + will be replaced with a _ and - character and the trailing = character(s) will be removed.
27+
FOO_KEY = "LCa0a2j_xo_5m0U8HTBBNBNCLXBkg7-g-YpeiGJm564"
28+
2529

2630
@pytest.fixture()
2731
def temp_dir():
@@ -356,25 +360,23 @@ def test_get_response_info__no_length(mock_urlopen):
356360

357361
def test_get_download_path(temp_dir: Path):
358362
# Act
359-
path = get_download_path(dir_path=temp_dir, file_name="file_name.zip", unique_key="foo")
363+
path = get_download_path(dir_path=temp_dir, file_name="file_name.zip")
360364

361365
# Assert
362366
assert path == temp_dir / "file_name.zip"
363367

364368

365369
def test_get_download_path__auto_dir():
366370
# Act
367-
path = get_download_path(file_name="file_name.zip")
371+
path = get_download_path(file_name="file_name.zip", unique_key="foo")
368372

369373
# Assert
370-
assert path == Path(tempfile.gettempdir()).resolve() / "file_name.zip"
374+
assert path == Path(tempfile.gettempdir()).resolve() / FOO_KEY / "file_name.zip"
371375

372376

373377
def test_get_download_path__auto_file_name(temp_dir: Path):
374378
# Arrange
375-
# The base64 representation of the sha256 hash of "foo" is LCa0a2j/xo/5m0U8HTBBNBNCLXBkg7+g+YpeiGJm564=
376-
# The / and + will be replaced with a _ and - character and the trailing = character(s) will be removed.
377-
expected_file_name = "LCa0a2j_xo_5m0U8HTBBNBNCLXBkg7-g-YpeiGJm564.download"
379+
expected_file_name = f"{FOO_KEY}.download"
378380

379381
# Act
380382
path = get_download_path(dir_path=temp_dir, unique_key="foo")
@@ -385,7 +387,7 @@ def test_get_download_path__auto_file_name(temp_dir: Path):
385387

386388
def test_get_download_path__missing_data(temp_dir: Path):
387389
# Act / Assert
388-
with pytest.raises(ValueError, match=r"Supply data in order to auto generate a download path\."):
390+
with pytest.raises(ValueError, match=r"Supply a unique key in order to auto generate a download path\."):
389391
get_download_path(dir_path=temp_dir)
390392

391393

tests/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ def eq(left, right) -> bool:
178178
return False
179179
if isinstance(left, NDFrame):
180180
return (left == right).all()
181+
if isinstance(right, NDFrame):
182+
return False
181183
if isnan(left) and isnan(right):
182184
return True
183185
return left == right

0 commit comments

Comments
 (0)