Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: create JSON-LD context without API request (DEV-2845) #581

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/dsp_tools/utils/xmlupload/upload_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,18 @@ class UploadConfig:
server_as_foldername: str = field(default="unknown")
save_location: Path = field(default=Path.home() / ".dsp-tools" / "xmluploads")
timestamp_str: str = field(default=datetime.now().strftime("%Y-%m-%d_%H%M%S"))
json_ld_context: dict[str, str] = field(
default_factory=lambda: {
"knora-api": "http://api.knora.org/ontology/knora-api/v2#",
"salsah-gui": "http://api.knora.org/ontology/salsah-gui/v2#",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"owl": "http://www.w3.org/2002/07/owl#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
}
)

def with_specific_save_location(
def with_server_info(
self,
server: str,
shortcode: str,
Expand All @@ -56,8 +66,10 @@ def with_specific_save_location(
save_location = Path.home() / Path(".dsp-tools") / "xmluploads" / server_as_foldername / shortcode / onto_name
save_location.mkdir(parents=True, exist_ok=True)
logger.info(f"save_location='{save_location}'")
context_iri = f"{server}/ontology/{shortcode}/{onto_name}/v2#"
return dataclasses.replace(
self,
save_location=save_location,
server_as_foldername=server_as_foldername,
json_ld_context=self.json_ld_context | {onto_name: context_iri},
)
22 changes: 2 additions & 20 deletions src/dsp_tools/utils/xmlupload/upload_stashed_resptr_props.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import json
from urllib.parse import quote_plus

from dsp_tools.connection.connection import Connection
from dsp_tools.models.exceptions import BaseError
Expand All @@ -17,6 +16,7 @@ def upload_stashed_resptr_props(
id2iri_mapping: dict[str, str],
con: Connection,
stashed_resptr_props: LinkValueStash,
context: dict[str, str],
) -> LinkValueStash | None:
"""
After all resources are uploaded, the stashed resptr props must be applied to their resources in DSP.
Expand All @@ -26,6 +26,7 @@ def upload_stashed_resptr_props(
id2iri_mapping: mapping of ids from the XML file to IRIs in DSP
con: connection to DSP
stashed_resptr_props: all resptr props that have been stashed
context: the JSON-LD context of the resource

Returns:
nonapplied_resptr_props: the resptr props that could not be uploaded
Expand All @@ -41,15 +42,9 @@ def upload_stashed_resptr_props(
# which will be handled by the caller
continue
res_iri = id2iri_mapping[res_id]
try:
existing_resource = try_network_action(con.get, route=f"/v2/resources/{quote_plus(res_iri)}")
except BaseError as err:
_log_if_unable_to_retrieve_resource(err, res_id)
continue
if verbose:
print(f' Upload resptrs of resource "{res_id}"...')
logger.debug(f' Upload resptrs of resource "{res_id}"...')
context: dict[str, str] = existing_resource["@context"]
for stash_item in stash_items:
target_iri = id2iri_mapping.get(stash_item.target_id)
if not target_iri:
Expand Down Expand Up @@ -96,19 +91,6 @@ def _log_unable_to_upload_link_value(msg: str, res_id: str, prop_name: str) -> N
logger.warning(err_msg, exc_info=True)


def _log_if_unable_to_retrieve_resource(
err: BaseError,
resource_id: str,
) -> None:
orig_err_msg = err.orig_err_msg_from_api or err.message
err_msg = (
f"Unable to upload resptrs of resource '{resource_id}', "
"because the resource cannot be retrieved from the DSP server."
)
print(f" WARNING: {err_msg} Original error message: {orig_err_msg}")
logger.warning(err_msg, exc_info=True)


def _create_resptr_prop_json_object_to_update(
stash: LinkValueStashItem,
res_iri: str,
Expand Down
16 changes: 14 additions & 2 deletions src/dsp_tools/utils/xmlupload/xmlupload.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def xmlupload(
preprocessing_done=config.preprocessing_done,
)

config = config.with_specific_save_location(
config = config.with_server_info(
server=server,
shortcode=shortcode,
onto_name=default_ontology,
Expand Down Expand Up @@ -152,7 +152,17 @@ def _upload(
con=con,
preprocessing_done=config.preprocessing_done,
)
nonapplied_stash = _upload_stash(stash, id2iri_mapping, con, config.verbose) if stash else None
nonapplied_stash = (
_upload_stash(
stash=stash,
id2iri_mapping=id2iri_mapping,
con=con,
context=config.json_ld_context,
verbose=config.verbose,
)
if stash
else None
)
if nonapplied_stash:
msg = "Some stashed resptrs or XML texts could not be reapplied to their resources on the DSP server."
logger.error(msg)
Expand Down Expand Up @@ -201,6 +211,7 @@ def _upload_stash(
stash: Stash,
id2iri_mapping: dict[str, str],
con: Connection,
context: dict[str, str],
verbose: bool,
) -> Stash | None:
if stash.standoff_stash:
Expand All @@ -218,6 +229,7 @@ def _upload_stash(
id2iri_mapping=id2iri_mapping,
con=con,
stashed_resptr_props=stash.link_value_stash,
context=context,
)
else:
nonapplied_resptr_props = None
Expand Down
2 changes: 1 addition & 1 deletion test/unittests/test_xmlupload/test_upload_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_save_location() -> None:
onto_name = "testonto"
expected_path = f"/.dsp-tools/xmluploads/{server}/{shortcode}/{onto_name}"
config = UploadConfig()
config_with_save_location = config.with_specific_save_location(
config_with_save_location = config.with_server_info(
server=server,
shortcode=shortcode,
onto_name=onto_name,
Expand Down
8 changes: 4 additions & 4 deletions test/unittests/test_xmlupload/test_upload_stash_with_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,12 @@ def test_upload_link_value_stash(self) -> None:
"001": "http://www.rdfh.ch/0001/001",
"002": "http://www.rdfh.ch/0001/002",
}
con: Connection = ConnectionMock(
get_responses=[{"@context": {}}],
post_responses=[{}],
)
con: Connection = ConnectionMock(post_responses=[{}])
nonapplied = _upload_stash(
stash=stash,
id2iri_mapping=id2iri_mapping,
con=con,
context={},
verbose=False,
)
assert nonapplied is None
Expand Down Expand Up @@ -135,6 +133,7 @@ def test_upload_text_value_stash(self) -> None:
stash=stash,
id2iri_mapping=id2iri_mapping,
con=con,
context={},
verbose=False,
)
assert nonapplied is None
Expand Down Expand Up @@ -179,6 +178,7 @@ def test_not_upload_text_value_stash_if_uuid_not_on_value(self) -> None:
stash=stash,
id2iri_mapping=id2iri_mapping,
con=con,
context={},
verbose=False,
)
assert nonapplied == stash