Skip to content

Commit

Permalink
refactor: xmlupload (#852)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Mar 4, 2024
1 parent 79a643a commit c8e6284
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/dsp_tools/commands/xmlupload/resource_create_client.py
Expand Up @@ -28,7 +28,7 @@ class ResourceCreateClient:

con: Connection
project_iri: str
id_to_iri_resolver: IriResolver
iri_resolver: IriResolver
json_ld_context: dict[str, str]
permissions_lookup: dict[str, Permissions]
listnode_lookup: dict[str, str]
Expand Down Expand Up @@ -123,11 +123,11 @@ def _make_value(self, value: XMLValue, value_type: str) -> dict[str, Any]:
case "interval":
res = _make_interval_value(value)
case "resptr":
res = _make_link_value(value, self.id_to_iri_resolver)
res = _make_link_value(value, self.iri_resolver)
case "list":
res = _make_list_value(value, self.listnode_lookup)
case "text":
res = _make_text_value(value, self.id_to_iri_resolver)
res = _make_text_value(value, self.iri_resolver)
case "time":
res = _make_time_value(value)
case "uri":
Expand Down
18 changes: 8 additions & 10 deletions src/dsp_tools/commands/xmlupload/xmlupload.py
Expand Up @@ -172,7 +172,6 @@ def _upload(
project_client: ProjectClient,
list_client: ListClient,
) -> tuple[IriResolver, list[str]]:
# upload all resources, then update the resources with the stashed XML texts and resptrs
failed_uploads: list[str] = []
iri_resolver = IriResolver()
try:
Expand All @@ -185,7 +184,7 @@ def _upload(
config=config,
project_client=project_client,
list_client=list_client,
id_to_iri_resolver=iri_resolver,
iri_resolver=iri_resolver,
)
nonapplied_stash = (
_upload_stash(
Expand All @@ -202,9 +201,8 @@ def _upload(
msg = "Some stashed resptrs or XML texts could not be reapplied to their resources on the DSP server."
raise BaseError(msg)
except BaseException as err: # noqa: BLE001 (blind-except)
# The forseeable errors are already handled by the variables
# failed_uploads, nonapplied_xml_texts, and nonapplied_resptr_props.
# Here we catch the unforseeable exceptions, hence BaseException (=the base class of all exceptions)
# The forseeable errors are already handled by failed_uploads and nonapplied_stash.
# Here we catch the unforseeable exceptions, incl. Ctrl+C.
_handle_upload_error(
err_msg=str(err),
iri_resolver=iri_resolver,
Expand Down Expand Up @@ -301,7 +299,7 @@ def _upload_resources(
config: UploadConfig,
project_client: ProjectClient,
list_client: ListClient,
id_to_iri_resolver: IriResolver,
iri_resolver: IriResolver,
) -> tuple[IriResolver, list[str]]:
"""
Iterates through all resources and tries to upload them to DSP.
Expand All @@ -317,7 +315,7 @@ def _upload_resources(
config: the upload configuration
project_client: a client for HTTP communication with the DSP-API
list_client: a client for HTTP communication with the DSP-API
id_to_iri_resolver: a resolver for internal IDs to IRIs
iri_resolver: mapping from internal IDs to IRIs
Returns:
id2iri_mapping, failed_uploads
Expand All @@ -331,7 +329,7 @@ def _upload_resources(
resource_create_client = ResourceCreateClient(
con=con,
project_iri=project_iri,
id_to_iri_resolver=id_to_iri_resolver,
iri_resolver=iri_resolver,
json_ld_context=json_ld_context,
permissions_lookup=permissions_lookup,
listnode_lookup=listnode_lookup,
Expand All @@ -352,13 +350,13 @@ def _upload_resources(
continue

iri, label = res
id_to_iri_resolver.update(resource.res_id, iri)
iri_resolver.update(resource.res_id, iri)

resource_designation = f"'{label}' (ID: '{resource.res_id}', IRI: '{iri}')"
print(f"{datetime.now()}: Created resource {i+1}/{len(resources)}: {resource_designation}")
logger.info(f"Created resource {i+1}/{len(resources)}: {resource_designation}")

return id_to_iri_resolver, failed_uploads
return iri_resolver, failed_uploads


def _create_resource(
Expand Down

0 comments on commit c8e6284

Please sign in to comment.