diff --git a/dsp_permissions_scripts/ap/ap_get.py b/dsp_permissions_scripts/ap/ap_get.py index 80e9e19f..6c5ac18c 100644 --- a/dsp_permissions_scripts/ap/ap_get.py +++ b/dsp_permissions_scripts/ap/ap_get.py @@ -1,11 +1,14 @@ from typing import Any from urllib.parse import quote_plus -from dsp_permissions_scripts.ap.ap_model import Ap, ApValue +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.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, +) logger = get_logger(__name__) @@ -50,7 +53,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 diff --git a/dsp_permissions_scripts/ap/ap_serialize.py b/dsp_permissions_scripts/ap/ap_serialize.py index 8dfcb104..df863f28 100644 --- a/dsp_permissions_scripts/ap/ap_serialize.py +++ b/dsp_permissions_scripts/ap/ap_serialize.py @@ -3,7 +3,8 @@ from typing import Literal from dsp_permissions_scripts.ap.ap_model import Ap -from dsp_permissions_scripts.utils.get_logger import get_logger, get_timestamp +from dsp_permissions_scripts.utils.get_logger import get_logger +from dsp_permissions_scripts.utils.get_logger import get_timestamp logger = get_logger(__name__) diff --git a/dsp_permissions_scripts/ap/ap_set.py b/dsp_permissions_scripts/ap/ap_set.py index 2ca6ae89..ed24c9e3 100644 --- a/dsp_permissions_scripts/ap/ap_set.py +++ b/dsp_permissions_scripts/ap/ap_set.py @@ -1,10 +1,8 @@ from typing import Any from urllib.parse import quote_plus -from dsp_permissions_scripts.ap.ap_get import ( - create_admin_route_object_from_ap, - create_ap_from_admin_route_object, -) +from dsp_permissions_scripts.ap.ap_get import create_admin_route_object_from_ap +from dsp_permissions_scripts.ap.ap_get import create_ap_from_admin_route_object from dsp_permissions_scripts.ap.ap_model import Ap from dsp_permissions_scripts.models.errors import ApiError from dsp_permissions_scripts.utils.dsp_client import DspClient diff --git a/dsp_permissions_scripts/doap/doap_get.py b/dsp_permissions_scripts/doap/doap_get.py index 54f946d6..ae4beec4 100644 --- a/dsp_permissions_scripts/doap/doap_get.py +++ b/dsp_permissions_scripts/doap/doap_get.py @@ -1,11 +1,15 @@ from typing import Any from urllib.parse import quote_plus -from dsp_permissions_scripts.doap.doap_model import Doap, DoapTarget, DoapTargetType +from dsp_permissions_scripts.doap.doap_model import Doap +from dsp_permissions_scripts.doap.doap_model import DoapTarget +from dsp_permissions_scripts.doap.doap_model import DoapTargetType 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, ) @@ -72,7 +76,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, diff --git a/dsp_permissions_scripts/doap/doap_model.py b/dsp_permissions_scripts/doap/doap_model.py index cb3ddc37..e69576c6 100644 --- a/dsp_permissions_scripts/doap/doap_model.py +++ b/dsp_permissions_scripts/doap/doap_model.py @@ -3,7 +3,8 @@ from enum import Enum from typing import Self -from pydantic import BaseModel, model_validator +from pydantic import BaseModel +from pydantic import model_validator from dsp_permissions_scripts.models.group import Group from dsp_permissions_scripts.models.scope import PermissionScope diff --git a/dsp_permissions_scripts/doap/doap_serialize.py b/dsp_permissions_scripts/doap/doap_serialize.py index 22179f23..ff09cdbd 100644 --- a/dsp_permissions_scripts/doap/doap_serialize.py +++ b/dsp_permissions_scripts/doap/doap_serialize.py @@ -2,8 +2,10 @@ from pathlib import Path from typing import Literal -from dsp_permissions_scripts.doap.doap_model import Doap, DoapTargetType -from dsp_permissions_scripts.utils.get_logger import get_logger, get_timestamp +from dsp_permissions_scripts.doap.doap_model import Doap +from dsp_permissions_scripts.doap.doap_model import DoapTargetType +from dsp_permissions_scripts.utils.get_logger import get_logger +from dsp_permissions_scripts.utils.get_logger import get_timestamp logger = get_logger(__name__) diff --git a/dsp_permissions_scripts/models/group.py b/dsp_permissions_scripts/models/group.py index 7b28661b..f08103d0 100644 --- a/dsp_permissions_scripts/models/group.py +++ b/dsp_permissions_scripts/models/group.py @@ -2,7 +2,9 @@ import re -from pydantic import BaseModel, ConfigDict, model_validator +from pydantic import BaseModel +from pydantic import ConfigDict +from pydantic import model_validator class Group(BaseModel): diff --git a/dsp_permissions_scripts/models/scope.py b/dsp_permissions_scripts/models/scope.py index 259c248d..5450ec9b 100644 --- a/dsp_permissions_scripts/models/scope.py +++ b/dsp_permissions_scripts/models/scope.py @@ -1,17 +1,18 @@ from __future__ import annotations -from typing import Iterable, Literal +from typing import Iterable +from typing import Literal -from pydantic import BaseModel, ConfigDict, model_validator +from pydantic import BaseModel +from pydantic import ConfigDict +from pydantic import model_validator -from dsp_permissions_scripts.models.group import ( - CREATOR, - KNOWN_USER, - PROJECT_ADMIN, - PROJECT_MEMBER, - UNKNOWN_USER, - Group, -) +from dsp_permissions_scripts.models.group import CREATOR +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 +from dsp_permissions_scripts.models.group import UNKNOWN_USER +from dsp_permissions_scripts.models.group import Group class PermissionScope(BaseModel): diff --git a/dsp_permissions_scripts/oap/oap_get.py b/dsp_permissions_scripts/oap/oap_get.py index bb5820f6..bb3ef616 100644 --- a/dsp_permissions_scripts/oap/oap_get.py +++ b/dsp_permissions_scripts/oap/oap_get.py @@ -1,13 +1,14 @@ -from typing import Any, Iterable +from typing import Any +from typing import Iterable from urllib.parse import quote_plus from dsp_permissions_scripts.models.errors import ApiError from dsp_permissions_scripts.oap.oap_model import Oap 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_all_resource_class_iris_of_project 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 @@ -99,9 +100,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) diff --git a/dsp_permissions_scripts/oap/oap_set.py b/dsp_permissions_scripts/oap/oap_set.py index 46475981..11b5be2c 100644 --- a/dsp_permissions_scripts/oap/oap_set.py +++ b/dsp_permissions_scripts/oap/oap_set.py @@ -1,10 +1,12 @@ # pylint: disable=too-many-arguments -from concurrent.futures import ThreadPoolExecutor, as_completed +from concurrent.futures import ThreadPoolExecutor +from concurrent.futures import as_completed from datetime import datetime from typing import Any -from dsp_permissions_scripts.models.errors import ApiError, PermissionsAlreadyUpToDate +from dsp_permissions_scripts.models.errors import ApiError +from dsp_permissions_scripts.models.errors import PermissionsAlreadyUpToDate from dsp_permissions_scripts.models.scope import PermissionScope from dsp_permissions_scripts.models.value import ValueUpdate from dsp_permissions_scripts.oap.oap_get import get_resource diff --git a/dsp_permissions_scripts/template.py b/dsp_permissions_scripts/template.py index bd713fbd..3792af8e 100644 --- a/dsp_permissions_scripts/template.py +++ b/dsp_permissions_scripts/template.py @@ -1,6 +1,7 @@ from dsp_permissions_scripts.ap.ap_delete import delete_ap_of_group_on_server from dsp_permissions_scripts.ap.ap_get import get_aps_of_project -from dsp_permissions_scripts.ap.ap_model import Ap, ApValue +from dsp_permissions_scripts.ap.ap_model import Ap +from dsp_permissions_scripts.ap.ap_model import ApValue from dsp_permissions_scripts.ap.ap_serialize import serialize_aps_of_project from dsp_permissions_scripts.ap.ap_set import apply_updated_aps_on_server from dsp_permissions_scripts.doap.doap_get import get_doaps_of_project diff --git a/dsp_permissions_scripts/utils/dsp_client.py b/dsp_permissions_scripts/utils/dsp_client.py index 9bb56435..70614ac3 100644 --- a/dsp_permissions_scripts/utils/dsp_client.py +++ b/dsp_permissions_scripts/utils/dsp_client.py @@ -1,14 +1,22 @@ import json import re import time -from dataclasses import dataclass, field +from dataclasses import dataclass +from dataclasses import field from functools import partial from importlib.metadata import version -from typing import Any, Literal, Optional, cast - -from requests import ReadTimeout, RequestException, Response, Session - -from dsp_permissions_scripts.models.errors import ApiError, PermissionsAlreadyUpToDate +from typing import Any +from typing import Literal +from typing import Optional +from typing import cast + +from requests import ReadTimeout +from requests import RequestException +from requests import Response +from requests import Session + +from dsp_permissions_scripts.models.errors import ApiError +from dsp_permissions_scripts.models.errors import PermissionsAlreadyUpToDate from dsp_permissions_scripts.utils.get_logger import get_logger from dsp_permissions_scripts.utils.helpers import PACKAGE_NAME diff --git a/dsp_permissions_scripts/utils/project.py b/dsp_permissions_scripts/utils/project.py index dca3eccb..8c53e68c 100644 --- a/dsp_permissions_scripts/utils/project.py +++ b/dsp_permissions_scripts/utils/project.py @@ -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)}") @@ -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]]: 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 diff --git a/dsp_permissions_scripts/utils/try_request.py b/dsp_permissions_scripts/utils/try_request.py index e60b2109..b1eb78e5 100644 --- a/dsp_permissions_scripts/utils/try_request.py +++ b/dsp_permissions_scripts/utils/try_request.py @@ -2,7 +2,8 @@ from typing import Callable import requests -from requests import ReadTimeout, RequestException +from requests import ReadTimeout +from requests import RequestException from urllib3.exceptions import ReadTimeoutError from dsp_permissions_scripts.utils.get_logger import get_logger diff --git a/tests/test_ap.py b/tests/test_ap.py index cbbd9cfd..6d69e120 100644 --- a/tests/test_ap.py +++ b/tests/test_ap.py @@ -1,6 +1,7 @@ import unittest -from dsp_permissions_scripts.ap.ap_model import Ap, ApValue +from dsp_permissions_scripts.ap.ap_model import Ap +from dsp_permissions_scripts.ap.ap_model import ApValue from dsp_permissions_scripts.models import group diff --git a/tests/test_ap_serialization.py b/tests/test_ap_serialization.py index ca5cdfe4..8942c75b 100644 --- a/tests/test_ap_serialization.py +++ b/tests/test_ap_serialization.py @@ -3,11 +3,10 @@ import unittest from pathlib import Path -from dsp_permissions_scripts.ap.ap_model import Ap, ApValue -from dsp_permissions_scripts.ap.ap_serialize import ( - deserialize_aps_of_project, - serialize_aps_of_project, -) +from dsp_permissions_scripts.ap.ap_model import Ap +from dsp_permissions_scripts.ap.ap_model import ApValue +from dsp_permissions_scripts.ap.ap_serialize import deserialize_aps_of_project +from dsp_permissions_scripts.ap.ap_serialize import serialize_aps_of_project from dsp_permissions_scripts.models import group from dsp_permissions_scripts.models.host import Hosts diff --git a/tests/test_doap_serialization.py b/tests/test_doap_serialization.py index 52f106d8..955ef9bb 100644 --- a/tests/test_doap_serialization.py +++ b/tests/test_doap_serialization.py @@ -2,11 +2,10 @@ import unittest from pathlib import Path -from dsp_permissions_scripts.doap.doap_model import Doap, DoapTarget -from dsp_permissions_scripts.doap.doap_serialize import ( - deserialize_doaps_of_project, - serialize_doaps_of_project, -) +from dsp_permissions_scripts.doap.doap_model import Doap +from dsp_permissions_scripts.doap.doap_model import DoapTarget +from dsp_permissions_scripts.doap.doap_serialize import deserialize_doaps_of_project +from dsp_permissions_scripts.doap.doap_serialize import serialize_doaps_of_project from dsp_permissions_scripts.models import group from dsp_permissions_scripts.models.host import Hosts from dsp_permissions_scripts.models.scope import PermissionScope diff --git a/tests/test_oap_serialization.py b/tests/test_oap_serialization.py index 69269c37..d2ee1828 100644 --- a/tests/test_oap_serialization.py +++ b/tests/test_oap_serialization.py @@ -5,10 +5,8 @@ from dsp_permissions_scripts.models import group from dsp_permissions_scripts.models.scope import PermissionScope from dsp_permissions_scripts.oap.oap_model import Oap -from dsp_permissions_scripts.oap.oap_serialize import ( - deserialize_resource_oaps, - serialize_resource_oaps, -) +from dsp_permissions_scripts.oap.oap_serialize import deserialize_resource_oaps +from dsp_permissions_scripts.oap.oap_serialize import serialize_resource_oaps from tests.test_scope_serialization import compare_scopes diff --git a/tests/test_scope_serialization.py b/tests/test_scope_serialization.py index 6a688b33..9603c699 100644 --- a/tests/test_scope_serialization.py +++ b/tests/test_scope_serialization.py @@ -5,10 +5,12 @@ from dsp_permissions_scripts.models.scope import PermissionScope 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, - create_scope_from_string, - create_string_from_scope, ) +from dsp_permissions_scripts.utils.scope_serialization import create_scope_from_string +from dsp_permissions_scripts.utils.scope_serialization import create_string_from_scope def compare_scopes(