Skip to content

Commit

Permalink
Merge pull request #248 from python/bpo-47142/refine-Traversable
Browse files Browse the repository at this point in the history
bpo-47142: Refine Traversable.
  • Loading branch information
jaraco committed Apr 13, 2022
2 parents 4bcd1e6 + ab11f01 commit 21d703e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Expand Up @@ -4,6 +4,7 @@ omit =
*/.tox/*
*/_itertools.py
*/_legacy.py
*/simple.py

[report]
show_missing = True
11 changes: 9 additions & 2 deletions importlib_resources/abc.py
Expand Up @@ -55,6 +55,9 @@ class Traversable(Protocol):
"""
An object with a subset of pathlib.Path methods suitable for
traversing directories and opening files.
Any exceptions that occur when accessing the backing resource
may propagate unaltered.
"""

@abc.abstractmethod
Expand Down Expand Up @@ -90,9 +93,13 @@ def is_file(self) -> bool:
"""

@abc.abstractmethod
def joinpath(self, child: StrPath) -> "Traversable":
def joinpath(self, *descendants: StrPath) -> "Traversable":
"""
Return Traversable child in self
Return Traversable resolved with any descendants applied.
Each descendant should be a path segment relative to self
and each may contain multiple levels separated by
``posixpath.sep`` (``/``).
"""

def __truediv__(self, child: StrPath) -> "Traversable":
Expand Down
7 changes: 5 additions & 2 deletions importlib_resources/simple.py
Expand Up @@ -99,10 +99,13 @@ def iterdir(self):
def open(self, *args, **kwargs):
raise IsADirectoryError()

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


class TraversableReader(TraversableResources, SimpleReader):
Expand Down

0 comments on commit 21d703e

Please sign in to comment.