From e13d8a333b4de42233483ea18ec5eadd3b0617fa Mon Sep 17 00:00:00 2001 From: "bit4bit@riseup.net" Date: Wed, 28 Oct 2020 02:16:41 +0000 Subject: [PATCH] facho/facho.py: adiciona replacement_for para sustitucion. FossilOrigin-Name: 5e9210afdf4577388a206310ded9abbcf129d02f3818671aba3aad1282726466 --- facho/facho.py | 8 ++++++++ tests/test_facho.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/facho/facho.py b/facho/facho.py index 0f143a9..9f7101e 100644 --- a/facho/facho.py +++ b/facho/facho.py @@ -82,6 +82,9 @@ class LXMLBuilder: def append(self, elem, child): elem.append(child) + def remove(self, elem): + elem.getparent().remove(elem) + def set_text(self, elem, text): elem.text = text @@ -164,6 +167,11 @@ class FachoXML: def placeholder_for(self, xpath): return self.find_or_create_element(xpath) + def replacement_for(self, xpath, new_xpath, content, **attrs): + elem = self.get_element(xpath) + self.builder.remove(elem) + return self.set_element(new_xpath, content, **attrs) + def find_or_create_element(self, xpath, append=False): """ @param xpath ruta xpath para crear o consultar de un solo elemendo diff --git a/tests/test_facho.py b/tests/test_facho.py index cfcffa1..321435d 100644 --- a/tests/test_facho.py +++ b/tests/test_facho.py @@ -170,3 +170,11 @@ def test_facho_xml_fragment_relative(): invoice = xml.fragment('./Invoice') invoice.set_element('./Id', 1) assert xml.tostring() == '1' + + +def test_facho_xml_replacement_for(): + xml = facho.FachoXML('root') + xml.placeholder_for('./child/type') + xml.replacement_for('./child/type', + './child/code', 'test') + assert xml.tostring() == 'test'