From df0bf33588ebae38d09756c73198d33a9ebaf867 Mon Sep 17 00:00:00 2001 From: Johannes Nussbaum <39048939+jnussbaum@users.noreply.github.com> Date: Mon, 10 Oct 2022 10:14:11 +0200 Subject: [PATCH] chore(xmlupload): improve error message when syntax for referencing ontos is wrong (DEV-1399) (#237) --- knora/dsplib/models/helpers.py | 2 +- knora/dsplib/utils/xml_upload.py | 31 +++++++++++++++++++++++++++++-- testdata/test-data-systematic.xml | 9 +++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/knora/dsplib/models/helpers.py b/knora/dsplib/models/helpers.py index 94f1ab002..14c7fc16e 100644 --- a/knora/dsplib/models/helpers.py +++ b/knora/dsplib/models/helpers.py @@ -63,7 +63,7 @@ def __str__(self) -> str: Convert to string :return: stringyfied error message """ - return "ERROR: " + self._message + "!\n\n" + format_exc() + return self._message + "\n\n" + format_exc() @property def message(self) -> str: diff --git a/knora/dsplib/utils/xml_upload.py b/knora/dsplib/utils/xml_upload.py index 4a5246b4b..29bb4bfd6 100644 --- a/knora/dsplib/utils/xml_upload.py +++ b/knora/dsplib/utils/xml_upload.py @@ -252,7 +252,8 @@ def xml_upload(input_file: str, server: str, user: str, password: str, imgdir: s # Connect to the DaSCH Service Platform API and get the project context con = Connection(server) try_network_action(failure_msg="Unable to login to DSP server", action=lambda: con.login(user, password)) - proj_context = ProjectContext(con=con) + proj_context = try_network_action(failure_msg="Unable to retrieve project context from DSP server", + action=lambda: ProjectContext(con=con)) sipi_server = Sipi(sipi, con.get_token()) tree = _parse_xml_file(input_file) @@ -274,6 +275,32 @@ def xml_upload(input_file: str, server: str, user: str, password: str, imgdir: s permissions_lookup: dict[str, Permissions] = {s: perm.get_permission_instance() for s, perm in permissions.items()} resclass_name_2_type: dict[str, type] = {s: res_inst_factory.get_resclass_type(s) for s in res_inst_factory.get_resclass_names()} + # check if the data in the XML is consistent with the ontology + if verbose: + print("Check if the resource types and properties in your XML are consistent with the ontology...") + knora_properties = resclass_name_2_type[resources[0].restype].knora_properties + for resource in resources: + if resource.restype not in resclass_name_2_type: + print(f"=========================\n" + f"ERROR: Resource '{resource.label}' (ID: {resource.id}) has an invalid resource type " + f"'{resource.restype}'. Is your syntax correct? Remember the rules:\n" + f" - DSP-API internals: (will be interpreted as 'knora-api:restype')\n" + f" - current ontology: ('restype' must be defined in the 'resources' section of your ontology)\n" + f" - other ontology: (not yet implemented: 'other' must be defined in the same JSON project file than your ontology)") + exit(1) + resource_properties = resclass_name_2_type[resource.restype].properties.keys() + for propname in [prop.name for prop in resource.properties]: + if propname not in knora_properties and propname not in resource_properties: + print(f"=========================\n" + f"ERROR: Resource '{resource.label}' (ID: {resource.id}) has an invalid property '{propname}'. " + f"Is your syntax correct? Remember the rules:\n" + f" - DSP-API internals: (will be interpreted as 'knora-api:propname')\n" + f" - current ontology: ('propname' must be defined in the 'properties' section of your ontology)\n" + f" - other ontology: (not yet implemented: 'other' must be defined in the same JSON project file than your ontology)") + exit(1) + + print("The resource types and properties in your XML are consistent with the ontology.") + # temporarily remove circular references, but only if not an incremental upload if not incremental: resources, stashed_xml_texts, stashed_resptr_props = _remove_circular_references(resources, verbose) @@ -318,7 +345,7 @@ def xml_upload(input_file: str, server: str, user: str, password: str, imgdir: s _write_stashed_resptr_props(nonapplied_resptr_props, timestamp_str) success = False if failed_uploads: - print(f"Could not upload the following resources: {failed_uploads}") + print(f"\nWARNING: Could not upload the following resources: {failed_uploads}\n") success = False if success: print("All resources have successfully been uploaded.") diff --git a/testdata/test-data-systematic.xml b/testdata/test-data-systematic.xml index 5d02eab25..be00d50f7 100644 --- a/testdata/test-data-systematic.xml +++ b/testdata/test-data-systematic.xml @@ -743,4 +743,13 @@ 8.0:12.0 + + +