Skip to content

bpo-47142: Refine Traversable. #248

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

Merged
merged 3 commits into from
Apr 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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