-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added bindings for listing the registered directories.
- Loading branch information
Showing
4 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|