Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: retrieve onto IRIs together with project IRI #82

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions dsp_permissions_scripts/ap/ap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from dsp_permissions_scripts.models.errors import ApiError
from dsp_permissions_scripts.utils.dsp_client import DspClient
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils.project import get_project_iri_by_shortcode
from dsp_permissions_scripts.utils.project import (
get_project_iri_and_onto_iris_by_shortcode,
)
jnussbaum marked this conversation as resolved.
Show resolved Hide resolved

logger = get_logger(__name__)

Expand Down Expand Up @@ -50,7 +52,7 @@ def _get_all_aps_of_project(project_iri: str, dsp_client: DspClient) -> list[Ap]
def get_aps_of_project(shortcode: str, dsp_client: DspClient) -> list[Ap]:
"""Returns the Administrative Permissions for a project."""
logger.info("****** Retrieving all Administrative Permissions... ******")
project_iri = get_project_iri_by_shortcode(shortcode, dsp_client)
project_iri, _ = get_project_iri_and_onto_iris_by_shortcode(shortcode, dsp_client)
aps = _get_all_aps_of_project(project_iri, dsp_client)
logger.info(f"Retrieved {len(aps)} Administrative Permissions")
return aps
6 changes: 4 additions & 2 deletions dsp_permissions_scripts/doap/doap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
from dsp_permissions_scripts.models.errors import ApiError
from dsp_permissions_scripts.utils.dsp_client import DspClient
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils.project import get_project_iri_by_shortcode
from dsp_permissions_scripts.utils.project import (
get_project_iri_and_onto_iris_by_shortcode,
)
from dsp_permissions_scripts.utils.scope_serialization import (
create_scope_from_admin_route_object,
)
Expand Down Expand Up @@ -72,7 +74,7 @@ def get_doaps_of_project(
By default, all DOAPs are returned, regardless of their target (target=all).
"""
logger.info("****** Retrieving all DOAPs... ******")
project_iri = get_project_iri_by_shortcode(shortcode, dsp_client)
project_iri, _ = get_project_iri_and_onto_iris_by_shortcode(shortcode, dsp_client)
doaps = _get_all_doaps_of_project(project_iri, dsp_client)
filtered_doaps = _filter_doaps_by_target(
doaps=doaps,
Expand Down
6 changes: 3 additions & 3 deletions dsp_permissions_scripts/oap/oap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from dsp_permissions_scripts.utils.get_logger import get_logger
from dsp_permissions_scripts.utils.project import (
get_all_resource_class_iris_of_project,
get_project_iri_by_shortcode,
get_project_iri_and_onto_iris_by_shortcode,
)
from dsp_permissions_scripts.utils.scope_serialization import create_scope_from_string

Expand Down Expand Up @@ -99,9 +99,9 @@ def get_all_resource_oaps_of_project(
excluded_class_iris: Iterable[str] = (),
) -> list[Oap]:
logger.info("******* Retrieving all resource OAPs... *******")
project_iri = get_project_iri_by_shortcode(shortcode, dsp_client)
project_iri, onto_iris = get_project_iri_and_onto_iris_by_shortcode(shortcode, dsp_client)
all_resource_oaps = []
resclass_iris = get_all_resource_class_iris_of_project(project_iri, dsp_client)
resclass_iris = get_all_resource_class_iris_of_project(onto_iris, dsp_client)
resclass_iris = [x for x in resclass_iris if x not in excluded_class_iris]
for resclass_iri in resclass_iris:
resource_oaps = _get_all_resource_oaps_of_resclass(resclass_iri, project_iri, dsp_client)
Expand Down
1 change: 0 additions & 1 deletion dsp_permissions_scripts/utils/dsp_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import re
import time
from dataclasses import dataclass, field
from datetime import datetime
from functools import partial
from importlib.metadata import version
from typing import Any, Literal, Optional, cast
Expand Down
23 changes: 6 additions & 17 deletions dsp_permissions_scripts/utils/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@
logger = get_logger(__name__)


def _get_onto_iris_of_project(project_iri: str, dsp_client: DspClient) -> list[str]:
try:
response = dsp_client.get("/v2/ontologies/metadata")
except ApiError as err:
err.message = f"Could not get onto IRIs of project {project_iri}"
raise err from None
all_ontologies = response["@graph"]
project_onto_iris = [o["@id"] for o in all_ontologies if o["knora-api:attachedToProject"]["@id"] == project_iri]
return project_onto_iris


def _get_class_iris_of_onto(onto_iri: str, dsp_client: DspClient) -> list[str]:
try:
response = dsp_client.get(f"/v2/ontologies/allentities/{quote_plus(onto_iri)}")
Expand All @@ -32,21 +21,21 @@ def _get_class_iris_of_onto(onto_iri: str, dsp_client: DspClient) -> list[str]:
return class_iris


def get_all_resource_class_iris_of_project(project_iri: str, dsp_client: DspClient) -> list[str]:
project_onto_iris = _get_onto_iris_of_project(project_iri, dsp_client)
def get_all_resource_class_iris_of_project(onto_iris: list[str], dsp_client: DspClient) -> list[str]:
all_class_iris = []
for onto_iri in project_onto_iris:
for onto_iri in onto_iris:
class_iris = _get_class_iris_of_onto(onto_iri, dsp_client)
all_class_iris.extend(class_iris)
logger.info(f"Found {len(class_iris)} resource classes in onto {onto_iri}.")
return all_class_iris


def get_project_iri_by_shortcode(shortcode: str, dsp_client: DspClient) -> str:
def get_project_iri_and_onto_iris_by_shortcode(shortcode: str, dsp_client: DspClient) -> tuple[str, list[str]]:
jnussbaum marked this conversation as resolved.
Show resolved Hide resolved
try:
response = dsp_client.get(f"/admin/projects/shortcode/{shortcode}")
except ApiError as err:
err.message = f"Could not get project IRI by shortcode {shortcode}"
raise err from None
iri: str = response["project"]["id"]
return iri
project_iri: str = response["project"]["id"]
onto_iris: list[str] = response["project"]["ontologies"]
return project_iri, onto_iris
Loading