Skip to content

Commit

Permalink
Merge pull request #22 from andrewfulton9/fix_s3_joinpath
Browse files Browse the repository at this point in the history
fixes issue #18
  • Loading branch information
andrewfulton9 authored May 31, 2021
2 parents 9d5f287 + 9192d59 commit dbc57f7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion upath/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Pathlib API extended to use fsspec backends"""
__version__ = "0.0.9"
__version__ = "0.0.10"

from upath.core import UPath
10 changes: 5 additions & 5 deletions upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
class UPath(pathlib.Path):
def __new__(cls, *args, **kwargs):
if cls is UPath:
new_args = list(args)
first_arg = new_args.pop(0)
parsed_url = urllib.parse.urlparse(first_arg)
args_list = list(args)
url = args_list.pop(0)
parsed_url = urllib.parse.urlparse(url)
for key in ["scheme", "netloc"]:
val = kwargs.get(key)
if val:
Expand All @@ -34,8 +34,8 @@ def __new__(cls, *args, **kwargs):
else:
cls = _registry[parsed_url.scheme]
kwargs["_url"] = parsed_url
new_args.insert(0, parsed_url.path)
args = tuple(new_args)
args_list.insert(0, parsed_url.path)
args = tuple(args_list)
self = cls._from_parts_init(args, init=False)
self._init(*args, **kwargs)
return self
20 changes: 20 additions & 0 deletions upath/implementations/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,23 @@ def _sub_path(self, name):
sp = self.path
subed = re.sub(f"^{self._url.netloc}/({sp}|{sp[1:]})/?", "", name)
return subed

def _init(self, *args, template=None, **kwargs):
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 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))
12 changes: 12 additions & 0 deletions upath/tests/implementations/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,15 @@ def test_fsspec_compat(self):
upath2 = UPath(p2, anon=self.anon, **self.s3so)
assert upath2.read_bytes() == content
upath2.unlink()

@pytest.mark.parametrize(
"joiner", [["bucket", "path", "file"], "bucket/path/file"]
)
def test_no_bucket_joinpath(self, joiner):
path = UPath("s3://", anon=self.anon, **self.s3so)
path = path.joinpath(joiner)
assert str(path) == "s3://bucket/path/file"

def test_creating_s3path_with_bucket(self):
path = UPath("s3://", bucket="bucket", anon=self.anon, **self.s3so)
assert str(path) == "s3://bucket/"

0 comments on commit dbc57f7

Please sign in to comment.