Skip to content

Commit

Permalink
serialise link value
Browse files Browse the repository at this point in the history
  • Loading branch information
Nora-Olivia-Ammann committed Nov 15, 2024
1 parent 716e58e commit 6bace5f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ def serialise(self) -> dict[str, Any]:
return serialised


class SerialiseLink(SerialiseValue):
"""A LinkValue to be serialised."""

value: str

def serialise(self) -> dict[str, Any]:
serialised = {
"@type": "knora-api:LinkValue",
"knora-api:linkValueHasTargetIri": {
"@id": self.value,
},
}
serialised.update(self._get_optionals())
return serialised


class SerialiseSimpletext(SerialiseValue):
"""A Simpletext to be serialised."""

Expand Down
29 changes: 19 additions & 10 deletions src/dsp_tools/commands/xmlupload/resource_create_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseGeometry
from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseGeoname
from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseInterval
from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseLink
from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseProperty
from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseRichtext
from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseSimpletext
Expand Down Expand Up @@ -133,7 +134,7 @@ def _make_resource(
res.update(_make_bitstream_file_value(bitstream_information))
return res

def _make_values(self, resource: XMLResource, res_bnode: BNode, namespaces: dict[str, Namespace]) -> dict[str, Any]:
def _make_values(self, resource: XMLResource, res_bnode: BNode, namespaces: dict[str, Namespace]) -> dict[str, Any]: # noqa:PLR0912 (Too many branches)
def prop_name(p: XMLProperty) -> str:
if p.valtype != "resptr":
return p.name
Expand Down Expand Up @@ -198,6 +199,11 @@ def make_values(p: XMLProperty) -> list[dict[str, Any]]:
permissions_lookup=self.permissions_lookup,
)
properties_serialised.update(transformed_prop.serialise())
case "resptr":
transformed_prop = _transform_into_link_prop(
prop=prop, permissions_lookup=self.permissions_lookup, iri_resolver=self.iri_resolver
)
properties_serialised.update(transformed_prop.serialise())
# serialised with rdflib
case "integer":
int_prop_name = self._get_absolute_prop_iri(prop.name, namespaces)
Expand Down Expand Up @@ -228,8 +234,6 @@ def _get_absolute_prop_iri(self, prefixed_prop: str, namespaces: dict[str, Names

def _make_value(self, value: XMLValue, value_type: str) -> dict[str, Any]:
match value_type:
case "resptr":
res = _make_link_value(value, self.iri_resolver)
case "list":
res = _make_list_value(value, self.listnode_lookup)
case _:
Expand Down Expand Up @@ -402,7 +406,16 @@ def _transform_into_interval_value(value: XMLValue, permissions_lookup: dict[str
raise BaseError(f"Could not parse interval value: {s}")


def _make_link_value(value: XMLValue, iri_resolver: IriResolver) -> dict[str, Any]:
def _transform_into_link_prop(
prop: XMLProperty, permissions_lookup: dict[str, Permissions], iri_resolver: IriResolver
) -> SerialiseProperty:
vals = [_transform_into_link_value(v, permissions_lookup, iri_resolver) for v in prop.values]
return SerialiseProperty(property_name=prop.name, values=vals)


def _transform_into_link_value(
value: XMLValue, permissions_lookup: dict[str, Permissions], iri_resolver: IriResolver
) -> SerialiseLink:
s = _assert_is_string(value.value)
if is_resource_iri(s):
iri = s
Expand All @@ -415,12 +428,8 @@ def _make_link_value(value: XMLValue, iri_resolver: IriResolver) -> dict[str, An
f"See {WARNINGS_SAVEPATH} for more information."
)
raise BaseError(msg)
return {
"@type": "knora-api:LinkValue",
"knora-api:linkValueHasTargetIri": {
"@id": iri,
},
}
permission_str = _get_permission_str(value.permissions, permissions_lookup)
return SerialiseLink(value=iri, permissions=permission_str, comment=value.comment)


def _make_list_value(value: XMLValue, iri_lookup: dict[str, str]) -> dict[str, Any]:
Expand Down

0 comments on commit 6bace5f

Please sign in to comment.