Skip to content

Commit

Permalink
raise in download_to if not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
pjbull committed May 3, 2024
1 parent 71331d3 commit c3ac4ec
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Drop support for Python 3.7; pin minimal `boto3` version to Python 3.8+ versions. (PR [#407](https://github.com/drivendataorg/cloudpathlib/pull/407))
- fix: use native `exists()` method in `GSClient`. (PR [#420](https://github.com/drivendataorg/cloudpathlib/pull/420))
- Enhancement: lazy instantiation of default client (PR [#432](https://github.com/drivendataorg/cloudpathlib/issues/432), Issue [#428](https://github.com/drivendataorg/cloudpathlib/issues/428))
- Adds existence check before downloading in `download_to` (Issue [#430](https://github.com/drivendataorg/cloudpathlib/issues/430), PR [#432](https://github.com/drivendataorg/cloudpathlib/pull/432))

## v0.18.1 (2024-02-26)

Expand Down
5 changes: 5 additions & 0 deletions cloudpathlib/cloudpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def _make_selector(pattern_parts, _flavour, case_sensitive=True):
CloudPathFileExistsError,
CloudPathIsADirectoryError,
CloudPathNotADirectoryError,
CloudPathNotExistsError,
CloudPathNotImplementedError,
DirectoryNotEmptyError,
IncompleteImplementationError,
Expand Down Expand Up @@ -899,6 +900,10 @@ def stat(self, follow_symlinks: bool = True) -> os.stat_result:
# =========== public cloud methods, not in pathlib ===============
def download_to(self, destination: Union[str, os.PathLike]) -> Path:
destination = Path(destination)

if not self.exists():
raise CloudPathNotExistsError(f"Cannot download because path does not exist: {self}")

if self.is_file():
if destination.is_dir():
destination = destination / self.name
Expand Down
4 changes: 4 additions & 0 deletions cloudpathlib/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class CloudPathFileExistsError(CloudPathException, FileExistsError):
pass


class CloudPathNotExistsError(CloudPathException):
pass


class CloudPathIsADirectoryError(CloudPathException, IsADirectoryError):
pass

Expand Down
4 changes: 4 additions & 0 deletions tests/test_cloudpath_file_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from cloudpathlib import CloudPath

from cloudpathlib.exceptions import (
CloudPathNotExistsError,
CloudPathIsADirectoryError,
CloudPathNotImplementedError,
DirectoryNotEmptyError,
Expand Down Expand Up @@ -396,6 +397,9 @@ def test_file_read_writes(rig, tmp_path):
)
assert cloud_rel_paths == dled_rel_paths

with pytest.raises(CloudPathNotExistsError):
(p / "not_exists_file").download_to(dl_file)


def test_dispatch_to_local_cache(rig):
p = rig.create_cloud_path("dir_0/file0_1.txt")
Expand Down

0 comments on commit c3ac4ec

Please sign in to comment.