Exemple #1
0
def object(object_id,
           original_name=None,
           child_elements=None,
           representation=False,
           bitstream=False):
    """Return the PREMIS object.

        :object_id: PREMIS identifier
        :original_name: Original name field
        :child_elements=None: Any other element appended
        :representation=False: If true, representation object
        :bitstream=False: If true, bitstream object

    Returns the following ElementTree structure::

        <premis:object xsi:type="premis:representation">

            {{ premis_identifier() }}

            <premis:originalName>varmiste.sig</premis:originalName>

            {{ premis_relationship() }}

        </premis:object>

    """

    _object = _element('object', ns=NAMESPACES)

    _object.append(object_id)

    if representation:
        _object.set(xsi_ns('type'), 'premis:representation')
    elif bitstream:
        _object.set(xsi_ns('type'), 'premis:bitstream')
    else:
        _object.set(xsi_ns('type'), 'premis:file')

    _object_elements = []

    if original_name:
        _original_name = _element('originalName')
        _original_name.text = original_name
        _object_elements.append(_original_name)

    if child_elements:
        _object_elements.extend(child_elements)

    # Sort elements before appending them to the _object
    _object_elements.sort(key=_object_elems_order)
    for elem in _object_elements:
        _object.append(elem)

    return _object
Exemple #2
0
def event(event_id, event_type, event_date_time, event_detail,
          child_elements=None, linking_objects=None, linking_agents=None):
    """Create PREMIS event element.

    :event_id: PREMIS event identifier
    :event_type: Type for the event
    :event_date_time: Event time
    :event_detail: Event details
    :child_elements: Any child elements appended to the event (default=None)
    :linking_objects: Any linking objects appended to the event (default=None)

    Returns the following ElementTree structure::

        <premis:event>

            <premis:eventType>digital signature validation</premis:eventType>
            <premis:eventDateTime>2015-02-03T13:04:25</premis:eventDateTime>
            <premis:eventDetail>
                Submission information package digital signature validation
            </premis:eventDetail>

            {{ child elements }}

        </premis:event>

    """
    _event = _element('event')

    _event.append(event_id)

    _event_type = _subelement(_event, 'eventType')
    _event_type.text = decode_utf8(event_type)

    _event_date_time = _subelement(_event, 'eventDateTime')
    _event_date_time.text = decode_utf8(event_date_time)

    _event_detail = _subelement(_event, 'eventDetail')
    _event_detail.text = decode_utf8(event_detail)

    if child_elements:
        for elem in child_elements:
            _event.append(elem)

    if linking_agents:
        for _agent in linking_agents:
            linking_agent = identifier(
                _agent.findtext('.//' + premis_ns('agentIdentifierType')),
                _agent.findtext('.//' + premis_ns('agentIdentifierValue')),
                'linkingAgent')
            _event.append(linking_agent)

    if linking_objects:
        for _object in linking_objects:
            linking_object = identifier(
                _object.findtext('.//' + premis_ns('objectIdentifierType')),
                _object.findtext('.//' + premis_ns('objectIdentifierValue')),
                'linkingObject')
            _event.append(linking_object)

    return _event
Exemple #3
0
def environment(characteristic=None,
                purposes=None,
                notes=None,
                child_elements=None):
    """Return the PREMIS environment structure.

    :param characteristic: PREMIS environment characteristic as a string
    :param purposes: A list of environment purposes to be appended
    :param notes: A list of environment notes to be appended
    :param child_elements: A list of child elements to be appended
    :returns: ElementTree DOM tree
    """

    _environment = _element('environment')

    if characteristic:
        char_elem = _subelement(_environment, 'environmentCharacteristic')
        char_elem.text = decode_utf8(characteristic)

    if purposes:
        for purpose in purposes:
            purpose_elem = _subelement(_environment, 'environmentPurpose')
            purpose_elem.text = decode_utf8(purpose)

    if notes:
        for note in notes:
            note_elem = _subelement(_environment, 'environmentNote')
            note_elem.text = decode_utf8(note)

    if child_elements:
        for elem in child_elements:
            _environment.append(elem)

    return _environment
Exemple #4
0
def date_created(date):
    """
    :param date:
    :return: Element object for date created.
    """
    date_el = _element('dateCreatedByApplication')
    date_el.text = decode_utf8(date)
    return date_el
Exemple #5
0
def format(child_elements=None):
    """
    :param child_elements:
    :return: Element object for format.
    """
    format_el = _element('format')
    if child_elements:
        for elem in child_elements:
            format_el.append(elem)
    return format_el
Exemple #6
0
def creating_application(child_elements=None):
    """
    :param child_elements:
    :return: Element object for creating application.
    """
    creating_app = _element('creatingApplication')
    if child_elements:
        for elem in child_elements:
            creating_app.append(elem)
    return creating_app
Exemple #7
0
def format_registry(registry_name, registry_key):
    """
    :param registry_name:
    :param registry_key:
    :return: Element object for format registry.
    """
    format_registry_el = _element('formatRegistry')
    registry_name_el = _subelement(format_registry_el, 'formatRegistryName')
    registry_name_el.text = decode_utf8(registry_name)
    registry_key_el = _subelement(format_registry_el, 'formatRegistryKey')
    registry_key_el.text = decode_utf8(registry_key)
    return format_registry_el
Exemple #8
0
def fixity(message_digest, digest_algorithm='MD5'):
    """
    :param message_digest:
    :param digest_algorithm:
    :return: Element object for fixity.
    """
    fixity_el = _element('fixity')
    fixity_algorithm = _subelement(fixity_el, 'messageDigestAlgorithm')
    fixity_algorithm.text = decode_utf8(digest_algorithm)
    fixity_checksum = _subelement(fixity_el, 'messageDigest')
    fixity_checksum.text = message_digest
    return fixity_el
Exemple #9
0
def outcome(outcome, detail_note=None, detail_extension=None,
            single_extension_element=False):
    """Create PREMIS event outcome DOM structure.

    :outcome: Event outcome (success, failure)
    :detail_note: String description for the event outcome
    :detail_extension: List of detail extension etree elements
    :single_extension_element:
        True: all element trees in detail_extension are placed in a single
              eventOutcomeDetailExtension element.
        False: each element tree in detail_extension is placed in a separate
               eventOutcomeDetailExtension element.

    Returns the following ElementTree structure::

        <premis:eventOutcomeInformation>
            <premis:eventOutcome>success</premis:eventOutcome>
            <premis:eventOutcomeDetail>
                <premis:eventOutcomeDetailNote>
                    mets.xml sha1 4d0c38dedcb5e5fc93586cfa2b7ebedbd63 OK
                </premis:eventOutcomeDetailNote>
            </premis:eventOutcomeDetail>
        </premis:eventOutcomeInformation>

    """

    outcome_information = _element('eventOutcomeInformation')

    _outcome = _subelement(outcome_information, 'eventOutcome')
    _outcome.text = decode_utf8(outcome)

    detail = _subelement(outcome_information, 'eventOutcomeDetail')

    if detail_note is not None:
        _detail_note = _subelement(detail, 'eventOutcomeDetailNote')
        _detail_note.text = decode_utf8(detail_note)

    if detail_extension:
        if single_extension_element:
            # Add all extensions into one eventOutcomeDetailExtension element
            _detail_extension = _subelement(detail,
                                            'eventOutcomeDetailExtension')
            for extension in detail_extension:
                _detail_extension.append(extension)
        else:
            # Separate eventOutcomeDetailExtension element for each extension
            for extension in detail_extension:
                _detail_extension = _subelement(detail,
                                                'eventOutcomeDetailExtension')
                _detail_extension.append(extension)

    return outcome_information
Exemple #10
0
def format_designation(format_name, format_version=None):
    """
    :param format_name:
    :param format_version:
    :return: Element object for format designation.
    """
    format_designation_el = _element('formatDesignation')
    format_name_el = _subelement(format_designation_el, 'formatName')
    format_name_el.text = decode_utf8(format_name)
    if format_version:
        format_version_el = _subelement(format_designation_el, 'formatVersion')
        format_version_el.text = decode_utf8(format_version)
    return format_designation_el
Exemple #11
0
def object_characteristics(composition_level='0', child_elements=None):
    """
    :param composition_level:
    :param child_elements:
    :return: Element object for object characteristics.
    """
    object_char = _element('objectCharacteristics')

    composition = _subelement(object_char, 'compositionLevel')
    composition.text = decode_utf8(composition_level)
    if child_elements:
        for elem in child_elements:
            object_char.append(elem)
    return object_char
Exemple #12
0
def dependency(names=None, identifiers=None):
    """Returns the PREMIS dependency structure.

    :param names: A list of strings of dependency names
    :param identifiers: A list of PREMIS identifier structures
    :returns: ElementTree DOM tree
    """
    _dependency = _element('dependency')
    if names:
        for name in names:
            name_elem = _subelement(_dependency, 'dependencyName')
            name_elem.text = decode_utf8(name)

    if identifiers:
        for identifier_elem in identifiers:
            _dependency.append(identifier_elem)

    return _dependency
Exemple #13
0
def relationship(relationship_type, relationship_subtype, related_object):
    """Create PREMIS relationship DOM segment.

    :relationship_type: Relationship type from PREMIS vocabulary
    :relationship_subtype: Relationship subtype from PREMIS vocabulary
    :related_object: Related object linked to relationship
    :returns: ElementTree DOM tree

    Produces the following PREMIS segment::

      <premis:relationship>

          <premis:relationshipType>structural</premis:relationshipType>
          <premis:relationshipSubType>
              is included in
          </premis:relationshipSubType>

          {{ premis_identifier(prefix=related) }}

      </premis:relationship>

    """
    if related_object is None:
        return None

    _relationship = _element('relationship')

    _type = _subelement(_relationship, 'relationshipType')
    _type.text = decode_utf8(relationship_type)

    _subtype = _subelement(_relationship, 'relationshipSubType')
    _subtype.text = decode_utf8(relationship_subtype)

    (related_type, related_value) = parse_identifier_type_value(related_object)

    related_identifier = identifier(related_type,
                                    related_value,
                                    prefix='relatedObject')

    _relationship.append(related_identifier)

    return _relationship
Exemple #14
0
def agent(agent_id, agent_name, agent_type, note=None):
    """Returns PREMIS agent element

    :agent_id: PREMIS identifier for the agent
    :agent_name: Agent name
    :agent_type: Agent type

    Returns the following ElementTree structure::

        <premis:agent>
            <premis:agentIdentifier>
                <premis:agentIdentifierType>
                    preservation-agent-id</premis:agentIdentifierType>
                <premis:agentIdentifierValue>
                    preservation-agent-check_virus_clamscan.py-0.63-1422
                </premis:agentIdentifierValue>
            </premis:agentIdentifier>
            <premis:agentName>check_virus_clamscan.py</premis:agentName>
            <premis:agentType>software</premis:agentType>
        </premis:agent>

    """

    _agent = _element('agent')

    _agent.append(agent_id)

    _agent_name = _subelement(_agent, 'agentName')
    _agent_name.text = decode_utf8(agent_name)

    _agent_type = _subelement(_agent, 'agentType')
    _agent_type.text = decode_utf8(agent_type)

    if note is not None:
        _agent_type = _subelement(_agent, 'agentNote')
        _agent_type.text = decode_utf8(note)

    return _agent
def test_element():
    """Test PREMIS _element"""
    xml = """<premis:xxx xmlns:premis="info:lc/xmlns/premis-v2"/>"""
    assert u.compare_trees(p._element('xxx'), ET.fromstring(xml))