Skip to content

Commit

Permalink
Merge branch 'main' into wip/detect-package-name
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum authored Apr 8, 2024
2 parents a9710e4 + 9eef375 commit 38ce2ac
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 74 deletions.
9 changes: 6 additions & 3 deletions dsp_permissions_scripts/ap/ap_get.py
Original file line number Diff line number Diff line change
@@ -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__)

Expand Down Expand Up @@ -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
3 changes: 2 additions & 1 deletion dsp_permissions_scripts/ap/ap_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
6 changes: 2 additions & 4 deletions dsp_permissions_scripts/ap/ap_set.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 7 additions & 3 deletions dsp_permissions_scripts/doap/doap_get.py
Original file line number Diff line number Diff line change
@@ -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,
)
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion dsp_permissions_scripts/doap/doap_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions dsp_permissions_scripts/doap/doap_serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
4 changes: 3 additions & 1 deletion dsp_permissions_scripts/models/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
21 changes: 11 additions & 10 deletions dsp_permissions_scripts/models/scope.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
11 changes: 6 additions & 5 deletions dsp_permissions_scripts/oap/oap_get.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions dsp_permissions_scripts/oap/oap_set.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion dsp_permissions_scripts/template.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 14 additions & 6 deletions dsp_permissions_scripts/utils/dsp_client.py
Original file line number Diff line number Diff line change
@@ -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

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]]:
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
3 changes: 2 additions & 1 deletion dsp_permissions_scripts/utils/try_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/test_ap.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down
9 changes: 4 additions & 5 deletions tests/test_ap_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 4 additions & 5 deletions tests/test_doap_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions tests/test_oap_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
6 changes: 4 additions & 2 deletions tests/test_scope_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 38ce2ac

Please sign in to comment.