Skip to content

Commit df0bf33

Browse files
authored
chore(xmlupload): improve error message when syntax for referencing ontos is wrong (DEV-1399) (#237)
1 parent 2ed4a70 commit df0bf33

3 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎knora/dsplib/models/helpers.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def __str__(self) -> str:
6363
Convert to string
6464
:return: stringyfied error message
6565
"""
66-
return "ERROR: " + self._message + "!\n\n" + format_exc()
66+
return self._message + "\n\n" + format_exc()
6767

6868
@property
6969
def message(self) -> str:

‎knora/dsplib/utils/xml_upload.py‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,8 @@ def xml_upload(input_file: str, server: str, user: str, password: str, imgdir: s
252252
# Connect to the DaSCH Service Platform API and get the project context
253253
con = Connection(server)
254254
try_network_action(failure_msg="Unable to login to DSP server", action=lambda: con.login(user, password))
255-
proj_context = ProjectContext(con=con)
255+
proj_context = try_network_action(failure_msg="Unable to retrieve project context from DSP server",
256+
action=lambda: ProjectContext(con=con))
256257
sipi_server = Sipi(sipi, con.get_token())
257258

258259
tree = _parse_xml_file(input_file)
@@ -274,6 +275,32 @@ def xml_upload(input_file: str, server: str, user: str, password: str, imgdir: s
274275
permissions_lookup: dict[str, Permissions] = {s: perm.get_permission_instance() for s, perm in permissions.items()}
275276
resclass_name_2_type: dict[str, type] = {s: res_inst_factory.get_resclass_type(s) for s in res_inst_factory.get_resclass_names()}
276277

278+
# check if the data in the XML is consistent with the ontology
279+
if verbose:
280+
print("Check if the resource types and properties in your XML are consistent with the ontology...")
281+
knora_properties = resclass_name_2_type[resources[0].restype].knora_properties
282+
for resource in resources:
283+
if resource.restype not in resclass_name_2_type:
284+
print(f"=========================\n"
285+
f"ERROR: Resource '{resource.label}' (ID: {resource.id}) has an invalid resource type "
286+
f"'{resource.restype}'. Is your syntax correct? Remember the rules:\n"
287+
f" - DSP-API internals: <resource restype=\"restype\"> (will be interpreted as 'knora-api:restype')\n"
288+
f" - current ontology: <resource restype=\":restype\"> ('restype' must be defined in the 'resources' section of your ontology)\n"
289+
f" - other ontology: <resource restype=\"other:restype\"> (not yet implemented: 'other' must be defined in the same JSON project file than your ontology)")
290+
exit(1)
291+
resource_properties = resclass_name_2_type[resource.restype].properties.keys()
292+
for propname in [prop.name for prop in resource.properties]:
293+
if propname not in knora_properties and propname not in resource_properties:
294+
print(f"=========================\n"
295+
f"ERROR: Resource '{resource.label}' (ID: {resource.id}) has an invalid property '{propname}'. "
296+
f"Is your syntax correct? Remember the rules:\n"
297+
f" - DSP-API internals: <text-prop name=\"propname\"> (will be interpreted as 'knora-api:propname')\n"
298+
f" - current ontology: <text-prop name=\":propname\"> ('propname' must be defined in the 'properties' section of your ontology)\n"
299+
f" - other ontology: <text-prop name=\"other:propname\"> (not yet implemented: 'other' must be defined in the same JSON project file than your ontology)")
300+
exit(1)
301+
302+
print("The resource types and properties in your XML are consistent with the ontology.")
303+
277304
# temporarily remove circular references, but only if not an incremental upload
278305
if not incremental:
279306
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
318345
_write_stashed_resptr_props(nonapplied_resptr_props, timestamp_str)
319346
success = False
320347
if failed_uploads:
321-
print(f"Could not upload the following resources: {failed_uploads}")
348+
print(f"\nWARNING: Could not upload the following resources: {failed_uploads}\n")
322349
success = False
323350
if success:
324351
print("All resources have successfully been uploaded.")

‎testdata/test-data-systematic.xml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,4 +743,13 @@
743743
<interval permissions="prop-default">8.0:12.0</interval>
744744
</interval-prop>
745745
</resource>
746+
747+
<!-- reference a resource class from another ontology defined in the same JSON project file than default-ontology-->
748+
<!-- Will work when DEV-1381 is resolved
749+
<resource label="TestThingPermissions 1" restype="testontoPermissions:TestThingPermissions" id="testthingpermissions_1">
750+
<text-prop name="testontoPermissions:hasText">
751+
<text encoding="utf8">Hello!</text>
752+
</text-prop>
753+
</resource>
754+
-->
746755
</knora>

0 commit comments

Comments
 (0)