Skip to content

Commit

Permalink
separate group.py and group_utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Oct 22, 2024
1 parent 6dc2ccc commit 2fc07d6
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 69 deletions.
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/ap/ap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/ap/ap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/doap/doap_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/doap/doap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 2 additions & 46 deletions dsp_permissions_scripts/models/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down
61 changes: 55 additions & 6 deletions dsp_permissions_scripts/models/group_utils.py
Original file line number Diff line number Diff line change
@@ -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]:
Expand All @@ -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
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/models/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/oap/oap_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/oap/update_iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
1 change: 1 addition & 0 deletions dsp_permissions_scripts/utils/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PACKAGE_NAME = "dsp-permissions-scripts"
KNORA_ADMIN_ONTO_NAMESPACE = "http://www.knora.org/ontology/knora-admin#"


def dereference_prefix(
Expand Down
2 changes: 1 addition & 1 deletion dsp_permissions_scripts/utils/scope_serialization.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 7 additions & 7 deletions tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_oap_update_iris.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/test_scope_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 2fc07d6

Please sign in to comment.