Esempio n. 1
0
def add_rights_elements(rights_list, files, updated=False):
    """
    Create and add rightsMDs for everything in rights_list to files.
    """
    # Add to files' amdSecs
    for fsentry in files:
        for rights in rights_list:
            # Create element
            new_rightsmd = fsentry.add_premis_rights(
                createmetsrights.createRightsStatement(rights,
                                                       fsentry.file_uuid))
            print('Adding rightsMD', new_rightsmd.id_string(),
                  'to amdSec with ID', fsentry.amdsecs[0].id_string(),
                  'for file', fsentry.file_uuid)

            if updated:
                # Mark as replacing another rightsMD
                # rightsBasis is semantically unique (though not currently enforced in code). Assume that this replaces a rightsMD with the same basis
                # Find the most ce
                superseded = [
                    s for s in fsentry.amdsecs[0].subsections
                    if s.subsection == 'rightsMD'
                ]
                superseded = sorted(superseded, key=lambda x: x.created)
                # NOTE sort(..., reverse=True) behaves differently with unsortable elements like '' and None
                for s in superseded[::-1]:
                    print('created', s.created)
                    if s.serialize().xpath('.//premis:rightsBasis[text()="' +
                                           rights.rightsbasis + '"]',
                                           namespaces=ns.NSMAP):
                        s.replace_with(new_rightsmd)
                        print('rightsMD', new_rightsmd.id_string(),
                              'replaces rightsMD', s.id_string())
                        break
Esempio n. 2
0
def add_rights_elements(job, rights_list, files, state, updated=False):
    """
    Create and add rightsMDs for everything in rights_list to files.
    """
    # Add to files' amdSecs
    for fsentry in files:
        for rights in rights_list:
            # Create element
            new_rightsmd = fsentry.add_premis_rights(
                createmetsrights.createRightsStatement(
                    job, rights, fsentry.file_uuid, state
                )
            )
            job.pyprint(
                "Adding rightsMD",
                new_rightsmd.id_string,
                "to amdSec with ID",
                fsentry.amdsecs[0].id_string,
                "for file",
                fsentry.file_uuid,
            )

            if updated:
                # Mark as replacing another rightsMD
                # rightsBasis is semantically unique (though not currently enforced in code). Assume that this replaces a rightsMD with the same basis
                # Find the most ce
                superseded = [
                    s
                    for s in fsentry.amdsecs[0].subsections
                    if s.subsection == "rightsMD"
                ]
                superseded = sorted(superseded, key=lambda x: x.created)
                # NOTE sort(..., reverse=True) behaves differently with unsortable elements like '' and None
                for rightmd in superseded[::-1]:
                    job.pyprint("created", rightmd.created)
                    if ns.xml_xpath_premis(
                        rightmd.serialize(),
                        './/premis:rightsBasis[text()="' + rights.rightsbasis + '"]',
                    ):
                        rightmd.replace_with(new_rightsmd)
                        job.pyprint(
                            "rightsMD",
                            new_rightsmd.id_string,
                            "replaces rightsMD",
                            rightmd.id_string,
                        )
                        break
Esempio n. 3
0
def add_rights_elements(rights_list,
                        amdsecs,
                        now,
                        rights_counter,
                        updated=False):
    """
    Create and add rightsMDs for everything in rights_list to amdsecs.
    """
    # Add to files' amdSecs
    for amdsec in amdsecs:
        # Get element to add rightsMDs after
        try:
            # Add after other rightsMDs
            add_after = amdsec.findall('mets:rightsMD',
                                       namespaces=ns.NSMAP)[-1]
        except IndexError:
            # If no rightsMDs, then techMD is aways there and previous subsection
            add_after = amdsec.findall('mets:techMD', namespaces=ns.NSMAP)[-1]
        for rights in rights_list:
            # Generate ID based on number of other rightsMDs
            rights_counter += 1
            rightsid = 'rightsMD_%s' % rights_counter
            print('Adding rightsMD', rightsid, 'to amdSec with ID',
                  amdsec.get('ID'))

            # Get file UUID for this file
            file_uuid = amdsec.findtext(
                'mets:techMD/mets:mdWrap[@MDTYPE="PREMIS:OBJECT"]//premis:objectIdentifierValue',
                namespaces=ns.NSMAP)
            print(rightsid, 'is for file', file_uuid)

            # Create element
            rightsMD = etree.Element(ns.metsBNS + "rightsMD",
                                     ID=rightsid,
                                     CREATED=now)
            mdWrap = etree.SubElement(rightsMD,
                                      ns.metsBNS + 'mdWrap',
                                      MDTYPE='PREMIS:RIGHTS')
            xmlData = etree.SubElement(mdWrap, ns.metsBNS + 'xmlData')
            rights_statement = createmetsrights.createRightsStatement(
                rights, file_uuid)
            xmlData.append(rights_statement)

            if updated:
                rightsMD.set('STATUS', 'current')
                # Find superseded rightsMD and mark as such
                # rightsBasis is semantically unique (though not currently
                # enforced in code). Find rightsMDs with the same rights basis
                # and mark superseded
                superseded = amdsec.xpath(
                    'mets:rightsMD[not(@STATUS) or @STATUS="current"]//premis:rightsBasis[text()="'
                    + rights.rightsbasis + '"]/ancestor::mets:rightsMD',
                    namespaces=ns.NSMAP)
                for elem in superseded:
                    print('Marking', elem.get('ID'), 'as superseded')
                    elem.set('STATUS', 'superseded')

            add_after.addnext(rightsMD)
            add_after = rightsMD

    return rights_counter