Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iterdir trailing slash #149

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,10 @@ def _make_child(self: PT, args: list[str]) -> PT:
def _make_child_relpath(self: PT, part: str) -> PT:
# This is an optimization used for dir walking. `part` must be
# a single part relative to this path.
parts = self._parts + [part]
if self._parts[-1:] == [""] and part:
parts = self._parts[:-1] + [part]
else:
parts = self._parts + [part]
return self._from_parsed_parts(
self._drv, self._root, parts, url=self._url, **self._kwargs
)
Expand Down Expand Up @@ -415,8 +418,8 @@ def rglob(self: PT, pattern: str) -> Generator[PT, None, None]:

def _sub_path(self, name):
# only want the path name with iterdir
sp = self._path
return re.sub(f"^({sp}|{sp[1:]})/", "", name)
sp = re.escape(self._path)
return re.sub(f"^({sp}|{sp[1:]})/?", "", name)

def absolute(self: PT) -> PT:
# fsspec paths are always absolute
Expand Down
11 changes: 11 additions & 0 deletions upath/implementations/hdfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ def mkdir(self, path, create_parents=True, **kwargs):
else:
if not kwargs.get("exist_ok", False) and self._fs.exists(pth):
raise FileExistsError(pth)
print(kwargs, self._fs.exists(pth), pth)
return self._fs.mkdir(pth, create_parents=create_parents, **kwargs)

def listdir(self, path, **kwargs):
try:
yield from super().listdir(path, **kwargs)
except OSError as err:
if err.args and err.args[0].startswith(
"GetFileInfo expects base_dir of selector to be a directory"
):
raise NotADirectoryError(path)
raise


class HDFSPath(upath.core.UPath):
_default_accessor = _HDFSAccessor
5 changes: 5 additions & 0 deletions upath/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ def test_iterdir2(self, local_testdir):
assert {p.name for p in pl_iter} == {u.name for u in up_iter}
assert next(self.path.parent.iterdir()).exists()

def test_iterdir_trailing_slash(self):
files_noslash = list(self.path.joinpath("folder1").iterdir())
files_slash = list(self.path.joinpath("folder1/").iterdir())
assert files_noslash == files_slash

def test_parents(self):
p = self.path.joinpath("folder1", "file1.txt")
assert p.is_file()
Expand Down
7 changes: 2 additions & 5 deletions upath/tests/implementations/test_hdfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ def path(self, local_testdir, hdfs):
def test_is_HDFSPath(self):
assert isinstance(self.path, HDFSPath)

def test_chmod(self):
# todo
pass

def test_fsspec_compat(self):
@pytest.mark.skip
def test_makedirs_exist_ok_false(self):
pass