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

chore(xmlupload): improve error message when syntax for referencing ontos is wrong (DEV-1399) #237

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
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 onto: <resource restype=\":restype\"> ('restype' must be defined in the 'resources' section of your ontology)\n"
jnussbaum marked this conversation as resolved.
Show resolved Hide resolved
f" - other onto: <resource restype=\"other:restype\"> (not yet implemented: 'other' must be defined in the same JSON project file than your ontology)")
jnussbaum marked this conversation as resolved.
Show resolved Hide resolved
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 onto: <text-prop name=\":propname\"> ('propname' must be defined in the 'properties' section of your ontology)\n"
f" - other onto: <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>