Skip to content

Commit

Permalink
Added bindings for listing the registered directories.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Sep 15, 2024
1 parent 0462be7 commit 0453375
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/sewerrat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@
from .retrieve_directory import retrieve_directory
from .retrieve_file import retrieve_file
from .retrieve_metadata import retrieve_metadata
from .list_registered_directories import list_registered_directories
35 changes: 35 additions & 0 deletions src/sewerrat/list_registered_directories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from typing import Optional, Union
import requests


def list_registered_directories(url: str, user: Optional[Union[str, bool]] = None):
"""
List all registered directories in the SewerRat instance.
Args:
url:
URL to the SewerRat REST API.
user:
Name of a user, used to filter the returned directories based on
the user who registered them. Alternatively True, to automatically
use the name of the current user.
Returns:
List of objects where each object corresponds to a registered directory
and contains the `path` to the directory, the `user` who registered it,
the Unix epoch `time` of the registration, and the `names` of the
metadata files to be indexed.
"""
if user == True:
import getpass
user = getpass.getuser()

url += "/registered"
if not user is None and user != False:
url += "?user=" + user

res = requests.get(url)
if res.status_code >= 300:
raise ut.format_error(res)
return res.json()
2 changes: 1 addition & 1 deletion src/sewerrat/start_sewerrat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
test_api_port = None


def start_sewerrat(db: Optional[str] = None, port: Optional[int] = None, wait: float = 1, version: str = "1.0.6", overwrite: bool = False) -> Tuple[bool, int]:
def start_sewerrat(db: Optional[str] = None, port: Optional[int] = None, wait: float = 1, version: str = "1.0.9", overwrite: bool = False) -> Tuple[bool, int]:
"""
Start a test SewerRat service.
Expand Down
34 changes: 34 additions & 0 deletions tests/test_registered_directories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import sewerrat
import os
import tempfile
import time


def test_list_registered_directories():
mydir = tempfile.mkdtemp()
with open(os.path.join(mydir, "metadata.json"), "w") as handle:
handle.write('{ "first": "Aaron", "last": "Lun" }')

_, url = sewerrat.start_sewerrat()

sewerrat.register(mydir, ["metadata.json"], url=url)
try:
regged = sewerrat.list_registered_directories(url)
assert len(regged) > 0

found = False
for x in regged:
if x["path"] == mydir:
found = True
assert x["names"] == [ "metadata.json" ]
assert found

filtered = sewerrat.list_registered_directories(url, user=True)
assert regged == filtered

filtered = sewerrat.list_registered_directories(url, user=regged[0]["user"] + "_asdasdasd")
assert len(filtered) == 0

finally:
sewerrat.deregister(mydir, url=url)

0 comments on commit 0453375

Please sign in to comment.