dsp-tools 17.0.0.post14__py3-none-any.whl → 17.0.0.post23__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of dsp-tools might be problematic. Click here for more details.
- dsp_tools/clients/metadata_client.py +24 -0
- dsp_tools/clients/metadata_client_live.py +42 -0
- dsp_tools/clients/ontology_client.py +21 -0
- dsp_tools/clients/ontology_client_live.py +139 -0
- dsp_tools/commands/create/communicate_problems.py +19 -0
- dsp_tools/commands/create/constants.py +6 -3
- dsp_tools/commands/create/create_on_server/cardinalities.py +121 -0
- dsp_tools/commands/create/create_on_server/mappers.py +12 -0
- dsp_tools/commands/create/models/input_problems.py +11 -2
- dsp_tools/commands/create/models/rdf_ontology.py +19 -0
- dsp_tools/commands/create/models/server_project_info.py +25 -0
- dsp_tools/commands/create/parsing/parse_ontology.py +7 -7
- dsp_tools/commands/create/parsing/parse_project.py +1 -1
- dsp_tools/commands/create/parsing/parsing_utils.py +2 -2
- dsp_tools/commands/create/serialisation/__init__.py +0 -0
- dsp_tools/commands/create/serialisation/ontology.py +41 -0
- dsp_tools/commands/project/create/project_create_all.py +35 -25
- dsp_tools/commands/project/create/project_create_ontologies.py +39 -89
- dsp_tools/commands/project/legacy_models/resourceclass.py +0 -33
- dsp_tools/commands/validate_data/models/api_responses.py +7 -0
- dsp_tools/commands/validate_data/models/validation.py +1 -0
- dsp_tools/commands/validate_data/prepare_data/make_data_graph.py +4 -1
- dsp_tools/commands/validate_data/prepare_data/prepare_data.py +32 -5
- dsp_tools/commands/validate_data/validate_data.py +1 -1
- dsp_tools/commands/validate_data/validation/get_validation_report.py +2 -1
- dsp_tools/error/exceptions.py +4 -0
- dsp_tools/utils/data_formats/iri_util.py +7 -0
- dsp_tools/utils/rdflib_utils.py +10 -0
- {dsp_tools-17.0.0.post14.dist-info → dsp_tools-17.0.0.post23.dist-info}/METADATA +1 -1
- {dsp_tools-17.0.0.post14.dist-info → dsp_tools-17.0.0.post23.dist-info}/RECORD +33 -21
- /dsp_tools/commands/create/{parsing/parse_lists.py → create_on_server/__init__.py} +0 -0
- {dsp_tools-17.0.0.post14.dist-info → dsp_tools-17.0.0.post23.dist-info}/WHEEL +0 -0
- {dsp_tools-17.0.0.post14.dist-info → dsp_tools-17.0.0.post23.dist-info}/entry_points.txt +0 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from enum import Enum
|
|
3
|
+
from enum import auto
|
|
4
|
+
from typing import Protocol
|
|
5
|
+
|
|
6
|
+
from dsp_tools.clients.authentication_client import AuthenticationClient
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class MetadataRetrieval(Enum):
|
|
10
|
+
SUCCESS = auto()
|
|
11
|
+
FAILURE = auto()
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass
|
|
15
|
+
class MetadataClient(Protocol):
|
|
16
|
+
"""
|
|
17
|
+
Protocol class/interface for the metadata endpoint in the API.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
server: str
|
|
21
|
+
authentication_client: AuthenticationClient
|
|
22
|
+
|
|
23
|
+
def get_resource_metadata(self, shortcode: str) -> tuple[MetadataRetrieval, list[dict[str, str]]]:
|
|
24
|
+
"""Get all resource metadata from one project."""
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
import requests
|
|
4
|
+
from loguru import logger
|
|
5
|
+
|
|
6
|
+
from dsp_tools.clients.authentication_client import AuthenticationClient
|
|
7
|
+
from dsp_tools.clients.metadata_client import MetadataClient
|
|
8
|
+
from dsp_tools.clients.metadata_client import MetadataRetrieval
|
|
9
|
+
from dsp_tools.utils.request_utils import RequestParameters
|
|
10
|
+
from dsp_tools.utils.request_utils import log_request
|
|
11
|
+
from dsp_tools.utils.request_utils import log_response
|
|
12
|
+
|
|
13
|
+
TIMEOUT = 120
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class MetadataClientLive(MetadataClient):
|
|
18
|
+
server: str
|
|
19
|
+
authentication_client: AuthenticationClient
|
|
20
|
+
|
|
21
|
+
def get_resource_metadata(self, shortcode: str) -> tuple[MetadataRetrieval, list[dict[str, str]]]:
|
|
22
|
+
url = f"{self.server}/v2/metadata/projects/{shortcode}/resources?format=JSON"
|
|
23
|
+
header = {"Authorization": f"Bearer {self.authentication_client.get_token()}"}
|
|
24
|
+
params = RequestParameters(method="GET", url=url, timeout=TIMEOUT, headers=header)
|
|
25
|
+
logger.debug("GET Resource Metadata")
|
|
26
|
+
log_request(params)
|
|
27
|
+
try:
|
|
28
|
+
response = requests.get(
|
|
29
|
+
url=params.url,
|
|
30
|
+
headers=params.headers,
|
|
31
|
+
timeout=params.timeout,
|
|
32
|
+
)
|
|
33
|
+
if response.ok:
|
|
34
|
+
# we log the response separately because if it was successful it will be too big
|
|
35
|
+
log_response(response, include_response_content=False)
|
|
36
|
+
return MetadataRetrieval.SUCCESS, response.json()
|
|
37
|
+
# here the response text is important
|
|
38
|
+
log_response(response)
|
|
39
|
+
return MetadataRetrieval.FAILURE, []
|
|
40
|
+
except Exception as err: # noqa: BLE001 (blind exception)
|
|
41
|
+
logger.error(err)
|
|
42
|
+
return MetadataRetrieval.FAILURE, []
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
from typing import Protocol
|
|
3
|
+
|
|
4
|
+
from rdflib import Literal
|
|
5
|
+
|
|
6
|
+
from dsp_tools.clients.authentication_client import AuthenticationClient
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class OntologyClient(Protocol):
|
|
10
|
+
"""
|
|
11
|
+
Protocol class/interface for the ontology endpoint in the API.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
server: str
|
|
15
|
+
authentication_client: AuthenticationClient
|
|
16
|
+
|
|
17
|
+
def get_last_modification_date(self, project_iri: str, onto_iri: str) -> str:
|
|
18
|
+
"""Get the last modification date of an ontology"""
|
|
19
|
+
|
|
20
|
+
def post_resource_cardinalities(self, cardinality_graph: dict[str, Any]) -> Literal | None:
|
|
21
|
+
"""Add cardinalities to an existing resource class."""
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from http import HTTPStatus
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
import requests
|
|
6
|
+
from loguru import logger
|
|
7
|
+
from rdflib import Graph
|
|
8
|
+
from rdflib import Literal
|
|
9
|
+
from rdflib import URIRef
|
|
10
|
+
from requests import ReadTimeout
|
|
11
|
+
from requests import Response
|
|
12
|
+
|
|
13
|
+
from dsp_tools.clients.authentication_client import AuthenticationClient
|
|
14
|
+
from dsp_tools.clients.ontology_client import OntologyClient
|
|
15
|
+
from dsp_tools.error.exceptions import BadCredentialsError
|
|
16
|
+
from dsp_tools.error.exceptions import UnexpectedApiResponseError
|
|
17
|
+
from dsp_tools.utils.rdflib_constants import KNORA_API
|
|
18
|
+
from dsp_tools.utils.request_utils import RequestParameters
|
|
19
|
+
from dsp_tools.utils.request_utils import log_and_raise_timeouts
|
|
20
|
+
from dsp_tools.utils.request_utils import log_request
|
|
21
|
+
from dsp_tools.utils.request_utils import log_response
|
|
22
|
+
|
|
23
|
+
TIMEOUT = 60
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass
|
|
27
|
+
class OntologyClientLive(OntologyClient):
|
|
28
|
+
"""
|
|
29
|
+
Client for the ontology endpoint in the API.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
server: str
|
|
33
|
+
authentication_client: AuthenticationClient
|
|
34
|
+
|
|
35
|
+
def get_last_modification_date(self, project_iri: str, onto_iri: str) -> Literal:
|
|
36
|
+
url = f"{self.server}/v2/ontologies/metadata"
|
|
37
|
+
header = {"X-Knora-Accept-Project": project_iri}
|
|
38
|
+
logger.debug("GET ontology metadata")
|
|
39
|
+
try:
|
|
40
|
+
response = self._get_and_log_request(url, header)
|
|
41
|
+
except (TimeoutError, ReadTimeout) as err:
|
|
42
|
+
log_and_raise_timeouts(err)
|
|
43
|
+
if response.ok:
|
|
44
|
+
date = _parse_last_modification_date(response.text, URIRef(onto_iri))
|
|
45
|
+
if not date:
|
|
46
|
+
raise UnexpectedApiResponseError(
|
|
47
|
+
f"Could not find the last modification date of the ontology '{onto_iri}' "
|
|
48
|
+
f"in the response: {response.text}"
|
|
49
|
+
)
|
|
50
|
+
return date
|
|
51
|
+
if response.status_code == HTTPStatus.FORBIDDEN:
|
|
52
|
+
raise BadCredentialsError("You do not have sufficient credentials to retrieve ontology metadata.")
|
|
53
|
+
else:
|
|
54
|
+
raise UnexpectedApiResponseError(
|
|
55
|
+
f"An unexpected response with the status code {response.status_code} was received from the API. "
|
|
56
|
+
f"Please consult 'warnings.log' for details."
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
def post_resource_cardinalities(self, cardinality_graph: dict[str, Any]) -> Literal | None:
|
|
60
|
+
url = f"{self.server}/v2/ontologies/cardinalities"
|
|
61
|
+
|
|
62
|
+
logger.debug("POST resource cardinalities to ontology")
|
|
63
|
+
try:
|
|
64
|
+
response = self._post_and_log_request(url, cardinality_graph)
|
|
65
|
+
except (TimeoutError, ReadTimeout) as err:
|
|
66
|
+
log_and_raise_timeouts(err)
|
|
67
|
+
if response.ok:
|
|
68
|
+
date = _parse_last_modification_date(response.text)
|
|
69
|
+
if not date:
|
|
70
|
+
raise UnexpectedApiResponseError(
|
|
71
|
+
f"Could not find the last modification date in the response: {response.text}"
|
|
72
|
+
)
|
|
73
|
+
return date
|
|
74
|
+
if response.status_code == HTTPStatus.FORBIDDEN:
|
|
75
|
+
raise BadCredentialsError(
|
|
76
|
+
"Only a project or system administrator can add cardinalities to resource classes. "
|
|
77
|
+
"Your permissions are insufficient for this action."
|
|
78
|
+
)
|
|
79
|
+
else:
|
|
80
|
+
logger.error(
|
|
81
|
+
f"During cardinality creation an unexpected response with the status code {response.status_code} "
|
|
82
|
+
f"was received from the API."
|
|
83
|
+
)
|
|
84
|
+
return None
|
|
85
|
+
|
|
86
|
+
def _post_and_log_request(
|
|
87
|
+
self,
|
|
88
|
+
url: str,
|
|
89
|
+
data: dict[str, Any] | None,
|
|
90
|
+
headers: dict[str, str] | None = None,
|
|
91
|
+
) -> Response:
|
|
92
|
+
data_dict, generic_headers = self._prepare_request(data, headers)
|
|
93
|
+
params = RequestParameters("POST", url, TIMEOUT, data_dict, generic_headers)
|
|
94
|
+
log_request(params)
|
|
95
|
+
response = requests.post(
|
|
96
|
+
url=params.url,
|
|
97
|
+
headers=params.headers,
|
|
98
|
+
data=params.data_serialized,
|
|
99
|
+
timeout=params.timeout,
|
|
100
|
+
)
|
|
101
|
+
log_response(response)
|
|
102
|
+
return response
|
|
103
|
+
|
|
104
|
+
def _get_and_log_request(
|
|
105
|
+
self,
|
|
106
|
+
url: str,
|
|
107
|
+
headers: dict[str, str] | None = None,
|
|
108
|
+
) -> Response:
|
|
109
|
+
_, generic_headers = self._prepare_request({}, headers)
|
|
110
|
+
params = RequestParameters(method="GET", url=url, timeout=TIMEOUT, headers=generic_headers)
|
|
111
|
+
log_request(params)
|
|
112
|
+
response = requests.get(
|
|
113
|
+
url=params.url,
|
|
114
|
+
headers=params.headers,
|
|
115
|
+
timeout=params.timeout,
|
|
116
|
+
)
|
|
117
|
+
log_response(response)
|
|
118
|
+
return response
|
|
119
|
+
|
|
120
|
+
def _prepare_request(
|
|
121
|
+
self, data: dict[str, Any] | None, headers: dict[str, str] | None
|
|
122
|
+
) -> tuple[dict[str, Any] | None, dict[str, str]]:
|
|
123
|
+
generic_headers = {
|
|
124
|
+
"Content-Type": "application/json",
|
|
125
|
+
"Authorization": f"Bearer {self.authentication_client.get_token()}",
|
|
126
|
+
}
|
|
127
|
+
data_dict = data if data else None
|
|
128
|
+
if headers:
|
|
129
|
+
generic_headers.update(headers)
|
|
130
|
+
return data_dict, generic_headers
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _parse_last_modification_date(response_text: str, onto_iri: URIRef | None = None) -> Literal | None:
|
|
134
|
+
g = Graph()
|
|
135
|
+
g.parse(data=response_text, format="json-ld")
|
|
136
|
+
result = next(g.objects(subject=onto_iri, predicate=KNORA_API.lastModificationDate), None)
|
|
137
|
+
if isinstance(result, Literal):
|
|
138
|
+
return result
|
|
139
|
+
return None
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from loguru import logger
|
|
2
|
+
|
|
3
|
+
from dsp_tools.commands.create.models.input_problems import CollectedProblems
|
|
4
|
+
from dsp_tools.commands.create.models.input_problems import CreateProblem
|
|
5
|
+
from dsp_tools.utils.ansi_colors import BOLD_RED
|
|
6
|
+
from dsp_tools.utils.ansi_colors import RED
|
|
7
|
+
from dsp_tools.utils.ansi_colors import RESET_TO_DEFAULT
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def print_problem_collection(problem_collection: CollectedProblems) -> None:
|
|
11
|
+
individual_problems = _create_individual_problem_strings(problem_collection.problems)
|
|
12
|
+
logger.error(problem_collection.header, individual_problems)
|
|
13
|
+
print(BOLD_RED, problem_collection.header, RESET_TO_DEFAULT)
|
|
14
|
+
print(RED, individual_problems, RESET_TO_DEFAULT)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _create_individual_problem_strings(problems: list[CreateProblem]) -> str:
|
|
18
|
+
str_list = [f"{p.problematic_object}: {p.problem!s}" for p in problems]
|
|
19
|
+
return " - " + "\n - ".join(str_list)
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
SALSAH_GUI = "http://api.knora.org/ontology/salsah-gui/v2#"
|
|
1
|
+
from rdflib import Namespace
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
KNORA_API_STR = "http://api.knora.org/ontology/knora-api/v2#"
|
|
4
|
+
SALSAH_GUI_STR = "http://api.knora.org/ontology/salsah-gui/v2#"
|
|
5
|
+
SALSAH_GUI = Namespace(SALSAH_GUI_STR)
|
|
6
|
+
|
|
7
|
+
UNIVERSAL_PREFIXES = {"knora-api": KNORA_API_STR, "salsah-gui": SALSAH_GUI_STR}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
from loguru import logger
|
|
4
|
+
from rdflib import Literal
|
|
5
|
+
from rdflib import URIRef
|
|
6
|
+
|
|
7
|
+
from dsp_tools.clients.ontology_client import OntologyClient
|
|
8
|
+
from dsp_tools.clients.ontology_client_live import OntologyClientLive
|
|
9
|
+
from dsp_tools.commands.create.models.input_problems import CollectedProblems
|
|
10
|
+
from dsp_tools.commands.create.models.input_problems import CreateProblem
|
|
11
|
+
from dsp_tools.commands.create.models.input_problems import ProblemType
|
|
12
|
+
from dsp_tools.commands.create.models.input_problems import UploadProblem
|
|
13
|
+
from dsp_tools.commands.create.models.parsed_ontology import ParsedClassCardinalities
|
|
14
|
+
from dsp_tools.commands.create.models.parsed_ontology import ParsedOntology
|
|
15
|
+
from dsp_tools.commands.create.models.parsed_ontology import ParsedPropertyCardinality
|
|
16
|
+
from dsp_tools.commands.create.models.server_project_info import CreatedIriCollection
|
|
17
|
+
from dsp_tools.commands.create.models.server_project_info import ProjectIriLookup
|
|
18
|
+
from dsp_tools.commands.create.serialisation.ontology import _make_one_cardinality_graph
|
|
19
|
+
from dsp_tools.commands.create.serialisation.ontology import make_ontology_base_graph
|
|
20
|
+
from dsp_tools.utils.data_formats.iri_util import from_dsp_iri_to_prefixed_iri
|
|
21
|
+
from dsp_tools.utils.rdflib_utils import serialise_json
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def add_all_cardinalities(
|
|
25
|
+
ontologies: list[ParsedOntology],
|
|
26
|
+
project_iri_lookup: ProjectIriLookup,
|
|
27
|
+
created_iris: CreatedIriCollection,
|
|
28
|
+
onto_client: OntologyClientLive,
|
|
29
|
+
) -> CollectedProblems | None:
|
|
30
|
+
all_problems = []
|
|
31
|
+
for onto in ontologies:
|
|
32
|
+
onto_iri = project_iri_lookup.onto_iris.get(onto.name)
|
|
33
|
+
# we do not inform about onto failures here, as it will have been done upstream
|
|
34
|
+
if onto_iri:
|
|
35
|
+
last_mod_date = onto_client.get_last_modification_date(project_iri_lookup.project_iri, onto_iri)
|
|
36
|
+
problems = _add_all_cardinalities_for_one_onto(
|
|
37
|
+
cardinalities=onto.cardinalities,
|
|
38
|
+
onto_iri=URIRef(onto_iri),
|
|
39
|
+
last_modification_date=last_mod_date,
|
|
40
|
+
onto_client=onto_client,
|
|
41
|
+
created_iris=created_iris,
|
|
42
|
+
)
|
|
43
|
+
all_problems.extend(problems)
|
|
44
|
+
if all_problems:
|
|
45
|
+
return CollectedProblems("During the cardinality creation the following problems occurred:", all_problems)
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _add_all_cardinalities_for_one_onto(
|
|
50
|
+
cardinalities: list[ParsedClassCardinalities],
|
|
51
|
+
onto_iri: URIRef,
|
|
52
|
+
last_modification_date: Literal,
|
|
53
|
+
onto_client: OntologyClient,
|
|
54
|
+
created_iris: CreatedIriCollection,
|
|
55
|
+
) -> list[CreateProblem]:
|
|
56
|
+
problems: list[CreateProblem] = []
|
|
57
|
+
for c in cardinalities:
|
|
58
|
+
# we do not inform about classes failures here, as it will have been done upstream
|
|
59
|
+
if c.class_iri not in created_iris.classes:
|
|
60
|
+
logger.warning(f"CARDINALITY: Class '{c.class_iri}' not in successes, no cardinalities added.")
|
|
61
|
+
continue
|
|
62
|
+
last_modification_date, creation_problems = _add_cardinalities_for_one_class(
|
|
63
|
+
resource_card=c,
|
|
64
|
+
onto_iri=onto_iri,
|
|
65
|
+
last_modification_date=last_modification_date,
|
|
66
|
+
onto_client=onto_client,
|
|
67
|
+
successful_props=created_iris.properties,
|
|
68
|
+
)
|
|
69
|
+
problems.extend(creation_problems)
|
|
70
|
+
return problems
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _add_cardinalities_for_one_class(
|
|
74
|
+
resource_card: ParsedClassCardinalities,
|
|
75
|
+
onto_iri: URIRef,
|
|
76
|
+
last_modification_date: Literal,
|
|
77
|
+
onto_client: OntologyClient,
|
|
78
|
+
successful_props: set[str],
|
|
79
|
+
) -> tuple[Literal, list[UploadProblem]]:
|
|
80
|
+
res_iri = URIRef(resource_card.class_iri)
|
|
81
|
+
problems = []
|
|
82
|
+
for one_card in resource_card.cards:
|
|
83
|
+
if one_card.propname not in successful_props:
|
|
84
|
+
logger.warning(f"CARDINALITY: Property '{one_card.propname}' not in successes, no cardinality added.")
|
|
85
|
+
continue
|
|
86
|
+
last_modification_date, problem = _add_one_cardinality(
|
|
87
|
+
one_card, res_iri, onto_iri, last_modification_date, onto_client
|
|
88
|
+
)
|
|
89
|
+
if problem:
|
|
90
|
+
problems.append(problem)
|
|
91
|
+
return last_modification_date, problems
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _add_one_cardinality(
|
|
95
|
+
card: ParsedPropertyCardinality,
|
|
96
|
+
res_iri: URIRef,
|
|
97
|
+
onto_iri: URIRef,
|
|
98
|
+
last_modification_date: Literal,
|
|
99
|
+
onto_client: OntologyClient,
|
|
100
|
+
) -> tuple[Literal, UploadProblem | None]:
|
|
101
|
+
card_serialised = _serialise_card(card, res_iri, onto_iri, last_modification_date)
|
|
102
|
+
new_mod_date = onto_client.post_resource_cardinalities(card_serialised)
|
|
103
|
+
if not new_mod_date:
|
|
104
|
+
prefixed_cls = from_dsp_iri_to_prefixed_iri(str(res_iri))
|
|
105
|
+
prefixed_prop = from_dsp_iri_to_prefixed_iri(card.propname)
|
|
106
|
+
return last_modification_date, UploadProblem(
|
|
107
|
+
f"{prefixed_cls} / {prefixed_prop}",
|
|
108
|
+
ProblemType.CARDINALITY_COULD_NOT_BE_ADDED,
|
|
109
|
+
)
|
|
110
|
+
return new_mod_date, None
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _serialise_card(
|
|
114
|
+
card: ParsedPropertyCardinality, res_iri: URIRef, onto_iri: URIRef, last_modification_date: Literal
|
|
115
|
+
) -> dict[str, Any]:
|
|
116
|
+
onto_g = make_ontology_base_graph(onto_iri, last_modification_date)
|
|
117
|
+
onto_serialised = next(iter(serialise_json(onto_g)))
|
|
118
|
+
card_g = _make_one_cardinality_graph(card, res_iri)
|
|
119
|
+
card_serialised = serialise_json(card_g)
|
|
120
|
+
onto_serialised["@graph"] = card_serialised
|
|
121
|
+
return onto_serialised
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from rdflib import OWL
|
|
2
|
+
from rdflib import Literal
|
|
3
|
+
|
|
4
|
+
from dsp_tools.commands.create.models.parsed_ontology import Cardinality
|
|
5
|
+
from dsp_tools.commands.create.models.rdf_ontology import RdfCardinalityRestriction
|
|
6
|
+
|
|
7
|
+
PARSED_CARDINALITY_TO_RDF = {
|
|
8
|
+
Cardinality.C_1: RdfCardinalityRestriction(OWL.cardinality, Literal(1)),
|
|
9
|
+
Cardinality.C_0_1: RdfCardinalityRestriction(OWL.maxCardinality, Literal(1)),
|
|
10
|
+
Cardinality.C_1_N: RdfCardinalityRestriction(OWL.minCardinality, Literal(1)),
|
|
11
|
+
Cardinality.C_0_N: RdfCardinalityRestriction(OWL.minCardinality, Literal(0)),
|
|
12
|
+
}
|
|
@@ -7,17 +7,26 @@ from enum import StrEnum
|
|
|
7
7
|
@dataclass
|
|
8
8
|
class CollectedProblems:
|
|
9
9
|
header: str
|
|
10
|
-
problems: list[
|
|
10
|
+
problems: list[CreateProblem]
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
@dataclass
|
|
14
|
-
class
|
|
14
|
+
class CreateProblem:
|
|
15
15
|
problematic_object: str
|
|
16
16
|
problem: ProblemType
|
|
17
17
|
|
|
18
18
|
|
|
19
|
+
@dataclass
|
|
20
|
+
class InputProblem(CreateProblem): ...
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class UploadProblem(CreateProblem): ...
|
|
25
|
+
|
|
26
|
+
|
|
19
27
|
class ProblemType(StrEnum):
|
|
20
28
|
PREFIX_COULD_NOT_BE_RESOLVED = (
|
|
21
29
|
"The prefix used is not defined in the 'prefix' section of the file, "
|
|
22
30
|
"nor does it belong to one of the project ontologies."
|
|
23
31
|
)
|
|
32
|
+
CARDINALITY_COULD_NOT_BE_ADDED = "The cardinality could not be added."
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from rdflib import Literal
|
|
6
|
+
from rdflib import URIRef
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class RdfResourceCardinality:
|
|
11
|
+
resource_iri: URIRef
|
|
12
|
+
on_property: URIRef
|
|
13
|
+
cardinality: RdfCardinalityRestriction
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class RdfCardinalityRestriction:
|
|
18
|
+
owl_property: URIRef
|
|
19
|
+
cardinality_value: Literal
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from dataclasses import field
|
|
3
|
+
|
|
4
|
+
from dsp_tools.commands.create.constants import KNORA_API_STR
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass
|
|
8
|
+
class ProjectIriLookup:
|
|
9
|
+
project_iri: str
|
|
10
|
+
onto_iris: dict[str, str] = field(default_factory=dict)
|
|
11
|
+
|
|
12
|
+
def add_onto(self, name: str, iri: str) -> None:
|
|
13
|
+
self.onto_iris[name] = iri
|
|
14
|
+
|
|
15
|
+
def get_onto_iri(self, name: str) -> str | None:
|
|
16
|
+
return self.onto_iris.get(name)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
@dataclass
|
|
20
|
+
class CreatedIriCollection:
|
|
21
|
+
classes: set[str] = field(default_factory=set)
|
|
22
|
+
properties: set[str] = field(default_factory=set)
|
|
23
|
+
|
|
24
|
+
def __post_init__(self) -> None:
|
|
25
|
+
self.properties.update({f"{KNORA_API_STR}seqnum", f"{KNORA_API_STR}isPartOf"})
|
|
@@ -2,7 +2,7 @@ from typing import Any
|
|
|
2
2
|
from typing import cast
|
|
3
3
|
|
|
4
4
|
from dsp_tools.commands.create.models.input_problems import CollectedProblems
|
|
5
|
-
from dsp_tools.commands.create.models.input_problems import
|
|
5
|
+
from dsp_tools.commands.create.models.input_problems import CreateProblem
|
|
6
6
|
from dsp_tools.commands.create.models.input_problems import ProblemType
|
|
7
7
|
from dsp_tools.commands.create.models.parsed_ontology import Cardinality
|
|
8
8
|
from dsp_tools.commands.create.models.parsed_ontology import ParsedClass
|
|
@@ -46,7 +46,7 @@ def parse_ontology(ontology_json: dict[str, Any], prefixes: dict[str, str]) -> P
|
|
|
46
46
|
|
|
47
47
|
def _parse_properties(
|
|
48
48
|
properties_list: list[dict[str, Any]], current_onto_prefix: str
|
|
49
|
-
) -> tuple[list[ParsedProperty], list[
|
|
49
|
+
) -> tuple[list[ParsedProperty], list[CreateProblem]]:
|
|
50
50
|
parsed = []
|
|
51
51
|
for prop in properties_list:
|
|
52
52
|
parsed.append(ParsedProperty(f"{current_onto_prefix}{prop['name']}", prop))
|
|
@@ -55,7 +55,7 @@ def _parse_properties(
|
|
|
55
55
|
|
|
56
56
|
def _parse_classes(
|
|
57
57
|
classes_list: list[dict[str, Any]], current_onto_prefix: str
|
|
58
|
-
) -> tuple[list[ParsedClass], list[
|
|
58
|
+
) -> tuple[list[ParsedClass], list[CreateProblem]]:
|
|
59
59
|
parsed = []
|
|
60
60
|
for cls in classes_list:
|
|
61
61
|
parsed.append(ParsedClass(f"{current_onto_prefix}{cls['name']}", cls))
|
|
@@ -64,7 +64,7 @@ def _parse_classes(
|
|
|
64
64
|
|
|
65
65
|
def _parse_cardinalities(
|
|
66
66
|
classes_list: list[dict[str, Any]], current_onto_prefix: str, prefixes: dict[str, str]
|
|
67
|
-
) -> tuple[list[ParsedClassCardinalities], list[
|
|
67
|
+
) -> tuple[list[ParsedClassCardinalities], list[CreateProblem]]:
|
|
68
68
|
parsed = []
|
|
69
69
|
failures = []
|
|
70
70
|
for c in classes_list:
|
|
@@ -79,7 +79,7 @@ def _parse_cardinalities(
|
|
|
79
79
|
|
|
80
80
|
def _parse_one_class_cardinality(
|
|
81
81
|
cls_json: dict[str, Any], current_onto_prefix: str, prefixes: dict[str, str]
|
|
82
|
-
) -> ParsedClassCardinalities | list[
|
|
82
|
+
) -> ParsedClassCardinalities | list[CreateProblem]:
|
|
83
83
|
failures = []
|
|
84
84
|
parsed = []
|
|
85
85
|
for c in cls_json["cardinalities"]:
|
|
@@ -96,10 +96,10 @@ def _parse_one_class_cardinality(
|
|
|
96
96
|
|
|
97
97
|
def _parse_one_cardinality(
|
|
98
98
|
card_json: dict[str, str | int], current_onto_prefix: str, prefixes: dict[str, str]
|
|
99
|
-
) -> ParsedPropertyCardinality |
|
|
99
|
+
) -> ParsedPropertyCardinality | CreateProblem:
|
|
100
100
|
prp_name = cast(str, card_json["propname"])
|
|
101
101
|
if not (resolved := resolve_to_absolute_iri(prp_name, current_onto_prefix, prefixes)):
|
|
102
|
-
return
|
|
102
|
+
return CreateProblem(prp_name, ProblemType.PREFIX_COULD_NOT_BE_RESOLVED)
|
|
103
103
|
gui = cast(int | None, card_json.get("gui_order"))
|
|
104
104
|
return ParsedPropertyCardinality(
|
|
105
105
|
propname=resolved,
|
|
@@ -56,7 +56,7 @@ def _parse_metadata(project_json: dict[str, Any]) -> ParsedProjectMetadata:
|
|
|
56
56
|
longname=project_json["longname"],
|
|
57
57
|
descriptions=project_json["descriptions"],
|
|
58
58
|
keywords=project_json["keywords"],
|
|
59
|
-
enabled_licenses=project_json
|
|
59
|
+
enabled_licenses=project_json.get("enabled_licenses", []),
|
|
60
60
|
)
|
|
61
61
|
|
|
62
62
|
|
|
@@ -2,7 +2,7 @@ from typing import Any
|
|
|
2
2
|
|
|
3
3
|
import regex
|
|
4
4
|
|
|
5
|
-
from dsp_tools.commands.create.constants import
|
|
5
|
+
from dsp_tools.commands.create.constants import KNORA_API_STR
|
|
6
6
|
from dsp_tools.commands.create.constants import UNIVERSAL_PREFIXES
|
|
7
7
|
from dsp_tools.utils.data_formats.uri_util import is_uri
|
|
8
8
|
|
|
@@ -14,7 +14,7 @@ def resolve_to_absolute_iri(prefixed: str, current_onto: str, prefix_lookup: dic
|
|
|
14
14
|
return f"{current_onto}{prefixed.lstrip(':')}"
|
|
15
15
|
segments = prefixed.split(":", maxsplit=1)
|
|
16
16
|
if len(segments) == 1:
|
|
17
|
-
return f"{
|
|
17
|
+
return f"{KNORA_API_STR}{segments[0]}"
|
|
18
18
|
if not (found := prefix_lookup.get(segments[0])):
|
|
19
19
|
return None
|
|
20
20
|
return f"{found}{segments[1]}"
|
|
File without changes
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from rdflib import OWL
|
|
2
|
+
from rdflib import RDF
|
|
3
|
+
from rdflib import RDFS
|
|
4
|
+
from rdflib import BNode
|
|
5
|
+
from rdflib import Graph
|
|
6
|
+
from rdflib import Literal
|
|
7
|
+
from rdflib import URIRef
|
|
8
|
+
|
|
9
|
+
from dsp_tools.commands.create.constants import SALSAH_GUI
|
|
10
|
+
from dsp_tools.commands.create.create_on_server.mappers import PARSED_CARDINALITY_TO_RDF
|
|
11
|
+
from dsp_tools.commands.create.models.parsed_ontology import ParsedPropertyCardinality
|
|
12
|
+
from dsp_tools.utils.rdflib_constants import KNORA_API
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def make_ontology_base_graph(onto_iri: URIRef, last_modification_date: Literal) -> Graph:
|
|
16
|
+
g = Graph()
|
|
17
|
+
g.add((onto_iri, RDF.type, OWL.Ontology))
|
|
18
|
+
g.add((onto_iri, KNORA_API.lastModificationDate, last_modification_date))
|
|
19
|
+
return g
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def make_cardinality_graph_for_request(
|
|
23
|
+
card: ParsedPropertyCardinality, res_iri: URIRef, onto_iri: URIRef, last_modification_date: Literal
|
|
24
|
+
) -> Graph:
|
|
25
|
+
g = make_ontology_base_graph(onto_iri, last_modification_date)
|
|
26
|
+
g += _make_one_cardinality_graph(card, res_iri)
|
|
27
|
+
return g
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _make_one_cardinality_graph(card: ParsedPropertyCardinality, res_iri: URIRef) -> Graph:
|
|
31
|
+
card_info = PARSED_CARDINALITY_TO_RDF[card.cardinality]
|
|
32
|
+
g = Graph()
|
|
33
|
+
bn_card = BNode()
|
|
34
|
+
g.add((res_iri, RDF.type, OWL.Class))
|
|
35
|
+
g.add((res_iri, RDFS.subClassOf, bn_card))
|
|
36
|
+
g.add((bn_card, RDF.type, OWL.Restriction))
|
|
37
|
+
g.add((bn_card, card_info.owl_property, card_info.cardinality_value))
|
|
38
|
+
g.add((bn_card, OWL.onProperty, URIRef(card.propname)))
|
|
39
|
+
if card.gui_order is not None:
|
|
40
|
+
g.add((bn_card, SALSAH_GUI.guiOrder, Literal(card.gui_order)))
|
|
41
|
+
return g
|