Skip to content

Commit

Permalink
Update Traversable to provide basic concrete implementations of read_…
Browse files Browse the repository at this point in the history
…bytes and read_text based on open and __truediv__ based on joinpath. Add docstring to TraversableResources.
  • Loading branch information
jaraco committed Jan 19, 2021
1 parent 486f4b4 commit d7ff6e7
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions importlib_resources/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,28 @@ def iterdir(self):
Yield Traversable objects in self
"""

@abc.abstractmethod
def read_bytes(self):
"""
Read contents of self as bytes
"""
with self.open('rb') as strm:
return strm.read()

@abc.abstractmethod
def read_text(self, encoding=None):
"""
Read contents of self as bytes
Read contents of self as text
"""
with self.open(encoding=encoding) as strm:
return strm.read()

@abc.abstractmethod
def is_dir(self):
def is_dir(self) -> bool:
"""
Return True if self is a dir
"""

@abc.abstractmethod
def is_file(self):
def is_file(self) -> bool:
"""
Return True if self is a file
"""
Expand All @@ -89,11 +91,11 @@ def joinpath(self, child):
Return Traversable child in self
"""

@abc.abstractmethod
def __truediv__(self, child):
"""
Return Traversable child in self
"""
return self.joinpath(child)

@abc.abstractmethod
def open(self, mode='r', *args, **kwargs):
Expand All @@ -113,6 +115,11 @@ def name(self) -> str:


class TraversableResources(ResourceReader):
"""
The required interface for providing traversable
resources.
"""

@abc.abstractmethod
def files(self):
"""Return a Traversable object for the loaded package."""
Expand Down

0 comments on commit d7ff6e7

Please sign in to comment.