def delimfileformat(recordseparator, fieldseparatingchar, quotingchar=None): """Creates the ADDML delimFileFormat section. :recordseparator: the charcter separating the records :fieldseparatingchar: the character separating the fields :quotingchar: the quoting character used around the fields (default=None) Returns the following lxml.etree strucure: <addml:delimFileFormat> <addml:recordSeparator>CR+LF</addml:recordSeparator> <addml:fieldSeparatingChar>;</addml:fieldSeparatingChar> <addml:quotingChar>'</addml:quotingChar> </addml:delimFileFormat> """ delimfileformat_el = _element('delimFileFormat') recordseparator_el = _subelement(delimfileformat_el, 'recordSeparator') recordseparator_el.text = h.decode_utf8(recordseparator) fieldseparatingchar_el = _subelement(delimfileformat_el, 'fieldSeparatingChar') fieldseparatingchar_el.text = h.decode_utf8(fieldseparatingchar) if quotingchar: quotingchar_el = _subelement(delimfileformat_el, 'quotingChar') quotingchar_el.text = h.decode_utf8(quotingchar) return delimfileformat_el
def definition_elems(tag, attname, reference=None, child_elements=None): """Creates addml definition elements with a supplied tag if it matches a value in the lists. The definition elements are addml elements that an be identified with the @name attribute. Most of the definition elements also carry reference attributes that link to an other definition element's @name. Appends child_elements if they are supplied. """ elems = ['flatFile', 'flatFileDefinition', 'recordDefinition'] ref_elems = ['flatFile', 'fieldDefinition'] noref_elems = ['flatFileType', 'recordType', 'fieldType', 'property'] if tag in elems or tag in ref_elems or tag in noref_elems: _definition_el = _element(tag) _definition_el.set('name', attname) if tag in ref_elems and not reference: reference = six.text_type(uuid4()) if tag in noref_elems: reference = None if tag == 'flatFile': _definition_el.set('definitionReference', reference) else: if reference: _definition_el.set('typeReference', reference) if child_elements: for elem in child_elements: _definition_el.append(elem) return _definition_el return None
def addml_basic_elem(tag, contents): """Creates ADDML basic elems that are elements which contain text as values. Only create elements if the supplied tag value is inlcuded in the tags list. """ tags = ['charset', 'dataType'] if tag in tags: addml_el = _element(tag) addml_el.text = h.decode_utf8(contents) return addml_el return None
def wrapper_elems(tag, child_elements=None): """Creates addml wrapper elements with a supplied tag if it matches a value in the elems list. Appends child_elements if they are supplied. """ elems = [ 'flatFiles', 'flatFileDefinitions', 'recordDefinitions', 'fieldDefinitions', 'structureTypes', 'flatFileTypes', 'recordTypes', 'fieldTypes', 'properties' ] if tag in elems: wrapper_el = _element(tag) if child_elements: for elem in child_elements: wrapper_el.append(elem) return wrapper_el return None
def test_element_with_prefix(): """Test ADDML _elementi with a prefix.""" xml = """<addml:flatFileDefinition xmlns:addml="http://www.arkivverket.no/standarder/addml"/>""" assert h.compare_trees(a._element('definition', prefix="flatFile"), ET.fromstring(xml)) is True
def test_element(): """Test ADDML _element""" xml = """<addml:xxx xmlns:addml="http://www.arkivverket.no/standarder/addml"/>""" assert h.compare_trees(a._element('xxx'), ET.fromstring(xml)) is True