Skip to content

Commit

Permalink
Improve retrieval of original name
Browse files Browse the repository at this point in the history
Having used AIPscan a little more, leaving the name blank is strange
to look at, but we also cannot always retrieve the original name. We
do our best to a) retrieve the name in this commit, as well as b)
populate the name with the transfer UUID as an alternative.
  • Loading branch information
ross-spencer committed Sep 30, 2020
1 parent 6c930b6 commit 9a1e708
Show file tree
Hide file tree
Showing 5 changed files with 3,584 additions and 17 deletions.
31 changes: 19 additions & 12 deletions AIPscan/Aggregator/mets_parse_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,43 @@ def parse_mets_with_metsrw(mets_file):


def get_aip_original_name(mets):
"""Retrieve PREMIS original name from a METSDocument object."""
"""Retrieve PREMIS original name from a METSDocument object.
If the original name cannot be reliably retrieved from the METS file
a METSError exception is returned to be handled by the caller as
desired.
"""

# Negated as we're going to want to remove this length of values.
NAMESUFFIX = -len("-00000000-0000-0000-0000-000000000000")

# The transfer directory prefix is a directory prefix that can also
# exist in a dmdSec intellectual entity and we want to identify and
# ignore those.
TRANSFER_DIR_PREFIX = "%transferDirectory%"

NAMESPACES = {u"premis": u"http://www.loc.gov/premis/v3"}
ELEM_ORIGINAL_NAME_PATTERN = ".//premis:originalName"

FIRST_DMDSEC = "dmdSec_1"

original_name = ""
for fsentry in mets.all_files():
try:
dmdsec = fsentry.dmdsecs[0]
if dmdsec.id_string != FIRST_DMDSEC:
continue
for dmdsec in fsentry.dmdsecs:
dmd_element = dmdsec.serialize()
full_name = dmd_element.find(
ELEM_ORIGINAL_NAME_PATTERN, namespaces=NAMESPACES
)
if full_name is not None and full_name.text.startswith(TRANSFER_DIR_PREFIX):
# We don't want this value, it will usually represent an
# directory entity.
continue
try:
original_name = full_name.text[:NAMESUFFIX]
except AttributeError:
pass
break
except IndexError:
pass
continue

# There should be a transfer name in every METS.
if original_name == "":
raise METSError()
raise METSError("Cannot locate transfer name in METS")

return original_name

Expand Down
2 changes: 1 addition & 1 deletion AIPscan/Aggregator/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def get_mets(
except METSError:
# Some other error with the METS file that we might want to
# log and act upon.
originalName = ""
originalName = packageUUID

aip = create_aip_object(
package_uuid=packageUUID,
Expand Down
Loading

0 comments on commit 9a1e708

Please sign in to comment.