Skip to content

Commit

Permalink
Merge pull request #249 from python/bugfix/resource-container-pathsep
Browse files Browse the repository at this point in the history
Honor '/'-separated names in ResourceContainer.joinpath.
  • Loading branch information
jaraco committed Apr 17, 2022
2 parents 8a17e81 + 07213e3 commit 8dceab3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
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

0 comments on commit 8dceab3

Please sign in to comment.