From 5d1417b1cdb82eb5eb52271d386c14312ea05227 Mon Sep 17 00:00:00 2001 From: Christine Date: Fri, 4 Sep 2020 18:04:52 -0400 Subject: [PATCH 1/4] add _fspath__ --- cloudpathlib/cloudpath.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cloudpathlib/cloudpath.py b/cloudpathlib/cloudpath.py index 31c93803..eff70663 100644 --- a/cloudpathlib/cloudpath.py +++ b/cloudpathlib/cloudpath.py @@ -194,6 +194,8 @@ def __hash__(self): def __eq__(self, other: Any): return repr(self) == repr(other) + def __fspath__(self): + return str(self) # ====================== NOT IMPLEMENTED ====================== # absolute - no cloud equivalent; all cloud paths are absolute already # as_posix - no cloud equivalent; not needed since we assume url separator From 9048ee88a49e10cccf2e22b636c0fa1345959e37 Mon Sep 17 00:00:00 2001 From: Christine Date: Fri, 4 Sep 2020 18:05:06 -0400 Subject: [PATCH 2/4] add test --- tests/test_azure_specific.py | 7 +++++++ tests/test_s3_specific.py | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/tests/test_azure_specific.py b/tests/test_azure_specific.py index 80e75643..4f42f079 100644 --- a/tests/test_azure_specific.py +++ b/tests/test_azure_specific.py @@ -1,3 +1,5 @@ +import os + from cloudpathlib import AzureBlobPath @@ -9,3 +11,8 @@ def test_azureblobpath_properties(azure_rig): p2 = AzureBlobPath("az://container/") assert p2.blob == "" assert p2.container == "container" + + +def test_azureblobpath_is_path(azure_rig): + p = AzureBlobPath("az://container") + assert isinstance(p, os.PathLike) diff --git a/tests/test_s3_specific.py b/tests/test_s3_specific.py index c343490d..23731a89 100644 --- a/tests/test_s3_specific.py +++ b/tests/test_s3_specific.py @@ -1,3 +1,5 @@ +import os + from cloudpathlib import S3Path @@ -9,3 +11,8 @@ def test_s3path_properties(s3_rig): p2 = S3Path("s3://bucket/") assert p2.key == "" assert p2.bucket == "bucket" + + +def test_s3path_is_path(s3_rig): + p = S3Path("s3://bucket") + assert isinstance(p, os.PathLike) From 26a657123934305bc6c6c842928d72c91ee35ba2 Mon Sep 17 00:00:00 2001 From: Christine Date: Fri, 2 Oct 2020 17:22:14 -0400 Subject: [PATCH 3/4] pr changes --- cloudpathlib/cloudpath.py | 9 ++++++++- tests/test_azure_specific.py | 7 ------- tests/test_cloudpath_instantiation.py | 18 ++++++++++++++++++ tests/test_s3_specific.py | 7 ------- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/cloudpathlib/cloudpath.py b/cloudpathlib/cloudpath.py index 2fff4332..6d285e36 100644 --- a/cloudpathlib/cloudpath.py +++ b/cloudpathlib/cloudpath.py @@ -208,7 +208,10 @@ def __eq__(self, other: Any): return repr(self) == repr(other) def __fspath__(self): - return str(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 @@ -279,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): diff --git a/tests/test_azure_specific.py b/tests/test_azure_specific.py index 4f42f079..80e75643 100644 --- a/tests/test_azure_specific.py +++ b/tests/test_azure_specific.py @@ -1,5 +1,3 @@ -import os - from cloudpathlib import AzureBlobPath @@ -11,8 +9,3 @@ def test_azureblobpath_properties(azure_rig): p2 = AzureBlobPath("az://container/") assert p2.blob == "" assert p2.container == "container" - - -def test_azureblobpath_is_path(azure_rig): - p = AzureBlobPath("az://container") - assert isinstance(p, os.PathLike) diff --git a/tests/test_cloudpath_instantiation.py b/tests/test_cloudpath_instantiation.py index 6495de55..6dc8cd78 100644 --- a/tests/test_cloudpath_instantiation.py +++ b/tests/test_cloudpath_instantiation.py @@ -1,3 +1,5 @@ +import os + import pytest from cloudpathlib import AzureBlobPath, CloudPath, InvalidPrefix, MissingDependencies, S3Path @@ -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: + pass diff --git a/tests/test_s3_specific.py b/tests/test_s3_specific.py index 23731a89..c343490d 100644 --- a/tests/test_s3_specific.py +++ b/tests/test_s3_specific.py @@ -1,5 +1,3 @@ -import os - from cloudpathlib import S3Path @@ -11,8 +9,3 @@ def test_s3path_properties(s3_rig): p2 = S3Path("s3://bucket/") assert p2.key == "" assert p2.bucket == "bucket" - - -def test_s3path_is_path(s3_rig): - p = S3Path("s3://bucket") - assert isinstance(p, os.PathLike) From fcc32cf73f86c4398bfaf45d0a4481f62d8dbf24 Mon Sep 17 00:00:00 2001 From: Christine Date: Fri, 2 Oct 2020 17:33:11 -0400 Subject: [PATCH 4/4] call open file to appease flake8 --- tests/test_cloudpath_instantiation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cloudpath_instantiation.py b/tests/test_cloudpath_instantiation.py index 6dc8cd78..e70757f0 100644 --- a/tests/test_cloudpath_instantiation.py +++ b/tests/test_cloudpath_instantiation.py @@ -77,4 +77,4 @@ def test_fspath(rig): def test_os_open(rig): p = rig.create_cloud_path("bucket/dir_0/file0_0.txt") with open(p, "r") as f: - pass + assert f.readable()