def update_element(elem, idx, root, path, vals): """ Internal helper function to encapsulate single item update """ has_root = bool(root and len(path) > len(root) and path.startswith(root)) path, attr = get_xpath_tuple(path) # 'path/@attr' to ('path', 'attr') if attr: removed = [get_element(elem, path)] remove_element_attributes(removed[0], attr) elif not has_root: removed = wrap_value(remove_element(elem, path)) else: path = get_xpath_branch(root, path) removed = [] if idx != 0 else [remove_element(e, path, True) for e in get_elements(elem, root)] if not vals: return removed items = [] for i, val in enumerate(wrap_value(vals)): elem_to_update = elem if has_root: elem_to_update = insert_element(elem, (i + idx), root) val = val.decode('utf-8') if not isinstance(val, string_types) else val if not attr: items.append(insert_element(elem_to_update, i, path, val)) else: items.append(insert_element(elem_to_update, i, path, **{attr: val})) return items
def update_element(elem, idx, root, path, vals): """ Internal helper function to encapsulate single item update """ has_root = bool(root and len(path) > len(root) and path.startswith(root)) path, attr = get_xpath_tuple(path) # 'path/@attr' to ('path', 'attr') if attr: removed = [get_element(elem, path)] remove_element_attributes(removed[0], attr) elif not has_root: removed = wrap_value(remove_element(elem, path)) else: path = get_xpath_branch(root, path) removed = [] if idx != 0 else [ remove_element(e, path, True) for e in get_elements(elem, root) ] if not vals: return removed items = [] for i, val in enumerate(wrap_value(vals)): elem_to_update = elem if has_root: elem_to_update = insert_element(elem, (i + idx), root) val = val.decode('utf-8') if not isinstance(val, string_types) else val if not attr: items.append(insert_element(elem_to_update, i, path, val)) elif path: items.append( insert_element(elem_to_update, i, path, **{attr: val})) else: set_element_attributes(elem_to_update, **{attr: val}) items.append(elem_to_update) return items
def test_iso_parser(self): """ Tests behavior unique to the ISO parser """ # Remove the attribute details href attribute iso_element = get_remote_element(self.iso_file) for citation_element in get_elements(iso_element, _iso_tag_formats["_attr_citation"]): removed = remove_element_attributes(citation_element, "href") # Assert that the href attribute was removed and a different one was read in iso_parser = IsoParser(element_to_string(iso_element)) attribute_href = iso_parser._attr_details_file_url self.assertIsNotNone(removed, "ISO file URL was not removed") self.assertIsNotNone(attribute_href, "ISO href attribute was not read in") self.assertNotEqual(attribute_href, removed, "ISO href attribute is the same as the one removed") # Remove the attribute details linkage attribute iso_element = get_remote_element(self.iso_file) for linkage_element in get_elements(iso_element, _iso_tag_formats["_attr_contact_url"]): removed = get_element_text(linkage_element) clear_element(linkage_element) # Assert that the linkage URL was removed and a different one was read in iso_parser = IsoParser(element_to_string(iso_element)) linkage_url = iso_parser._attr_details_file_url self.assertIsNotNone(removed, "ISO linkage URL was not removed") self.assertIsNotNone(linkage_url, "ISO linkage URL was not read in") self.assertNotEqual(linkage_url, removed, "ISO file URL is the same as the one removed") # Change the href attribute so that it is invalid for citation_element in get_elements(iso_element, _iso_tag_formats["_attr_citation"]): removed = set_element_attributes(citation_element, href="neither url nor file") # Assert that the href attribute was removed and a different one was read in iso_parser = IsoParser(element_to_string(iso_element)) attributes = iso_parser.attributes self.assertIsNone(iso_parser._attr_details_file_url, "Invalid URL stored with parser") self.assertEqual( attributes, TEST_METADATA_VALUES[ATTRIBUTES], "Invalid parsed attributes: {0}".format(attributes) )