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
def mix(child_elements=None, namespaces=None): """Create MIX Data Dictionary root element. :child_elements: Any elements appended to the MIX dictionary Returns the following ElementTree structure:: <mix:mix xmlns:mix="http://www.loc.gov/mix/v20" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.loc.gov/mix/v20 http://www.loc.gov/mix/mix.xsd"/> """ if namespaces is None: namespaces = NAMESPACES _mix = _element('mix', namespaces=namespaces) _mix.set(xsi_ns('schemaLocation'), 'http://www.loc.gov/mix/v20 ' 'http://www.loc.gov/mix/mix.xsd') if child_elements: child_elements.sort(key=mix_root_order) for element in child_elements: _mix.append(element) return _mix
def premis(child_elements=None, namespaces=None): """Create PREMIS Data Dictionary root element. :child_elements: Any elements appended to the PREMIS dictionary Returns the following ElementTree structure:: <premis:premis xmlns:premis="info:lc/xmlns/premis-v2" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="info:lc/xmlns/premis-v2 http://www.loc.gov/standards/premis/v2/premis-v2-3.xsd" version="2.2"> """ if namespaces is None: namespaces = NAMESPACES _premis = _element('premis', ns=namespaces) _premis.set( xsi_ns('schemaLocation'), 'info:lc/xmlns/premis-v2 ' 'http://www.loc.gov/standards/premis/v2/premis-v2-3.xsd') _premis.set('version', '2.2') if child_elements: for elem in child_elements: _premis.append(elem) return _premis
def mets(profile='local', objid=None, label=None, namespaces=None, child_elements=None): """Create METS Element""" if namespaces is None: namespaces = NAMESPACES if objid is None: objid = six.text_type(uuid.uuid4()) _mets = _element('mets', ns=namespaces) _mets.set( xsi_ns('schemaLocation'), 'http://www.loc.gov/METS/ ' 'http://www.loc.gov/standards/mets/mets.xsd') _mets.set('PROFILE', decode_utf8(profile)) _mets.set('OBJID', decode_utf8(objid)) if label: _mets.set('LABEL', decode_utf8(label)) if child_elements: for elem in child_elements: _mets.append(elem) return _mets
def test_parse_mdwrap(): """Test the parse_mdwrap """ root = ET.parse(os.path.join(TESTPATH, 'data', 'valid_mets.xml')).getroot() wrap = mets.parse_mdwrap(mets.parse_element_with_id(root, 'tech003')) mdtype = mets.parse_wrap_mdtype(wrap) xmldata = mets.parse_xmldata(wrap) assert mdtype['mdtype'] == 'PREMIS:OBJECT' assert xmldata.attrib[xsi_ns('type')] == 'premis:file'
def addml(child_elements=None, namespaces=None): """Creates an addml root element with correct namespace definition and schemalocation attributes. Also creates the mandatory <addml:dataset> element within the root element. """ if namespaces is None: namespaces = NAMESPACES _addml = _element('addml', ns=namespaces) _addml.set( h.xsi_ns('schemaLocation'), 'http://www.arkivverket.no/standarder/addml ' 'http://schema.arkivverket.no/ADDML/latest/addml.xsd') _dataset = _subelement(_addml, 'dataset') if child_elements: for elem in child_elements: _dataset.append(elem) return _addml
def test_xsi_ns(): """test xsi namespace""" assert u.xsi_ns('a') == '{http://www.w3.org/2001/XMLSchema-instance}a'
def test_get_namespace(): """test get_namespace""" elem = ET.Element(u.xsi_ns('a')) assert u.get_namespace(elem) == 'http://www.w3.org/2001/XMLSchema-instance'