Skip to content

Commit

Permalink
Base._ls(): may always return an empty list (#178)
Browse files Browse the repository at this point in the history
* Base.ls(): always return

* DOC: update

* DOC: update comment

* Update audbackend/core/backend/base.py

Co-authored-by: Hagen Wierstorf <hwierstorf@audeering.com>

---------

Co-authored-by: Hagen Wierstorf <hwierstorf@audeering.com>
  • Loading branch information
frankenjoe and hagenw authored Feb 5, 2024
1 parent 65c6844 commit 4647863
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 37 deletions.
14 changes: 2 additions & 12 deletions audbackend/core/backend/artifactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,7 @@ def _exists(
path: str,
) -> bool:
r"""Check if file exists on backend."""
path = self._expand(path)
path = _artifactory_path(
path,
self._username,
self._api_key,
)
path = self._path(path)
return path.exists()

def _expand(
Expand Down Expand Up @@ -265,13 +260,8 @@ def _ls(
) -> typing.List[str]:
r"""List all files under sub-path."""
path = self._path(path)
path = _artifactory_path(
path,
self._username,
self._api_key,
)
if not path.exists():
utils.raise_file_not_found_error(str(path))
return []

paths = [str(x) for x in path.glob("**/*") if x.is_file()]
paths = [self._collapse(path) for path in paths]
Expand Down
24 changes: 11 additions & 13 deletions audbackend/core/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,8 @@ def _ls(
) -> typing.List[str]: # pragma: no cover
r"""List all files under sub-path.
If ``path`` is ``'/'`` and no files exist on the repository,
an empty list should be returned
Otherwise,
if ``path`` does not exist or no files are found under ``path``,
an error should be raised.
If ``path`` does not exist
an empty list can be returned.
"""
raise NotImplementedError()
Expand Down Expand Up @@ -419,17 +416,18 @@ def ls(
if self.exists(path):
paths = [path]
else:
if not suppress_backend_errors:
# since the backend does no longer raise an error
# if the path does not exist
# we have to do it
try:
raise utils.raise_file_not_found_error(path)
except FileNotFoundError as ex:
raise BackendError(ex)
paths = []

if not paths:

if path != '/' and not suppress_backend_errors:
# if the path does not exist
# we raise an error
try:
raise utils.raise_file_not_found_error(path)
except FileNotFoundError as ex:
raise BackendError(ex)

return []

paths = sorted(paths)
Expand Down
3 changes: 2 additions & 1 deletion audbackend/core/backend/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def _ls(
r"""List all files under sub-path."""
path = self._expand(path)
if not os.path.exists(path):
utils.raise_file_not_found_error(path)
return []

paths = audeer.list_file_names(
path,
recursive=True,
Expand Down
8 changes: 0 additions & 8 deletions docs/developer-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -544,14 +544,6 @@ we provide a listing method.
ls = db.execute(query, [path]).fetchall()
ls = [x[0] for x in ls]

if not ls and not path == '/':
# path has to exists if not root
raise FileNotFoundError(
errno.ENOENT,
os.strerror(errno.ENOENT),
path,
)

return ls


Expand Down
3 changes: 0 additions & 3 deletions tests/singlefolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ def _ls(
if p.startswith(path):
ls.append(p)

if not ls and not path == '/':
raise audbackend.core.utils.raise_file_not_found_error(path)

return ls

def _owner(
Expand Down

0 comments on commit 4647863

Please sign in to comment.