Skip to content

Commit 1a26784

Browse files
committed
python(refactor): renamed arguments to be more descriptive, added try/catch for zip
1 parent eb9f9c5 commit 1a26784

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

  • python/lib/sift_client/_internal/util

python/lib/sift_client/_internal/util/file.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from __future__ import annotations
22

3+
import warnings
34
import zipfile
45
from typing import TYPE_CHECKING
56

7+
from sift_client.errors import SiftWarning
8+
69
if TYPE_CHECKING:
710
from pathlib import Path
811

912
from sift_client.transport.rest_transport import RestClient
1013

1114

12-
def download_file(url: str, dest: Path, *, rest_client: RestClient) -> Path:
15+
def download_file(signed_url: str, output_path: Path, *, rest_client: RestClient) -> Path:
1316
"""Download a file from a URL in streaming 4 MiB chunks.
1417
1518
Args:
@@ -23,15 +26,15 @@ def download_file(url: str, dest: Path, *, rest_client: RestClient) -> Path:
2326
Raises:
2427
requests.HTTPError: If the download request fails.
2528
"""
26-
dest.parent.mkdir(parents=True, exist_ok=True)
29+
output_path.parent.mkdir(parents=True, exist_ok=True)
2730
# Strip the session's default Authorization header, presigned URLs carry their own auth
28-
with rest_client.get(url, stream=True, headers={"Authorization": None}) as response:
31+
with rest_client.get(signed_url, stream=True, headers={"Authorization": None}) as response:
2932
response.raise_for_status()
30-
with dest.open("wb") as file:
33+
with output_path.open("wb") as file:
3134
for chunk in response.iter_content(chunk_size=4194304): # 4 MiB
3235
if chunk:
3336
file.write(chunk)
34-
return dest
37+
return output_path
3538

3639

3740
def extract_zip(zip_path: Path, output_dir: Path, *, delete_zip: bool = True) -> list[Path]:
@@ -53,5 +56,8 @@ def extract_zip(zip_path: Path, output_dir: Path, *, delete_zip: bool = True) ->
5356
names = zip_file.namelist()
5457
zip_file.extractall(output_dir)
5558
if delete_zip:
56-
zip_path.unlink()
59+
try:
60+
zip_path.unlink()
61+
except OSError:
62+
warnings.warn(f"Failed to delete zip file '{zip_path}'", SiftWarning, stacklevel=2)
5763
return [output_dir / name for name in names if not name.endswith("/")]

0 commit comments

Comments
 (0)