Skip to content

Commit

Permalink
feat(xmlupload): id2iri mapping contains servername (DEV-1889) (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnussbaum committed Nov 10, 2023
1 parent afb16b1 commit 19a3f29
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/dsp_tools/utils/xmlupload/write_diagnostic_info.py
Expand Up @@ -8,21 +8,24 @@
from lxml import etree

from dsp_tools.utils.create_logger import get_logger
from dsp_tools.utils.xmlupload.upload_config import DiagnosticsConfig

logger = get_logger(__name__)


def write_id2iri_mapping(
id2iri_mapping: dict[str, str],
input_file: str | Path | etree._ElementTree[Any],
timestamp_str: str,
diagnostics: DiagnosticsConfig,
) -> None:
"""Writes the mapping of internal IDs to IRIs to a file."""
timestamp = diagnostics.timestamp_str
servername = diagnostics.server_as_foldername
match input_file:
case str() | Path():
id2iri_filename = f"{Path(input_file).stem}_id2iri_mapping_{timestamp_str}.json"
id2iri_filename = f"{Path(input_file).stem}_id2iri_mapping_{servername}_{timestamp}.json"
case _:
id2iri_filename = f"{timestamp_str}_id2iri_mapping.json"
id2iri_filename = f"{timestamp}_id2iri_mapping_{servername}.json"
with open(id2iri_filename, "x", encoding="utf-8") as f:
json.dump(id2iri_mapping, f, ensure_ascii=False, indent=4)
print(f"{datetime.now()}: The mapping of internal IDs to IRIs was written to {id2iri_filename}")
Expand Down
13 changes: 9 additions & 4 deletions src/dsp_tools/utils/xmlupload/xmlupload.py
Expand Up @@ -108,7 +108,7 @@ def xmlupload(
list_client=list_client,
)

write_id2iri_mapping(iri_resolver.lookup, input_file, config.diagnostics.timestamp_str)
write_id2iri_mapping(iri_resolver.lookup, input_file, config.diagnostics)
success = not failed_uploads
if success:
print(f"{datetime.now()}: All resources have successfully been uploaded.")
Expand Down Expand Up @@ -412,8 +412,11 @@ def _handle_upload_error(
)
logger.error("xmlupload must be aborted because of an error", exc_info=err)

timestamp = diagnostics.timestamp_str
servername = diagnostics.server_as_foldername

if iri_resolver.non_empty():
id2iri_mapping_file = f"{diagnostics.save_location}/{diagnostics.timestamp_str}_id2iri_mapping.json"
id2iri_mapping_file = f"{diagnostics.save_location}/{timestamp}_id2iri_mapping_{servername}.json"
with open(id2iri_mapping_file, "x", encoding="utf-8") as f:
json.dump(iri_resolver.lookup, f, ensure_ascii=False, indent=4)
print(f"The mapping of internal IDs to IRIs was written to {id2iri_mapping_file}")
Expand All @@ -423,7 +426,8 @@ def _handle_upload_error(
filename = _save_stash_as_json(
stash=stash,
save_location=diagnostics.save_location,
timestamp_str=diagnostics.timestamp_str,
timestamp_str=timestamp,
servername=servername,
)
msg = (
f"There are stashed links that could not be reapplied to the resources they were stripped from. "
Expand All @@ -445,8 +449,9 @@ def _save_stash_as_json(
stash: Stash,
save_location: Path,
timestamp_str: str,
servername: str,
) -> str:
filename = f"{save_location}/{timestamp_str}_stashed_links.json"
filename = f"{save_location}/{timestamp_str}_stashed_links_{servername}.json"
with open(filename, "x", encoding="utf-8") as file:
json.dump(
obj=asdict(stash),
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/test_fast_xmlupload.py
Expand Up @@ -61,7 +61,7 @@ def tearDown(self) -> None:
for pickle_file in list(Path().glob("*.pkl")):
pickle_file.unlink()

for id2iri_file in list(Path().glob("*id2iri_mapping.json")):
for id2iri_file in list(Path().glob("*id2iri_mapping*.json")):
id2iri_file.unlink()

for txt_file in self.txt_files:
Expand Down

0 comments on commit 19a3f29

Please sign in to comment.