Skip to content

Commit

Permalink
Make CloudPath Pathlike (#72)
Browse files Browse the repository at this point in the history
* add _fspath__

* add test

* pr changes

* call open file to appease flake8
  • Loading branch information
cszc authored Oct 2, 2020
1 parent 42572f6 commit 008663f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cloudpathlib/cloudpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ def __hash__(self):
def __eq__(self, other: Any):
return repr(self) == repr(other)

def __fspath__(self):
if self.is_file():
self._refresh_cache(force_overwrite_from_cloud=False)
return str(self._local)

# ====================== NOT IMPLEMENTED ======================
# absolute - no cloud equivalent; all cloud paths are absolute already
# as_posix - no cloud equivalent; not needed since we assume url separator
Expand Down Expand Up @@ -277,6 +282,10 @@ def as_uri(self) -> str:
def exists(self) -> bool:
return self.client._exists(self)

@property
def fspath(self) -> str:
return self.__fspath__()

def glob(self, pattern: str) -> Iterable["CloudPath"]:
# strip cloud prefix from pattern if it is included
if pattern.startswith(self.cloud_prefix):
Expand Down
18 changes: 18 additions & 0 deletions tests/test_cloudpath_instantiation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import pytest

from cloudpathlib import AzureBlobPath, CloudPath, InvalidPrefix, MissingDependencies, S3Path
Expand Down Expand Up @@ -60,3 +62,19 @@ def test_dependencies_not_loaded(rig, monkeypatch):
CloudPath(f"{rig.cloud_prefix}/bucket/dir_0/file0_0.txt")
with pytest.raises(MissingDependencies):
rig.create_cloud_path("bucket/dir_0/file0_0.txt")


def test_is_pathlike(rig):
p = rig.create_cloud_path("bucket")
assert isinstance(p, os.PathLike)


def test_fspath(rig):
p = rig.create_cloud_path("bucket")
os.fspath(p)


def test_os_open(rig):
p = rig.create_cloud_path("bucket/dir_0/file0_0.txt")
with open(p, "r") as f:
assert f.readable()

0 comments on commit 008663f

Please sign in to comment.