From b64c4f873f7baae8c081b2a90f9ba8519d380672 Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:11:29 +0100 Subject: [PATCH 01/11] Update serialise_value.py --- .../models/serialise/serialise_value.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py index ae680e765e..45c29c2f4d 100644 --- a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py +++ b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py @@ -51,6 +51,23 @@ def serialise(self) -> dict[str, Any]: return serialised +@dataclass(frozen=True) +class SerialiseGeoname(SerialiseValue): + """A GeonameValue to be serialised.""" + + value: str + permissions: str | None + comment: str | None + + def serialise(self) -> dict[str, Any]: + serialised = { + "@type": "knora-api:GeonameValue", + "knora-api:geonameValueAsGeonameCode": self.value, + } + serialised.update(self._get_optionals()) + return serialised + + @dataclass(frozen=True) class SerialiseURI(SerialiseValue): """A UriValue to be serialised.""" From 0e75cf01212ac1ddc036ee2691e357cc33bf4cc0 Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:13:18 +0100 Subject: [PATCH 02/11] Update resource_create_client.py --- .../xmlupload/resource_create_client.py | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/dsp_tools/commands/xmlupload/resource_create_client.py b/src/dsp_tools/commands/xmlupload/resource_create_client.py index 732b132052..11d132baee 100644 --- a/src/dsp_tools/commands/xmlupload/resource_create_client.py +++ b/src/dsp_tools/commands/xmlupload/resource_create_client.py @@ -27,6 +27,7 @@ from dsp_tools.commands.xmlupload.models.permission import Permissions from dsp_tools.commands.xmlupload.models.serialise.jsonld_serialiser import serialise_property_graph from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseColor +from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseGeoname from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseProperty from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseURI from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseValue @@ -135,15 +136,12 @@ def make_values(p: XMLProperty) -> list[dict[str, Any]]: # To frame the json-ld correctly, we need one property used in the graph. It does not matter which. last_prop_name = None - str_value_to_serialiser_mapper = { - "uri": SerialiseURI, - "color": SerialiseColor, - } + str_value_to_serialiser_mapper = {"uri": SerialiseURI, "color": SerialiseColor, "geoname": SerialiseGeoname} for prop in resource.properties: match prop.valtype: # serialised as dict - case "uri" | "color" as val_type: + case "uri" | "color" | "geoname" as val_type: transformed_prop = _transform_into_serialise_prop( prop=prop, permissions_lookup=self.permissions_lookup, @@ -186,8 +184,6 @@ def _make_value(self, value: XMLValue, value_type: str) -> dict[str, Any]: # no res = _make_decimal_value(value) case "geometry": res = _make_geometry_value(value) - case "geoname": - res = _make_geoname_value(value) case "interval": res = _make_interval_value(value) case "resptr": @@ -320,13 +316,6 @@ def _make_geometry_value(value: XMLValue) -> dict[str, Any]: } -def _make_geoname_value(value: XMLValue) -> dict[str, Any]: - return { - "@type": "knora-api:GeonameValue", - "knora-api:geonameValueAsGeonameCode": value.value, - } - - def _make_boolean_prop( prop: XMLProperty, res_bn: BNode, prop_name: URIRef, permissions_lookup: dict[str, Permissions] ) -> Graph: From 56c8ad86e6487b34290e20810b26f8f87bafb16b Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:15:39 +0100 Subject: [PATCH 03/11] serialise time --- .../models/serialise/serialise_value.py | 20 ++++++++++++++++ .../xmlupload/resource_create_client.py | 24 +++++++------------ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py index 45c29c2f4d..181f955688 100644 --- a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py +++ b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py @@ -68,6 +68,26 @@ def serialise(self) -> dict[str, Any]: return serialised +@dataclass(frozen=True) +class SerialiseTime(SerialiseValue): + """A TimeValue to be serialised.""" + + value: str + permissions: str | None + comment: str | None + + def serialise(self) -> dict[str, Any]: + serialised = { + "@type": "knora-api:TimeValue", + "knora-api:timeValueAsTimeStamp": { + "@type": "xsd:dateTimeStamp", + "@value": self.value, + }, + } + serialised.update(self._get_optionals()) + return serialised + + @dataclass(frozen=True) class SerialiseURI(SerialiseValue): """A UriValue to be serialised.""" diff --git a/src/dsp_tools/commands/xmlupload/resource_create_client.py b/src/dsp_tools/commands/xmlupload/resource_create_client.py index 11d132baee..549a43f1fb 100644 --- a/src/dsp_tools/commands/xmlupload/resource_create_client.py +++ b/src/dsp_tools/commands/xmlupload/resource_create_client.py @@ -29,6 +29,7 @@ from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseColor from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseGeoname from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseProperty +from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseTime from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseURI from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseValue from dsp_tools.models.exceptions import BaseError @@ -136,12 +137,17 @@ def make_values(p: XMLProperty) -> list[dict[str, Any]]: # To frame the json-ld correctly, we need one property used in the graph. It does not matter which. last_prop_name = None - str_value_to_serialiser_mapper = {"uri": SerialiseURI, "color": SerialiseColor, "geoname": SerialiseGeoname} + str_value_to_serialiser_mapper = { + "uri": SerialiseURI, + "color": SerialiseColor, + "geoname": SerialiseGeoname, + "time": SerialiseTime, + } for prop in resource.properties: match prop.valtype: # serialised as dict - case "uri" | "color" | "geoname" as val_type: + case "uri" | "color" | "geoname" | "time" as val_type: transformed_prop = _transform_into_serialise_prop( prop=prop, permissions_lookup=self.permissions_lookup, @@ -176,7 +182,7 @@ def _get_absolute_prop_iri(self, prefixed_prop: str, namespaces: dict[str, Names raise InputError(f"Could not find namespace for prefix: {prefix}") return namespace[prop] - def _make_value(self, value: XMLValue, value_type: str) -> dict[str, Any]: # noqa: PLR0912 (too many branches) + def _make_value(self, value: XMLValue, value_type: str) -> dict[str, Any]: match value_type: case "date": res = _make_date_value(value) @@ -192,8 +198,6 @@ def _make_value(self, value: XMLValue, value_type: str) -> dict[str, Any]: # no res = _make_list_value(value, self.listnode_lookup) case "text": res = _make_text_value(value, self.iri_resolver) - case "time": - res = _make_time_value(value) case _: raise UserError(f"Unknown value type: {value_type}") if value.comment: @@ -437,16 +441,6 @@ def _make_text_value(value: XMLValue, iri_resolver: IriResolver) -> dict[str, An assert_never(value.value) -def _make_time_value(value: XMLValue) -> dict[str, Any]: - return { - "@type": "knora-api:TimeValue", - "knora-api:timeValueAsTimeStamp": { - "@type": "xsd:dateTimeStamp", - "@value": value.value, - }, - } - - def _transform_into_serialise_prop( prop: XMLProperty, permissions_lookup: dict[str, Permissions], From 1751d05f528c79a29578a566aecd7bbcba81426d Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:30:18 +0100 Subject: [PATCH 04/11] transform decimal --- .../models/serialise/serialise_value.py | 37 ++++++++++++++++++ .../xmlupload/resource_create_client.py | 39 +++++++++++-------- 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py index 181f955688..3ecf8a9069 100644 --- a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py +++ b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py @@ -51,6 +51,43 @@ def serialise(self) -> dict[str, Any]: return serialised +@dataclass(frozen=True) +class SerialiseDecimal(SerialiseValue): + """A to be serialised.""" + + value: str + permissions: str | None + comment: str | None + + def serialise(self) -> dict[str, Any]: + serialised = { + "@type": "knora-api:DecimalValue", + "knora-api:decimalValueAsDecimal": { + "@type": "xsd:decimal", + "@value": self.value, + }, + } + serialised.update(self._get_optionals()) + return serialised + + +@dataclass(frozen=True) +class SerialiseGeometry(SerialiseValue): + """A GeomValue to be serialised.""" + + value: str + permissions: str | None + comment: str | None + + def serialise(self) -> dict[str, Any]: + serialised = { + "@type": "knora-api:GeomValue", + "knora-api:geometryValueAsGeometry": self.value, + } + serialised.update(self._get_optionals()) + return serialised + + @dataclass(frozen=True) class SerialiseGeoname(SerialiseValue): """A GeonameValue to be serialised.""" diff --git a/src/dsp_tools/commands/xmlupload/resource_create_client.py b/src/dsp_tools/commands/xmlupload/resource_create_client.py index 549a43f1fb..e4c6fd772d 100644 --- a/src/dsp_tools/commands/xmlupload/resource_create_client.py +++ b/src/dsp_tools/commands/xmlupload/resource_create_client.py @@ -27,6 +27,8 @@ from dsp_tools.commands.xmlupload.models.permission import Permissions from dsp_tools.commands.xmlupload.models.serialise.jsonld_serialiser import serialise_property_graph from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseColor +from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseDecimal +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 SerialiseProperty from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseTime @@ -138,10 +140,10 @@ def make_values(p: XMLProperty) -> list[dict[str, Any]]: last_prop_name = None str_value_to_serialiser_mapper = { - "uri": SerialiseURI, "color": SerialiseColor, "geoname": SerialiseGeoname, "time": SerialiseTime, + "uri": SerialiseURI, } for prop in resource.properties: @@ -154,6 +156,9 @@ def make_values(p: XMLProperty) -> list[dict[str, Any]]: seraliser=str_value_to_serialiser_mapper[val_type], ) properties_serialised.update(transformed_prop.serialise()) + case "decimal": + transformed_prop = _transform_decimal_prop(prop=prop, permissions_lookup=self.permissions_lookup) + properties_serialised.update(transformed_prop.serialise()) # serialised with rdflib case "integer": int_prop_name = self._get_absolute_prop_iri(prop.name, namespaces) @@ -186,8 +191,6 @@ def _make_value(self, value: XMLValue, value_type: str) -> dict[str, Any]: match value_type: case "date": res = _make_date_value(value) - case "decimal": - res = _make_decimal_value(value) case "geometry": res = _make_geometry_value(value) case "interval": @@ -299,25 +302,29 @@ def _make_date_value(value: XMLValue) -> dict[str, Any]: return res -def _make_decimal_value(value: XMLValue) -> dict[str, Any]: +def _transform_decimal_prop(prop: XMLProperty, permissions_lookup: dict[str, Permissions]) -> SerialiseProperty: + vals = [_transform_into_serialise_decimal(v, permissions_lookup) for v in prop.values] + return SerialiseProperty(property_name=prop.name, values=vals) + + +def _transform_into_serialise_decimal(value: XMLValue, permissions_lookup: dict[str, Permissions]) -> SerialiseDecimal: s = _assert_is_string(value.value) - return { - "@type": "knora-api:DecimalValue", - "knora-api:decimalValueAsDecimal": { - "@type": "xsd:decimal", - "@value": str(float(s)), - }, - } + val = str(float(s)) + permission_str = _get_permission_str(value.permissions, permissions_lookup) + return SerialiseDecimal(value=val, permissions=permission_str, comment=value.comment) + + +def _transform_geometry_prop(prop: XMLProperty, permissions_lookup: dict[str, Permissions]) -> SerialiseProperty: + vals = [_make_geometry_value(v, permissions_lookup) for v in prop.values] + return SerialiseProperty(property_name=prop.name, values=vals) -def _make_geometry_value(value: XMLValue) -> dict[str, Any]: +def _make_geometry_value(value: XMLValue, permissions_lookup: dict[str, Permissions]) -> SerialiseGeometry: s = _assert_is_string(value.value) # this removes all whitespaces from the embedded json string encoded_value = json.dumps(json.loads(s)) - return { - "@type": "knora-api:GeomValue", - "knora-api:geometryValueAsGeometry": encoded_value, - } + permission_str = _get_permission_str(value.permissions, permissions_lookup) + return SerialiseGeometry(value=encoded_value, permissions=permission_str, comment=value.comment) def _make_boolean_prop( From 50dbf570bf02bba1a4372cca5a099dd659cc29d0 Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:30:59 +0100 Subject: [PATCH 05/11] Update resource_create_client.py --- src/dsp_tools/commands/xmlupload/resource_create_client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/dsp_tools/commands/xmlupload/resource_create_client.py b/src/dsp_tools/commands/xmlupload/resource_create_client.py index e4c6fd772d..241ac3928c 100644 --- a/src/dsp_tools/commands/xmlupload/resource_create_client.py +++ b/src/dsp_tools/commands/xmlupload/resource_create_client.py @@ -159,6 +159,9 @@ def make_values(p: XMLProperty) -> list[dict[str, Any]]: case "decimal": transformed_prop = _transform_decimal_prop(prop=prop, permissions_lookup=self.permissions_lookup) properties_serialised.update(transformed_prop.serialise()) + case "geometry": + transformed_prop = _transform_geometry_prop(prop=prop, permissions_lookup=self.permissions_lookup) + properties_serialised.update(transformed_prop.serialise()) # serialised with rdflib case "integer": int_prop_name = self._get_absolute_prop_iri(prop.name, namespaces) @@ -191,8 +194,6 @@ def _make_value(self, value: XMLValue, value_type: str) -> dict[str, Any]: match value_type: case "date": res = _make_date_value(value) - case "geometry": - res = _make_geometry_value(value) case "interval": res = _make_interval_value(value) case "resptr": From a8e7ab18c05a03ce7449f282b3560f80134df336 Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:46:57 +0100 Subject: [PATCH 06/11] change text value --- .../models/serialise/serialise_value.py | 37 ++++++++++++ .../xmlupload/resource_create_client.py | 57 ++++++++++++------- 2 files changed, 72 insertions(+), 22 deletions(-) diff --git a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py index 3ecf8a9069..d23fa4d105 100644 --- a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py +++ b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py @@ -105,6 +105,43 @@ def serialise(self) -> dict[str, Any]: return serialised +@dataclass(frozen=True) +class SerialiseSimpletext(SerialiseValue): + """A Simpletext to be serialised.""" + + value: str + permissions: str | None + comment: str | None + + def serialise(self) -> dict[str, Any]: + serialised = { + "@type": "knora-api:TextValue", + "knora-api:valueAsString": self.value, + } + serialised.update(self._get_optionals()) + return serialised + + +@dataclass(frozen=True) +class SerialiseRichtext(SerialiseValue): + """A Richtext to be serialised.""" + + value: str + permissions: str | None + comment: str | None + + def serialise(self) -> dict[str, Any]: + serialised = { + "@type": "knora-api:TextValue", + "knora-api:textValueAsXml": self.value, + "knora-api:textValueHasMapping": { + "@id": "http://rdfh.ch/standoff/mappings/StandardMapping", + }, + } + serialised.update(self._get_optionals()) + return serialised + + @dataclass(frozen=True) class SerialiseTime(SerialiseValue): """A TimeValue to be serialised.""" diff --git a/src/dsp_tools/commands/xmlupload/resource_create_client.py b/src/dsp_tools/commands/xmlupload/resource_create_client.py index 241ac3928c..e62b7256ba 100644 --- a/src/dsp_tools/commands/xmlupload/resource_create_client.py +++ b/src/dsp_tools/commands/xmlupload/resource_create_client.py @@ -31,6 +31,8 @@ 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 SerialiseProperty +from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseRichtext +from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseSimpletext from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseTime from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseURI from dsp_tools.commands.xmlupload.models.serialise.serialise_value import SerialiseValue @@ -162,6 +164,13 @@ def make_values(p: XMLProperty) -> list[dict[str, Any]]: case "geometry": transformed_prop = _transform_geometry_prop(prop=prop, permissions_lookup=self.permissions_lookup) properties_serialised.update(transformed_prop.serialise()) + case "text": + transformed_prop = _transform_text_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) @@ -200,8 +209,6 @@ def _make_value(self, value: XMLValue, value_type: str) -> dict[str, Any]: res = _make_link_value(value, self.iri_resolver) case "list": res = _make_list_value(value, self.listnode_lookup) - case "text": - res = _make_text_value(value, self.iri_resolver) case _: raise UserError(f"Unknown value type: {value_type}") if value.comment: @@ -304,7 +311,7 @@ def _make_date_value(value: XMLValue) -> dict[str, Any]: def _transform_decimal_prop(prop: XMLProperty, permissions_lookup: dict[str, Permissions]) -> SerialiseProperty: - vals = [_transform_into_serialise_decimal(v, permissions_lookup) for v in prop.values] + vals: list[SerialiseValue] = [_transform_into_serialise_decimal(v, permissions_lookup) for v in prop.values] return SerialiseProperty(property_name=prop.name, values=vals) @@ -316,7 +323,7 @@ def _transform_into_serialise_decimal(value: XMLValue, permissions_lookup: dict[ def _transform_geometry_prop(prop: XMLProperty, permissions_lookup: dict[str, Permissions]) -> SerialiseProperty: - vals = [_make_geometry_value(v, permissions_lookup) for v in prop.values] + vals: list[SerialiseValue] = [_make_geometry_value(v, permissions_lookup) for v in prop.values] return SerialiseProperty(property_name=prop.name, values=vals) @@ -429,24 +436,30 @@ def _make_list_value(value: XMLValue, iri_lookup: dict[str, str]) -> dict[str, A raise BaseError(msg) -def _make_text_value(value: XMLValue, iri_resolver: IriResolver) -> dict[str, Any]: - match value.value: - case str() as s: - return { - "@type": "knora-api:TextValue", - "knora-api:valueAsString": s, - } - case FormattedTextValue() as xml: - xml_with_iris = xml.with_iris(iri_resolver) - return { - "@type": "knora-api:TextValue", - "knora-api:textValueAsXml": xml_with_iris.as_xml(), - "knora-api:textValueHasMapping": { - "@id": "http://rdfh.ch/standoff/mappings/StandardMapping", - }, - } - case _: - assert_never(value.value) +def _transform_text_prop( + prop: XMLProperty, permissions_lookup: dict[str, Permissions], iri_resolver: IriResolver +) -> SerialiseProperty: + for val in prop.values: + match val.value: + case str(): + return _transform_into_serialise_prop(prop, permissions_lookup, SerialiseSimpletext) + case FormattedTextValue(): + return _transform_into_serialise_richtext(prop, permissions_lookup, iri_resolver) + case _: + assert_never(val) + + +def _transform_into_serialise_richtext( + prop: XMLProperty, permissions_lookup: dict[str, Permissions], iri_resolver: IriResolver +) -> SerialiseProperty: + values: list[SerialiseValue] = [] + for val in prop.values: + xml_val = cast(FormattedTextValue, val.value) + xml_with_iris = xml_val.with_iris(iri_resolver) + val_str = xml_with_iris.as_xml() + permission_str = _get_permission_str(val.permissions, permissions_lookup) + values.append(SerialiseRichtext(value=val_str, permissions=permission_str, comment=val.comment)) + return SerialiseProperty(property_name=prop.name, values=values) def _transform_into_serialise_prop( From 99467473a20c55b09b761eb2efe2fbe010bad8c3 Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:54:53 +0100 Subject: [PATCH 07/11] fix linting --- .../xmlupload/resource_create_client.py | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/dsp_tools/commands/xmlupload/resource_create_client.py b/src/dsp_tools/commands/xmlupload/resource_create_client.py index e62b7256ba..26fb58243d 100644 --- a/src/dsp_tools/commands/xmlupload/resource_create_client.py +++ b/src/dsp_tools/commands/xmlupload/resource_create_client.py @@ -439,27 +439,26 @@ def _make_list_value(value: XMLValue, iri_lookup: dict[str, str]) -> dict[str, A def _transform_text_prop( prop: XMLProperty, permissions_lookup: dict[str, Permissions], iri_resolver: IriResolver ) -> SerialiseProperty: + values: list[SerialiseValue] = [] for val in prop.values: match val.value: case str(): - return _transform_into_serialise_prop(prop, permissions_lookup, SerialiseSimpletext) + values.append(_transform_into_serialise_value(val, permissions_lookup, SerialiseSimpletext)) case FormattedTextValue(): - return _transform_into_serialise_richtext(prop, permissions_lookup, iri_resolver) + values.append(_transform_into_serialise_richtext(val, permissions_lookup, iri_resolver)) case _: - assert_never(val) + assert_never(val.value) + return SerialiseProperty(property_name=prop.name, values=values) def _transform_into_serialise_richtext( - prop: XMLProperty, permissions_lookup: dict[str, Permissions], iri_resolver: IriResolver -) -> SerialiseProperty: - values: list[SerialiseValue] = [] - for val in prop.values: - xml_val = cast(FormattedTextValue, val.value) - xml_with_iris = xml_val.with_iris(iri_resolver) - val_str = xml_with_iris.as_xml() - permission_str = _get_permission_str(val.permissions, permissions_lookup) - values.append(SerialiseRichtext(value=val_str, permissions=permission_str, comment=val.comment)) - return SerialiseProperty(property_name=prop.name, values=values) + val: XMLValue, permissions_lookup: dict[str, Permissions], iri_resolver: IriResolver +) -> SerialiseRichtext: + xml_val = cast(FormattedTextValue, val.value) + xml_with_iris = xml_val.with_iris(iri_resolver) + val_str = xml_with_iris.as_xml() + permission_str = _get_permission_str(val.permissions, permissions_lookup) + return SerialiseRichtext(value=val_str, permissions=permission_str, comment=val.comment) def _transform_into_serialise_prop( From f2bd71f5729a87fb84baaf5c1f561b7e48bbf451 Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:57:51 +0100 Subject: [PATCH 08/11] Update resource_create_client.py --- .../xmlupload/resource_create_client.py | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/dsp_tools/commands/xmlupload/resource_create_client.py b/src/dsp_tools/commands/xmlupload/resource_create_client.py index 26fb58243d..2791000dfc 100644 --- a/src/dsp_tools/commands/xmlupload/resource_create_client.py +++ b/src/dsp_tools/commands/xmlupload/resource_create_client.py @@ -159,10 +159,14 @@ def make_values(p: XMLProperty) -> list[dict[str, Any]]: ) properties_serialised.update(transformed_prop.serialise()) case "decimal": - transformed_prop = _transform_decimal_prop(prop=prop, permissions_lookup=self.permissions_lookup) + transformed_prop = _transform_into_decimal_prop( + prop=prop, permissions_lookup=self.permissions_lookup + ) properties_serialised.update(transformed_prop.serialise()) case "geometry": - transformed_prop = _transform_geometry_prop(prop=prop, permissions_lookup=self.permissions_lookup) + transformed_prop = _transform_into_geometry_prop( + prop=prop, permissions_lookup=self.permissions_lookup + ) properties_serialised.update(transformed_prop.serialise()) case "text": transformed_prop = _transform_text_prop( @@ -310,24 +314,24 @@ def _make_date_value(value: XMLValue) -> dict[str, Any]: return res -def _transform_decimal_prop(prop: XMLProperty, permissions_lookup: dict[str, Permissions]) -> SerialiseProperty: - vals: list[SerialiseValue] = [_transform_into_serialise_decimal(v, permissions_lookup) for v in prop.values] +def _transform_into_decimal_prop(prop: XMLProperty, permissions_lookup: dict[str, Permissions]) -> SerialiseProperty: + vals: list[SerialiseValue] = [_transform_into_decimal_value(v, permissions_lookup) for v in prop.values] return SerialiseProperty(property_name=prop.name, values=vals) -def _transform_into_serialise_decimal(value: XMLValue, permissions_lookup: dict[str, Permissions]) -> SerialiseDecimal: +def _transform_into_decimal_value(value: XMLValue, permissions_lookup: dict[str, Permissions]) -> SerialiseDecimal: s = _assert_is_string(value.value) val = str(float(s)) permission_str = _get_permission_str(value.permissions, permissions_lookup) return SerialiseDecimal(value=val, permissions=permission_str, comment=value.comment) -def _transform_geometry_prop(prop: XMLProperty, permissions_lookup: dict[str, Permissions]) -> SerialiseProperty: - vals: list[SerialiseValue] = [_make_geometry_value(v, permissions_lookup) for v in prop.values] +def _transform_into_geometry_prop(prop: XMLProperty, permissions_lookup: dict[str, Permissions]) -> SerialiseProperty: + vals: list[SerialiseValue] = [_transform_into_geometry_value(v, permissions_lookup) for v in prop.values] return SerialiseProperty(property_name=prop.name, values=vals) -def _make_geometry_value(value: XMLValue, permissions_lookup: dict[str, Permissions]) -> SerialiseGeometry: +def _transform_into_geometry_value(value: XMLValue, permissions_lookup: dict[str, Permissions]) -> SerialiseGeometry: s = _assert_is_string(value.value) # this removes all whitespaces from the embedded json string encoded_value = json.dumps(json.loads(s)) @@ -445,13 +449,13 @@ def _transform_text_prop( case str(): values.append(_transform_into_serialise_value(val, permissions_lookup, SerialiseSimpletext)) case FormattedTextValue(): - values.append(_transform_into_serialise_richtext(val, permissions_lookup, iri_resolver)) + values.append(_transform_into_richtext_value(val, permissions_lookup, iri_resolver)) case _: assert_never(val.value) return SerialiseProperty(property_name=prop.name, values=values) -def _transform_into_serialise_richtext( +def _transform_into_richtext_value( val: XMLValue, permissions_lookup: dict[str, Permissions], iri_resolver: IriResolver ) -> SerialiseRichtext: xml_val = cast(FormattedTextValue, val.value) From 2ffbcc0f1315397732a6e8200204d89cae6bf7d2 Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum Date: Thu, 14 Nov 2024 16:14:45 +0100 Subject: [PATCH 09/11] don't repeat inherited fields --- .../models/serialise/serialise_value.py | 41 +------------------ 1 file changed, 1 insertion(+), 40 deletions(-) diff --git a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py index d23fa4d105..57b12ea128 100644 --- a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py +++ b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py @@ -16,6 +16,7 @@ def serialise(self) -> dict[str, Any]: return {self.property_name: [value.serialise() for value in self.values]} +@dataclass(frozen=True) class SerialiseValue(ABC): """A value to be serialised.""" @@ -34,14 +35,9 @@ def _get_optionals(self) -> dict[str, str]: return optionals -@dataclass(frozen=True) class SerialiseColor(SerialiseValue): """A ColorValue to be serialised.""" - value: str - permissions: str | None - comment: str | None - def serialise(self) -> dict[str, Any]: serialised = { "@type": "knora-api:ColorValue", @@ -51,14 +47,9 @@ def serialise(self) -> dict[str, Any]: return serialised -@dataclass(frozen=True) class SerialiseDecimal(SerialiseValue): """A to be serialised.""" - value: str - permissions: str | None - comment: str | None - def serialise(self) -> dict[str, Any]: serialised = { "@type": "knora-api:DecimalValue", @@ -71,14 +62,9 @@ def serialise(self) -> dict[str, Any]: return serialised -@dataclass(frozen=True) class SerialiseGeometry(SerialiseValue): """A GeomValue to be serialised.""" - value: str - permissions: str | None - comment: str | None - def serialise(self) -> dict[str, Any]: serialised = { "@type": "knora-api:GeomValue", @@ -88,14 +74,9 @@ def serialise(self) -> dict[str, Any]: return serialised -@dataclass(frozen=True) class SerialiseGeoname(SerialiseValue): """A GeonameValue to be serialised.""" - value: str - permissions: str | None - comment: str | None - def serialise(self) -> dict[str, Any]: serialised = { "@type": "knora-api:GeonameValue", @@ -105,14 +86,9 @@ def serialise(self) -> dict[str, Any]: return serialised -@dataclass(frozen=True) class SerialiseSimpletext(SerialiseValue): """A Simpletext to be serialised.""" - value: str - permissions: str | None - comment: str | None - def serialise(self) -> dict[str, Any]: serialised = { "@type": "knora-api:TextValue", @@ -122,14 +98,9 @@ def serialise(self) -> dict[str, Any]: return serialised -@dataclass(frozen=True) class SerialiseRichtext(SerialiseValue): """A Richtext to be serialised.""" - value: str - permissions: str | None - comment: str | None - def serialise(self) -> dict[str, Any]: serialised = { "@type": "knora-api:TextValue", @@ -142,14 +113,9 @@ def serialise(self) -> dict[str, Any]: return serialised -@dataclass(frozen=True) class SerialiseTime(SerialiseValue): """A TimeValue to be serialised.""" - value: str - permissions: str | None - comment: str | None - def serialise(self) -> dict[str, Any]: serialised = { "@type": "knora-api:TimeValue", @@ -162,14 +128,9 @@ def serialise(self) -> dict[str, Any]: return serialised -@dataclass(frozen=True) class SerialiseURI(SerialiseValue): """A UriValue to be serialised.""" - value: str - permissions: str | None - comment: str | None - def serialise(self) -> dict[str, Any]: serialised = { "@type": "knora-api:UriValue", From d0a17ed9f1d080547f4a562f041a17bc12d09413 Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:05:49 +0100 Subject: [PATCH 10/11] Update src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py Co-authored-by: Johannes Nussbaum <39048939+jnussbaum@users.noreply.github.com> --- .../commands/xmlupload/models/serialise/serialise_value.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py index 57b12ea128..dcb39b0cda 100644 --- a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py +++ b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py @@ -48,7 +48,7 @@ def serialise(self) -> dict[str, Any]: class SerialiseDecimal(SerialiseValue): - """A to be serialised.""" + """A DecimalValue to be serialised.""" def serialise(self) -> dict[str, Any]: serialised = { From 7baa831d486e09435a192a64d77535436a8faa63 Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Thu, 14 Nov 2024 17:13:40 +0100 Subject: [PATCH 11/11] change sequence --- .../commands/xmlupload/models/serialise/serialise_value.py | 3 ++- src/dsp_tools/commands/xmlupload/resource_create_client.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py index 57b12ea128..43bb6bd945 100644 --- a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py +++ b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py @@ -2,6 +2,7 @@ from abc import ABC from abc import abstractmethod +from collections.abc import Sequence from dataclasses import dataclass from typing import Any @@ -9,7 +10,7 @@ @dataclass(frozen=True) class SerialiseProperty: property_name: str - values: list[SerialiseValue] + values: Sequence[SerialiseValue] def serialise(self) -> dict[str, Any]: """Serialise the property and all its values.""" diff --git a/src/dsp_tools/commands/xmlupload/resource_create_client.py b/src/dsp_tools/commands/xmlupload/resource_create_client.py index 2791000dfc..ea24cf0e6b 100644 --- a/src/dsp_tools/commands/xmlupload/resource_create_client.py +++ b/src/dsp_tools/commands/xmlupload/resource_create_client.py @@ -315,7 +315,7 @@ def _make_date_value(value: XMLValue) -> dict[str, Any]: def _transform_into_decimal_prop(prop: XMLProperty, permissions_lookup: dict[str, Permissions]) -> SerialiseProperty: - vals: list[SerialiseValue] = [_transform_into_decimal_value(v, permissions_lookup) for v in prop.values] + vals = [_transform_into_decimal_value(v, permissions_lookup) for v in prop.values] return SerialiseProperty(property_name=prop.name, values=vals) @@ -327,7 +327,7 @@ def _transform_into_decimal_value(value: XMLValue, permissions_lookup: dict[str, def _transform_into_geometry_prop(prop: XMLProperty, permissions_lookup: dict[str, Permissions]) -> SerialiseProperty: - vals: list[SerialiseValue] = [_transform_into_geometry_value(v, permissions_lookup) for v in prop.values] + vals = [_transform_into_geometry_value(v, permissions_lookup) for v in prop.values] return SerialiseProperty(property_name=prop.name, values=vals) @@ -443,7 +443,7 @@ def _make_list_value(value: XMLValue, iri_lookup: dict[str, str]) -> dict[str, A def _transform_text_prop( prop: XMLProperty, permissions_lookup: dict[str, Permissions], iri_resolver: IriResolver ) -> SerialiseProperty: - values: list[SerialiseValue] = [] + values = [] for val in prop.values: match val.value: case str():