-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from andrewfulton9/pr36
Adds GCSPath
- Loading branch information
Showing
9 changed files
with
218 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,7 +36,7 @@ test = [ | |
"pylint", | ||
"pytest", | ||
"requests", | ||
"s3fs", | ||
"s3fs" | ||
] | ||
|
||
[tool.flit.scripts] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import upath.core | ||
import os | ||
import re | ||
|
||
|
||
class _GCSAccessor(upath.core._FSSpecAccessor): | ||
def __init__(self, parsed_url, *args, **kwargs): | ||
super().__init__(parsed_url, *args, **kwargs) | ||
|
||
def _format_path(self, s): | ||
""" | ||
netloc has already been set to project via `GCSPath._init` | ||
""" | ||
s = os.path.join(self._url.netloc, s.lstrip("/")) | ||
return s | ||
|
||
|
||
# project is not part of the path, but is part of the credentials | ||
class GCSPath(upath.core.UPath): | ||
_default_accessor = _GCSAccessor | ||
|
||
def _init(self, *args, template=None, **kwargs): | ||
# ensure that the bucket is part of the netloc path | ||
if kwargs.get("bucket") and kwargs.get("_url"): | ||
bucket = kwargs.pop("bucket") | ||
kwargs["_url"] = kwargs["_url"]._replace(netloc=bucket) | ||
super()._init(*args, template=template, **kwargs) | ||
|
||
def _sub_path(self, name): | ||
"""gcs returns path as `{bucket}/<path>` with listdir | ||
and glob, so here we can add the netloc to the sub string | ||
so it gets subbed out as well | ||
""" | ||
sp = self.path | ||
subed = re.sub(f"^({self._url.netloc})?/?({sp}|{sp[1:]})/?", "", name) | ||
return subed | ||
|
||
def joinpath(self, *args): | ||
if self._url.netloc: | ||
return super().joinpath(*args) | ||
# handles a bucket in the path | ||
else: | ||
path = args[0] | ||
if isinstance(path, list): | ||
args_list = list(*args) | ||
else: | ||
args_list = path.split(self._flavour.sep) | ||
bucket = args_list.pop(0) | ||
self._kwargs["bucket"] = bucket | ||
return super().joinpath(*tuple(args_list)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import pytest | ||
import sys | ||
|
||
from upath import UPath | ||
from upath.implementations.gcs import GCSPath | ||
from upath.errors import NotDirectoryError | ||
from upath.tests.cases import BaseTests | ||
|
||
|
||
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Windows bad") | ||
@pytest.mark.usefixtures("path") | ||
class TestGCSPath(BaseTests): | ||
@pytest.fixture(autouse=True, scope="function") | ||
def path(self, local_testdir, gcs): | ||
scheme = "gs:/" | ||
self.path = UPath(f"{scheme}{local_testdir}", endpoint_url=gcs) | ||
self.endpoint_url = gcs | ||
|
||
def test_is_GCSPath(self): | ||
assert isinstance(self.path, GCSPath) | ||
|
||
def test_mkdir(self): | ||
new_dir = self.path.joinpath("new_dir") | ||
new_dir.joinpath("test.txt").touch() | ||
assert new_dir.exists() | ||
|
||
def test_glob(self, pathlib_base): | ||
mock_glob = list(self.path.glob("**.txt")) | ||
path_glob = list(pathlib_base.glob("**/*.txt")) | ||
|
||
assert len(mock_glob) == len(path_glob) | ||
assert all( | ||
map(lambda m: m.path in [str(p)[4:] for p in path_glob], mock_glob) | ||
) | ||
|
||
def test_rmdir(self, local_testdir): | ||
dirname = "rmdir_test" | ||
mock_dir = self.path.joinpath(dirname) | ||
mock_dir.joinpath("test.txt").write_text("hello") | ||
mock_dir.fs.invalidate_cache() | ||
mock_dir.rmdir() | ||
assert not mock_dir.exists() | ||
with pytest.raises(NotDirectoryError): | ||
self.path.joinpath("file1.txt").rmdir() | ||
|
||
def test_fsspec_compat(self): | ||
fs = self.path.fs | ||
scheme = self.path._url.scheme | ||
content = b"a,b,c\n1,2,3\n4,5,6" | ||
|
||
p1 = f"{scheme}:///tmp/output1.csv" | ||
upath1 = UPath(p1, endpoint_url=self.endpoint_url) | ||
upath1.write_bytes(content) | ||
with fs.open(p1) as f: | ||
assert f.read() == content | ||
upath1.unlink() | ||
|
||
# write with fsspec, read with upath | ||
p2 = f"{scheme}:///tmp/output2.csv" | ||
with fs.open(p2, "wb") as f: | ||
f.write(content) | ||
upath2 = UPath(p2, endpoint_url=self.endpoint_url) | ||
assert upath2.read_bytes() == content | ||
upath2.unlink() |