Skip to content

Commit

Permalink
chore(xmlupload): improve error message when syntax for referencing o…
Browse files Browse the repository at this point in the history
…ntos is wrong (DEV-1399) (#237)
  • Loading branch information
jnussbaum committed Oct 10, 2022
1 parent 2ed4a70 commit df0bf33
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion knora/dsplib/models/helpers.py
Expand Up @@ -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:
Expand Down
31 changes: 29 additions & 2 deletions knora/dsplib/utils/xml_upload.py
Expand Up @@ -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)
Expand All @@ -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: <resource restype=\"restype\"> (will be interpreted as 'knora-api:restype')\n"
f" - current ontology: <resource restype=\":restype\"> ('restype' must be defined in the 'resources' section of your ontology)\n"
f" - other ontology: <resource restype=\"other:restype\"> (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: <text-prop name=\"propname\"> (will be interpreted as 'knora-api:propname')\n"
f" - current ontology: <text-prop name=\":propname\"> ('propname' must be defined in the 'properties' section of your ontology)\n"
f" - other ontology: <text-prop name=\"other:propname\"> (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)
Expand Down Expand Up @@ -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.")
Expand Down
9 changes: 9 additions & 0 deletions testdata/test-data-systematic.xml
Expand Up @@ -743,4 +743,13 @@
<interval permissions="prop-default">8.0:12.0</interval>
</interval-prop>
</resource>

<!-- reference a resource class from another ontology defined in the same JSON project file than default-ontology-->
<!-- Will work when DEV-1381 is resolved
<resource label="TestThingPermissions 1" restype="testontoPermissions:TestThingPermissions" id="testthingpermissions_1">
<text-prop name="testontoPermissions:hasText">
<text encoding="utf8">Hello!</text>
</text-prop>
</resource>
-->
</knora>

0 comments on commit df0bf33

Please sign in to comment.