Skip to content

Commit

Permalink
Added option for non-recursive listing of directory contents.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Nov 25, 2024
1 parent 60eb49b commit 1317074
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/sewerrat/list_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from . import _utils as ut


def list_files(path: str, url: str, force_remote: bool = False) -> List[str]:
def list_files(path: str, url: str, recursive: bool = True, force_remote: bool = False) -> List[str]:
"""
List the contents of a registered directory or a subdirectory thereof.
Expand All @@ -15,6 +15,11 @@ def list_files(path: str, url: str, force_remote: bool = False) -> List[str]:
url:
URL to the SewerRat REST API. Only used for remote access.
recursive:
Whether to list the contents recursively. If False, the contents of
subdirectories are not listed, and the names of directories are
suffxed with ``/`` in the returned list.
force_remote:
Whether to force remote access via the API, even if ``path`` is
on the same filesystem as the caller.
Expand All @@ -31,12 +36,16 @@ def list_files(path: str, url: str, force_remote: bool = False) -> List[str]:
listing.append(os.path.join(rel, f))
else:
listing.append(f)
if not recursive:
for d in dirs:
listing.append(d + "/")
break

return listing

else:
import urllib
res = requests.get(url + "/list?path=" + urllib.parse.quote_plus(path) + "&recursive=true")
res = requests.get(url + "/list?path=" + urllib.parse.quote_plus(path) + "&recursive=" + str(recursive).lower())
if res.status_code >= 300:
raise ut.format_error(res)
return res.json()
3 changes: 3 additions & 0 deletions tests/test_list_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def test_list_files(basic_config):
out = sewerrat.list_files(mydir, url=url)
assert sorted(out) == [ "diet/metadata.json", "metadata.json" ]

out = sewerrat.list_files(mydir, url=url, recursive=False)
assert sorted(out) == [ "diet/", "metadata.json" ]

out = sewerrat.list_files(mydir + "/diet", url=url)
assert sorted(out) == [ "metadata.json" ]

Expand Down

0 comments on commit 1317074

Please sign in to comment.