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

Honor '/'-separated names in ResourceContainer.joinpath. #249

Merged
merged 2 commits into from Apr 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.rst
@@ -1,3 +1,9 @@
v5.7.1
======

* #249: In ``simple.ResourceContainer.joinpath``, honor
names split by ``posixpath.sep``.

v5.7.0
======

Expand Down
16 changes: 11 additions & 5 deletions importlib_resources/simple.py
Expand Up @@ -99,13 +99,19 @@ def iterdir(self):
def open(self, *args, **kwargs):
raise IsADirectoryError()

def joinpath(self, *names):
if not names:
@staticmethod
def _flatten(compound_names):
for name in compound_names:
yield from name.split('/')

def joinpath(self, *descendants):
if not descendants:
return self
name, rest = names[0], names[1:]
names = self._flatten(descendants)
target = next(names)
return next(
traversable for traversable in self.iterdir() if traversable.name == name
).joinpath(*rest)
traversable for traversable in self.iterdir() if traversable.name == target
).joinpath(*names)


class TraversableReader(TraversableResources, SimpleReader):
Expand Down