Skip to content

Commit

Permalink
Change: Extend download context manager to accept string as destination
Browse files Browse the repository at this point in the history
If a file should be downloaded to the current working dir it is nicer
to pass a string as destination argument to the download context
manager function.
  • Loading branch information
bjoernricks committed Sep 20, 2022
1 parent c87b238 commit cc5cf9d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions pontos/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import subprocess
from contextlib import contextmanager
from pathlib import Path
from typing import Generator, Iterable, Iterator, Optional
from typing import Generator, Iterable, Iterator, Optional, Union

import httpx

Expand Down Expand Up @@ -86,7 +86,7 @@ def run(self):
@contextmanager
def download(
url: str,
destination: Optional[Path] = None,
destination: Optional[Union[Path, str]] = None,
*,
chunk_size: int = DEFAULT_CHUNK_SIZE,
timeout: int = DEFAULT_TIMEOUT,
Expand All @@ -113,7 +113,9 @@ def download(
for progress in progress_it:
print(progress)
"""
destination = Path(url.split("/")[-1]) if not destination else destination
destination = (
Path(url.split("/")[-1]) if not destination else Path(destination)
)

with httpx.stream(
"GET", url, timeout=timeout, follow_redirects=True
Expand Down
8 changes: 4 additions & 4 deletions tests/github/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def test_release(self, requests_mock: MagicMock):

self.assertEqual(data["id"], 52499047)

@patch("pontos.github.api.Path")
@patch("pontos.helper.Path")
@patch("pontos.github.api.httpx.stream")
def test_download_release_tarball(
self, requests_mock: MagicMock, path_mock: MagicMock
Expand Down Expand Up @@ -288,7 +288,7 @@ def test_download_release_tarball(
with self.assertRaises(StopIteration):
next(it)

@patch("pontos.github.api.Path")
@patch("pontos.helper.Path")
@patch("pontos.github.api.httpx.stream")
def test_download_release_tarball_with_content_length(
self, requests_mock: MagicMock, path_mock: MagicMock
Expand Down Expand Up @@ -328,7 +328,7 @@ def test_download_release_tarball_with_content_length(
with self.assertRaises(StopIteration):
next(it)

@patch("pontos.github.api.Path")
@patch("pontos.helper.Path")
@patch("pontos.github.api.httpx.stream")
def test_download_release_zip(
self, requests_mock: MagicMock, path_mock: MagicMock
Expand Down Expand Up @@ -442,7 +442,7 @@ def test_added_files_in_pr(self, requests_mock: MagicMock):
},
)

@patch("pontos.github.api.Path")
@patch("pontos.helper.Path")
@patch("pontos.github.api.httpx.get")
@patch("pontos.github.api.httpx.stream")
def test_download_release_assets(
Expand Down

0 comments on commit cc5cf9d

Please sign in to comment.