-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve re-usability of subcomponents (#116)
* Move auxiliary functions and types to separate files * Refactor shared logic for dataset access for users * Move some queries to db module * remove level of nestedness in api response
- Loading branch information
Showing
7 changed files
with
164 additions
and
145 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from typing import Any | ||
|
||
from database.users import User, UserGroup | ||
from schemas.datasets.openml import Visibility | ||
|
||
|
||
def _user_has_access( | ||
dataset: dict[str, Any], | ||
user: User | None = None, | ||
) -> bool: | ||
"""Determine if `user` has the right to view `dataset`.""" | ||
is_public = dataset["visibility"] == Visibility.PUBLIC | ||
return is_public or ( | ||
user is not None and (user.user_id == dataset["uploader"] or UserGroup.ADMIN in user.groups) | ||
) |
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,7 @@ | ||
from enum import IntEnum | ||
|
||
|
||
class DatasetError(IntEnum): | ||
NOT_FOUND = 111 | ||
NO_ACCESS = 112 | ||
NO_DATA_FILE = 113 |
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,40 @@ | ||
import html | ||
from typing import Any | ||
|
||
from schemas.datasets.openml import DatasetFileFormat | ||
|
||
from core.errors import DatasetError | ||
|
||
|
||
def _format_error(*, code: DatasetError, message: str) -> dict[str, str]: | ||
"""Formatter for JSON bodies of OpenML error codes.""" | ||
return {"code": str(code), "message": message} | ||
|
||
|
||
def _format_parquet_url(dataset: dict[str, Any]) -> str | None: | ||
if dataset["format"].lower() != DatasetFileFormat.ARFF: | ||
return None | ||
|
||
minio_base_url = "https://openml1.win.tue.nl" | ||
return f"{minio_base_url}/dataset{dataset['did']}/dataset_{dataset['did']}.pq" | ||
|
||
|
||
def _format_dataset_url(dataset: dict[str, Any]) -> str: | ||
base_url = "https://test.openml.org" | ||
filename = f"{html.escape(dataset['name'])}.{dataset['format'].lower()}" | ||
return f"{base_url}/data/v1/download/{dataset['file_id']}/{filename}" | ||
|
||
|
||
def _safe_unquote(text: str | None) -> str | None: | ||
"""Remove any open and closing quotes and return the remainder if non-empty.""" | ||
if not text: | ||
return None | ||
return text.strip("'\"") or None | ||
|
||
|
||
def _csv_as_list(text: str | None, *, unquote_items: bool = True) -> list[str]: | ||
"""Return comma-separated values in `text` as list, optionally remove quotes.""" | ||
if not text: | ||
return [] | ||
chars_to_strip = "'\"\t " if unquote_items else "\t " | ||
return [item.strip(chars_to_strip) for item in text.split(",")] |
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
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
Oops, something went wrong.