Skip to content
Open
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
10 changes: 10 additions & 0 deletions etils/epath/resource_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ def __eq__(self, other) -> bool:
def __hash__(self) -> int:
return hash((self.root, self.at)) # pytype: disable=attribute-error

def resolve(self, strict: bool = False) -> 'ResourcePath':
"""Returns the absolute path."""
if strict and not self.exists():
raise FileNotFoundError(f'{self} not found.')
return self

def absolute(self) -> 'ResourcePath':
"""Returns the absolute path."""
return self.resolve()

if sys.version_info < (3, 10):
# Required due to: https://bugs.python.org/issue42043
def _next(self, at) -> 'ResourcePath': # pylint: disable=g-wrong-blank-lines
Expand Down
16 changes: 15 additions & 1 deletion etils/epath/resource_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,19 @@ def test_resource_path():
path = epath.Path(path)
assert isinstance(path, epath.resource_utils.ResourcePath)

assert path.joinpath() == path
assert path.joinpath('abc', 'def.txt').name == 'def.txt'


def test_resource_resolve():
path = epath.g3_path()
# This should not raise AttributeError
resolved = path.resolve()
assert resolved == path
assert resolved.resolve() == resolved


def test_resource_path_resolve():
path = epath.resource_utils.ResourcePath(_make_zip_file())
# This would have failed before the fix
resolved = path.resolve()
assert resolved == path
Loading