Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions site/cds_rdm/inspire_harvester/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@ def retrieve_identifiers(identifiers, scheme):
yield ident["identifier"]


def pids_equal(left, right):
"""Compare PIDs ignoring DataCite client on the DOI."""
left, right = dict(left), dict(right)
for pids in (left, right):
doi = pids.get("doi")
if doi:
pids["doi"] = {k: doi[k] for k in ("identifier", "provider") if k in doi}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop seems like an overkill decreasing readability just for 2 keys

return left == right


def _present_keys(value):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename this to be more readable, wdyt?

"""Keys that have stored content, not empty dump placeholders."""
return {k for k, v in value.items() if v not in (None, [], {})}


def compare_metadata(a, b):
"""Compare metadata based on id key only."""
# If both are dicts
Expand All @@ -30,18 +45,23 @@ def compare_metadata(a, b):
if "id" in a and "id" in b:
return a["id"] == b["id"]

keys_a, keys_b = _present_keys(a), _present_keys(b)

# Otherwise compare keys recursively
if a.keys() != b.keys():
if keys_a != keys_b:
return False

return all(compare_metadata(a[k], b[k]) for k in a)
return all(compare_metadata(a[k], b[k]) for k in keys_a)

# If both are lists
if isinstance(a, list) and isinstance(b, list):
if len(a) != len(b):
return False
return all(compare_metadata(x, y) for x, y in zip(a, b))

if isinstance(a, str) and isinstance(b, str):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add some comments for context

return a.casefold() == b.casefold()

# Fallback normal comparison
return a == b

Expand Down
4 changes: 2 additions & 2 deletions site/cds_rdm/inspire_harvester/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
UpdateEngine,
UpdateEngineConflict,
)
from cds_rdm.inspire_harvester.utils import compare_metadata
from cds_rdm.inspire_harvester.utils import compare_metadata, pids_equal
from cds_rdm.utils import compact_text


Expand Down Expand Up @@ -178,7 +178,7 @@ def _update_record(
if should_update_files and has_cds_doi and latest_res_type_changed:
self._resource_type_versioning(record, update_metadata, ctx, logger)
else:
is_pids_equal = update_metadata["pids"] == record_dict["pids"]
is_pids_equal = pids_equal(update_metadata["pids"], record_dict["pids"])
is_metadata_equal = compare_metadata(
update_metadata["metadata"], record_dict["metadata"]
)
Expand Down
Loading