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
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
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
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
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
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
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
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 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
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
def test_subelement(): """Test PREMIS _subelement""" xml = """<premis:xxx xmlns:premis="info:lc/xmlns/premis-v2"/>""" parent_xml = """<premis:premis xmlns:premis="info:lc/xmlns/premis-v2"/>""" parent = ET.fromstring(parent_xml) assert u.compare_trees(p._subelement(parent, 'xxx'), ET.fromstring(xml))