def preprocess_xml(xml): """This transforms the read XML through macros. Each macro consists of an xpath and a replacement xml string""" for path, replacement in content.Macros(): replacement = etree.fromstring('<ROOT>' + replacement + '</ROOT>') for node in xml.xpath(path): parent = node.getparent() idx = parent.index(node) parent.remove(node) for repl in replacement: parent.insert(idx, repl) idx += 1
def test_iterate(self, try_to_load): def response(path, _): if path == 'return_none': return None else: return [('a', 'b'), ('c', 'd')] try_to_load.side_effect = response settings.MACROS_SOURCES = ['source1', 'return_none', 'source2'] pairs = [pair for pair in content.Macros()] self.assertEqual( pairs, [ ('a', 'b'), ('c', 'd'), # source1 ('a', 'b'), ('c', 'd') ]) # source2