Skip to content

Commit dbc57f7

Browse files
Merge pull request #22 from andrewfulton9/fix_s3_joinpath
fixes issue #18
2 parents 9d5f287 + 9192d59 commit dbc57f7

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

upath/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""Pathlib API extended to use fsspec backends"""
2-
__version__ = "0.0.9"
2+
__version__ = "0.0.10"
33

44
from upath.core import UPath

upath/core.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
class UPath(pathlib.Path):
1111
def __new__(cls, *args, **kwargs):
1212
if cls is UPath:
13-
new_args = list(args)
14-
first_arg = new_args.pop(0)
15-
parsed_url = urllib.parse.urlparse(first_arg)
13+
args_list = list(args)
14+
url = args_list.pop(0)
15+
parsed_url = urllib.parse.urlparse(url)
1616
for key in ["scheme", "netloc"]:
1717
val = kwargs.get(key)
1818
if val:
@@ -34,8 +34,8 @@ def __new__(cls, *args, **kwargs):
3434
else:
3535
cls = _registry[parsed_url.scheme]
3636
kwargs["_url"] = parsed_url
37-
new_args.insert(0, parsed_url.path)
38-
args = tuple(new_args)
37+
args_list.insert(0, parsed_url.path)
38+
args = tuple(args_list)
3939
self = cls._from_parts_init(args, init=False)
4040
self._init(*args, **kwargs)
4141
return self

upath/implementations/s3.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,23 @@ def _sub_path(self, name):
2424
sp = self.path
2525
subed = re.sub(f"^{self._url.netloc}/({sp}|{sp[1:]})/?", "", name)
2626
return subed
27+
28+
def _init(self, *args, template=None, **kwargs):
29+
if kwargs.get("bucket") and kwargs.get("_url"):
30+
bucket = kwargs.pop("bucket")
31+
kwargs["_url"] = kwargs["_url"]._replace(netloc=bucket)
32+
super()._init(*args, template=template, **kwargs)
33+
34+
def joinpath(self, *args):
35+
if self._url.netloc:
36+
return super().joinpath(*args)
37+
# handles a bucket in the path
38+
else:
39+
path = args[0]
40+
if isinstance(path, list):
41+
args_list = list(*args)
42+
else:
43+
args_list = path.split(self._flavour.sep)
44+
bucket = args_list.pop(0)
45+
self._kwargs["bucket"] = bucket
46+
return super().joinpath(*tuple(args_list))

upath/tests/implementations/test_s3.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,15 @@ def test_fsspec_compat(self):
8383
upath2 = UPath(p2, anon=self.anon, **self.s3so)
8484
assert upath2.read_bytes() == content
8585
upath2.unlink()
86+
87+
@pytest.mark.parametrize(
88+
"joiner", [["bucket", "path", "file"], "bucket/path/file"]
89+
)
90+
def test_no_bucket_joinpath(self, joiner):
91+
path = UPath("s3://", anon=self.anon, **self.s3so)
92+
path = path.joinpath(joiner)
93+
assert str(path) == "s3://bucket/path/file"
94+
95+
def test_creating_s3path_with_bucket(self):
96+
path = UPath("s3://", bucket="bucket", anon=self.anon, **self.s3so)
97+
assert str(path) == "s3://bucket/"

0 commit comments

Comments
 (0)