From 2fc07d620e9ac06677e04001426e07308bf2279d Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Tue, 22 Oct 2024 21:11:05 +0200 Subject: [PATCH] separate group.py and group_utils.py --- dsp_permissions_scripts/ap/ap_get.py | 2 +- dsp_permissions_scripts/ap/ap_set.py | 2 +- dsp_permissions_scripts/doap/doap_get.py | 2 +- dsp_permissions_scripts/doap/doap_set.py | 2 +- dsp_permissions_scripts/models/group.py | 48 +-------------- dsp_permissions_scripts/models/group_utils.py | 61 +++++++++++++++++-- dsp_permissions_scripts/models/scope.py | 2 +- dsp_permissions_scripts/oap/oap_set.py | 2 +- dsp_permissions_scripts/oap/update_iris.py | 2 +- dsp_permissions_scripts/utils/helpers.py | 1 + .../utils/scope_serialization.py | 2 +- tests/test_group.py | 14 ++--- tests/test_oap_update_iris.py | 2 +- tests/test_scope_serialization.py | 2 +- 14 files changed, 75 insertions(+), 69 deletions(-) diff --git a/dsp_permissions_scripts/ap/ap_get.py b/dsp_permissions_scripts/ap/ap_get.py index 53b1f263..b8551f87 100644 --- a/dsp_permissions_scripts/ap/ap_get.py +++ b/dsp_permissions_scripts/ap/ap_get.py @@ -4,8 +4,8 @@ from dsp_permissions_scripts.ap.ap_model import Ap from dsp_permissions_scripts.ap.ap_model import ApValue from dsp_permissions_scripts.models.errors import ApiError -from dsp_permissions_scripts.models.group import get_prefixed_iri_from_full_iri from dsp_permissions_scripts.models.group import group_builder +from dsp_permissions_scripts.models.group_utils import get_prefixed_iri_from_full_iri 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_and_onto_iris_by_shortcode diff --git a/dsp_permissions_scripts/ap/ap_set.py b/dsp_permissions_scripts/ap/ap_set.py index c25cd592..a164237f 100644 --- a/dsp_permissions_scripts/ap/ap_set.py +++ b/dsp_permissions_scripts/ap/ap_set.py @@ -7,7 +7,7 @@ from dsp_permissions_scripts.ap.ap_model import ApValue from dsp_permissions_scripts.models.errors import ApiError from dsp_permissions_scripts.models.group import GroupType -from dsp_permissions_scripts.models.group import get_full_iri_from_prefixed_iri +from dsp_permissions_scripts.models.group_utils import get_full_iri_from_prefixed_iri 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_and_onto_iris_by_shortcode diff --git a/dsp_permissions_scripts/doap/doap_get.py b/dsp_permissions_scripts/doap/doap_get.py index 6362fc7f..a9c957f4 100644 --- a/dsp_permissions_scripts/doap/doap_get.py +++ b/dsp_permissions_scripts/doap/doap_get.py @@ -5,8 +5,8 @@ from dsp_permissions_scripts.doap.doap_model import EntityDoapTarget from dsp_permissions_scripts.doap.doap_model import GroupDoapTarget from dsp_permissions_scripts.models.errors import ApiError -from dsp_permissions_scripts.models.group import get_prefixed_iri_from_full_iri from dsp_permissions_scripts.models.group import group_builder +from dsp_permissions_scripts.models.group_utils import get_prefixed_iri_from_full_iri 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_and_onto_iris_by_shortcode diff --git a/dsp_permissions_scripts/doap/doap_set.py b/dsp_permissions_scripts/doap/doap_set.py index 191ac13c..50880697 100644 --- a/dsp_permissions_scripts/doap/doap_set.py +++ b/dsp_permissions_scripts/doap/doap_set.py @@ -5,7 +5,7 @@ from dsp_permissions_scripts.doap.doap_model import NewEntityDoapTarget from dsp_permissions_scripts.doap.doap_model import NewGroupDoapTarget from dsp_permissions_scripts.models.errors import ApiError -from dsp_permissions_scripts.models.group import get_full_iri_from_prefixed_iri +from dsp_permissions_scripts.models.group_utils import get_full_iri_from_prefixed_iri from dsp_permissions_scripts.models.scope import PermissionScope from dsp_permissions_scripts.utils.dsp_client import DspClient from dsp_permissions_scripts.utils.get_logger import get_logger diff --git a/dsp_permissions_scripts/models/group.py b/dsp_permissions_scripts/models/group.py index 474f7a1b..783b1ac5 100644 --- a/dsp_permissions_scripts/models/group.py +++ b/dsp_permissions_scripts/models/group.py @@ -15,11 +15,10 @@ from dsp_permissions_scripts.models.errors import InvalidGroupError from dsp_permissions_scripts.models.errors import InvalidIRIError -from dsp_permissions_scripts.utils.dsp_client import DspClient +from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE -NAMES_OF_BUILTIN_GROUPS = ["SystemAdmin", "Creator", "ProjectAdmin", "ProjectMember", "KnownUser", "UnknownUser"] -KNORA_ADMIN_ONTO_NAMESPACE = "http://www.knora.org/ontology/knora-admin#" PREFIXED_IRI_REGEX = r"^[\w-]+:[\w -]+$" +NAMES_OF_BUILTIN_GROUPS = ["SystemAdmin", "Creator", "ProjectAdmin", "ProjectMember", "KnownUser", "UnknownUser"] class BuiltinGroup(BaseModel): @@ -86,49 +85,6 @@ def is_prefixed_group_iri(iri: str) -> bool: raise InvalidIRIError(f"{iri} is not a valid group IRI") -def get_prefixed_iri_from_full_iri(full_iri: str, dsp_client: DspClient) -> str: - if full_iri.startswith(KNORA_ADMIN_ONTO_NAMESPACE) and full_iri.endswith(tuple(NAMES_OF_BUILTIN_GROUPS)): - return full_iri.replace(KNORA_ADMIN_ONTO_NAMESPACE, "knora-admin:") - elif full_iri.startswith("http://rdfh.ch/groups/"): - all_groups = dsp_client.get("/admin/groups")["groups"] - if not (group := [grp for grp in all_groups if grp["id"] == full_iri]): - raise InvalidGroupError( - f"{full_iri} is not a valid full IRI of a group. " - f"Available group IRIs: {', '.join([grp['id'] for grp in all_groups])}" - ) - return f"{group[0]["project"]["shortname"]}:{group[0]["name"]}" - else: - raise InvalidIRIError(f"Could not transform full IRI {full_iri} to prefixed IRI") - - -def get_full_iri_from_prefixed_iri(prefixed_iri: str, dsp_client: DspClient) -> str: - if not is_prefixed_group_iri(prefixed_iri): - raise InvalidIRIError(f"{prefixed_iri} is not a valid prefixed group IRI") - prefix, groupname = prefixed_iri.split(":") - if prefix == "knora-admin": - return _get_full_iri_from_builtin_group(prefix, groupname) - else: - return _get_full_iri_from_custom_group(prefix, groupname, dsp_client) - - -def _get_full_iri_from_builtin_group(prefix: str, groupname: str) -> str: - if groupname not in NAMES_OF_BUILTIN_GROUPS: - raise InvalidGroupError(f"{prefix}:{groupname} is not a valid builtin group") - return f"{KNORA_ADMIN_ONTO_NAMESPACE}{groupname}" - - -def _get_full_iri_from_custom_group(prefix: str, groupname: str, dsp_client: DspClient) -> str: - all_groups = dsp_client.get("/admin/groups")["groups"] - proj_groups = [grp for grp in all_groups if grp["project"]["shortname"] == prefix] - if not (group := [grp for grp in proj_groups if grp["name"] == groupname]): - raise InvalidGroupError( - f"{prefix}:{groupname} is not a valid group. " - f"Available groups for the project {prefix}: {', '.join([grp['name'] for grp in proj_groups])}" - ) - full_iri: str = group[0]["id"] - return full_iri - - UNKNOWN_USER = BuiltinGroup(prefixed_iri="knora-admin:UnknownUser") KNOWN_USER = BuiltinGroup(prefixed_iri="knora-admin:KnownUser") PROJECT_MEMBER = BuiltinGroup(prefixed_iri="knora-admin:ProjectMember") diff --git a/dsp_permissions_scripts/models/group_utils.py b/dsp_permissions_scripts/models/group_utils.py index d0932e10..37a67c93 100644 --- a/dsp_permissions_scripts/models/group_utils.py +++ b/dsp_permissions_scripts/models/group_utils.py @@ -1,19 +1,19 @@ from typing import Iterable +from dsp_permissions_scripts.models.errors import InvalidGroupError +from dsp_permissions_scripts.models.errors import InvalidIRIError from dsp_permissions_scripts.models.group import CREATOR from dsp_permissions_scripts.models.group import KNOWN_USER +from dsp_permissions_scripts.models.group import NAMES_OF_BUILTIN_GROUPS from dsp_permissions_scripts.models.group import PROJECT_ADMIN from dsp_permissions_scripts.models.group import PROJECT_MEMBER from dsp_permissions_scripts.models.group import SYSTEM_ADMIN from dsp_permissions_scripts.models.group import UNKNOWN_USER from dsp_permissions_scripts.models.group import BuiltinGroup from dsp_permissions_scripts.models.group import GroupType - - -def _get_sort_pos_of_custom_group(prefixed_iri: str) -> int: - alphabet = list("abcdefghijklmnopqrstuvwxyz") - relevant_letter = prefixed_iri.split(":")[-1][0] - return alphabet.index(relevant_letter.lower()) + 99 # must be higher than the highest index of the builtin groups +from dsp_permissions_scripts.models.group import is_prefixed_group_iri +from dsp_permissions_scripts.utils.dsp_client import DspClient +from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE def sort_groups(groups_original: Iterable[GroupType]) -> list[GroupType]: @@ -30,3 +30,52 @@ def sort_groups(groups_original: Iterable[GroupType]) -> list[GroupType]: else _get_sort_pos_of_custom_group(x.prefixed_iri) ) return groups + + +def _get_sort_pos_of_custom_group(prefixed_iri: str) -> int: + alphabet = list("abcdefghijklmnopqrstuvwxyz") + relevant_letter = prefixed_iri.split(":")[-1][0] + return alphabet.index(relevant_letter.lower()) + 99 # must be higher than the highest index of the builtin groups + + +def get_prefixed_iri_from_full_iri(full_iri: str, dsp_client: DspClient) -> str: + if full_iri.startswith(KNORA_ADMIN_ONTO_NAMESPACE) and full_iri.endswith(tuple(NAMES_OF_BUILTIN_GROUPS)): + return full_iri.replace(KNORA_ADMIN_ONTO_NAMESPACE, "knora-admin:") + elif full_iri.startswith("http://rdfh.ch/groups/"): + all_groups = dsp_client.get("/admin/groups")["groups"] + if not (group := [grp for grp in all_groups if grp["id"] == full_iri]): + raise InvalidGroupError( + f"{full_iri} is not a valid full IRI of a group. " + f"Available group IRIs: {', '.join([grp['id'] for grp in all_groups])}" + ) + return f"{group[0]["project"]["shortname"]}:{group[0]["name"]}" + else: + raise InvalidIRIError(f"Could not transform full IRI {full_iri} to prefixed IRI") + + +def get_full_iri_from_prefixed_iri(prefixed_iri: str, dsp_client: DspClient) -> str: + if not is_prefixed_group_iri(prefixed_iri): + raise InvalidIRIError(f"{prefixed_iri} is not a valid prefixed group IRI") + prefix, groupname = prefixed_iri.split(":") + if prefix == "knora-admin": + return _get_full_iri_from_builtin_group(prefix, groupname) + else: + return _get_full_iri_from_custom_group(prefix, groupname, dsp_client) + + +def _get_full_iri_from_builtin_group(prefix: str, groupname: str) -> str: + if groupname not in NAMES_OF_BUILTIN_GROUPS: + raise InvalidGroupError(f"{prefix}:{groupname} is not a valid builtin group") + return f"{KNORA_ADMIN_ONTO_NAMESPACE}{groupname}" + + +def _get_full_iri_from_custom_group(prefix: str, groupname: str, dsp_client: DspClient) -> str: + all_groups = dsp_client.get("/admin/groups")["groups"] + proj_groups = [grp for grp in all_groups if grp["project"]["shortname"] == prefix] + if not (group := [grp for grp in proj_groups if grp["name"] == groupname]): + raise InvalidGroupError( + f"{prefix}:{groupname} is not a valid group. " + f"Available groups for the project {prefix}: {', '.join([grp['name'] for grp in proj_groups])}" + ) + full_iri: str = group[0]["id"] + return full_iri diff --git a/dsp_permissions_scripts/models/scope.py b/dsp_permissions_scripts/models/scope.py index 86e55aa8..1b07a1eb 100644 --- a/dsp_permissions_scripts/models/scope.py +++ b/dsp_permissions_scripts/models/scope.py @@ -15,9 +15,9 @@ from dsp_permissions_scripts.models.group import UNKNOWN_USER from dsp_permissions_scripts.models.group import CustomGroup from dsp_permissions_scripts.models.group import GroupType -from dsp_permissions_scripts.models.group import get_prefixed_iri_from_full_iri from dsp_permissions_scripts.models.group import group_builder from dsp_permissions_scripts.models.group import is_prefixed_group_iri +from dsp_permissions_scripts.models.group_utils import get_prefixed_iri_from_full_iri from dsp_permissions_scripts.utils.dsp_client import DspClient diff --git a/dsp_permissions_scripts/oap/oap_set.py b/dsp_permissions_scripts/oap/oap_set.py index 77cca9a3..7a0b89be 100644 --- a/dsp_permissions_scripts/oap/oap_set.py +++ b/dsp_permissions_scripts/oap/oap_set.py @@ -6,12 +6,12 @@ from dsp_permissions_scripts.models.errors import ApiError from dsp_permissions_scripts.models.errors import PermissionsAlreadyUpToDate -from dsp_permissions_scripts.models.group import KNORA_ADMIN_ONTO_NAMESPACE from dsp_permissions_scripts.models.scope import PermissionScope from dsp_permissions_scripts.oap.oap_model import ModifiedOap from dsp_permissions_scripts.oap.oap_model import ValueOap from dsp_permissions_scripts.utils.dsp_client import DspClient from dsp_permissions_scripts.utils.get_logger import get_logger +from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE from dsp_permissions_scripts.utils.scope_serialization import create_string_from_scope logger = get_logger(__name__) diff --git a/dsp_permissions_scripts/oap/update_iris.py b/dsp_permissions_scripts/oap/update_iris.py index 82ca1dfe..d2c57262 100644 --- a/dsp_permissions_scripts/oap/update_iris.py +++ b/dsp_permissions_scripts/oap/update_iris.py @@ -11,13 +11,13 @@ from dsp_permissions_scripts.models.errors import ApiError from dsp_permissions_scripts.models.errors import InvalidIRIError -from dsp_permissions_scripts.models.group import KNORA_ADMIN_ONTO_NAMESPACE from dsp_permissions_scripts.models.scope import PermissionScope from dsp_permissions_scripts.oap.oap_get import get_value_oaps from dsp_permissions_scripts.oap.oap_set import update_permissions_for_resource from dsp_permissions_scripts.oap.oap_set import update_permissions_for_value from dsp_permissions_scripts.utils.dsp_client import DspClient from dsp_permissions_scripts.utils.get_logger import get_logger +from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE logger = get_logger(__name__) diff --git a/dsp_permissions_scripts/utils/helpers.py b/dsp_permissions_scripts/utils/helpers.py index ebe0afc2..7e2fbe0e 100644 --- a/dsp_permissions_scripts/utils/helpers.py +++ b/dsp_permissions_scripts/utils/helpers.py @@ -1,4 +1,5 @@ PACKAGE_NAME = "dsp-permissions-scripts" +KNORA_ADMIN_ONTO_NAMESPACE = "http://www.knora.org/ontology/knora-admin#" def dereference_prefix( diff --git a/dsp_permissions_scripts/utils/scope_serialization.py b/dsp_permissions_scripts/utils/scope_serialization.py index ce97c8ab..a82ce75b 100644 --- a/dsp_permissions_scripts/utils/scope_serialization.py +++ b/dsp_permissions_scripts/utils/scope_serialization.py @@ -1,6 +1,6 @@ from typing import Any -from dsp_permissions_scripts.models.group import get_full_iri_from_prefixed_iri +from dsp_permissions_scripts.models.group_utils import get_full_iri_from_prefixed_iri from dsp_permissions_scripts.models.group_utils import sort_groups from dsp_permissions_scripts.models.scope import PermissionScope from dsp_permissions_scripts.utils.dsp_client import DspClient diff --git a/tests/test_group.py b/tests/test_group.py index 6e56ec16..a274589d 100644 --- a/tests/test_group.py +++ b/tests/test_group.py @@ -6,7 +6,6 @@ from dsp_permissions_scripts.models.errors import InvalidGroupError from dsp_permissions_scripts.models.errors import InvalidIRIError from dsp_permissions_scripts.models.group import CREATOR -from dsp_permissions_scripts.models.group import KNORA_ADMIN_ONTO_NAMESPACE from dsp_permissions_scripts.models.group import KNOWN_USER from dsp_permissions_scripts.models.group import NAMES_OF_BUILTIN_GROUPS from dsp_permissions_scripts.models.group import PROJECT_ADMIN @@ -15,14 +14,15 @@ from dsp_permissions_scripts.models.group import UNKNOWN_USER from dsp_permissions_scripts.models.group import BuiltinGroup from dsp_permissions_scripts.models.group import CustomGroup -from dsp_permissions_scripts.models.group import _get_full_iri_from_builtin_group -from dsp_permissions_scripts.models.group import _get_full_iri_from_custom_group -from dsp_permissions_scripts.models.group import get_full_iri_from_prefixed_iri -from dsp_permissions_scripts.models.group import get_prefixed_iri_from_full_iri from dsp_permissions_scripts.models.group import group_builder from dsp_permissions_scripts.models.group import is_prefixed_group_iri +from dsp_permissions_scripts.models.group_utils import _get_full_iri_from_builtin_group +from dsp_permissions_scripts.models.group_utils import _get_full_iri_from_custom_group +from dsp_permissions_scripts.models.group_utils import get_full_iri_from_prefixed_iri +from dsp_permissions_scripts.models.group_utils import get_prefixed_iri_from_full_iri from dsp_permissions_scripts.models.group_utils import sort_groups from dsp_permissions_scripts.utils.dsp_client import DspClient +from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE @pytest.fixture @@ -89,13 +89,13 @@ def test_get_full_iri_from_prefixed_iri_invalid(iri: str) -> None: get_full_iri_from_prefixed_iri(iri, DspClient("foo")) -@patch("dsp_permissions_scripts.models.group._get_full_iri_from_builtin_group") +@patch("dsp_permissions_scripts.models.group_utils._get_full_iri_from_builtin_group") def test_get_full_iri_from_prefixed_iri_with_builtin_group(patched_func: Mock) -> None: get_full_iri_from_prefixed_iri("knora-admin:ProjectAdmin", DspClient("foo")) patched_func.assert_called_once_with("knora-admin", "ProjectAdmin") -@patch("dsp_permissions_scripts.models.group._get_full_iri_from_custom_group") +@patch("dsp_permissions_scripts.models.group_utils._get_full_iri_from_custom_group") def test_get_full_iri_from_prefixed_iri_with_custom_group(patched_func: Mock) -> None: dsp_client = DspClient("foo") get_full_iri_from_prefixed_iri("limc:groupname", dsp_client) diff --git a/tests/test_oap_update_iris.py b/tests/test_oap_update_iris.py index 64d58fa6..fd221728 100644 --- a/tests/test_oap_update_iris.py +++ b/tests/test_oap_update_iris.py @@ -4,7 +4,6 @@ import pytest from dsp_permissions_scripts.models.errors import InvalidIRIError -from dsp_permissions_scripts.models.group import KNORA_ADMIN_ONTO_NAMESPACE from dsp_permissions_scripts.models.group import PROJECT_ADMIN from dsp_permissions_scripts.models.scope import PermissionScope from dsp_permissions_scripts.oap import update_iris @@ -13,6 +12,7 @@ from dsp_permissions_scripts.oap.update_iris import ResourceIRIUpdater from dsp_permissions_scripts.oap.update_iris import ValueIRIUpdater from dsp_permissions_scripts.utils.dsp_client import DspClient +from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE @pytest.fixture diff --git a/tests/test_scope_serialization.py b/tests/test_scope_serialization.py index 4f1079b9..fa048354 100644 --- a/tests/test_scope_serialization.py +++ b/tests/test_scope_serialization.py @@ -4,7 +4,6 @@ from pytest_unordered import unordered from dsp_permissions_scripts.models.group import CREATOR -from dsp_permissions_scripts.models.group import KNORA_ADMIN_ONTO_NAMESPACE from dsp_permissions_scripts.models.group import KNOWN_USER from dsp_permissions_scripts.models.group import PROJECT_ADMIN from dsp_permissions_scripts.models.group import PROJECT_MEMBER @@ -13,6 +12,7 @@ from dsp_permissions_scripts.models.group import CustomGroup from dsp_permissions_scripts.models.scope import PermissionScope from dsp_permissions_scripts.utils.dsp_client import DspClient +from dsp_permissions_scripts.utils.helpers import KNORA_ADMIN_ONTO_NAMESPACE from dsp_permissions_scripts.utils.scope_serialization import create_admin_route_object_from_scope from dsp_permissions_scripts.utils.scope_serialization import create_scope_from_admin_route_object from dsp_permissions_scripts.utils.scope_serialization import create_scope_from_string