Signed-off-by: Chris Evich <[email protected]>
---
 client/shared/xml_utils.py          |   33 +++++++++++++++++++++++++++++++++
 client/shared/xml_utils_unittest.py |    9 +++++++++
 2 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/client/shared/xml_utils.py b/client/shared/xml_utils.py
index 4d5c977..3efcbbc 100644
--- a/client/shared/xml_utils.py
+++ b/client/shared/xml_utils.py
@@ -159,6 +159,39 @@ class XMLTreeFile(ElementTree.ElementTree, XMLBackup):
         self.flush()
 
 
+    def get_parent_map(self, element=None):
+        """
+        Return a child to parent mapping dictionary
+
+        param: element: Search only below this element
+        """
+        # Comprehension loop over all children in all parents
+        return dict((c, p) for p in self.getiterator(element) for c in p)
+
+
+    def get_parent(self, element, relative_root=None):
+        """
+        Return the parent node of an element or None
+
+        param: element: Element to retrieve parent of
+        param: relative_root: Search only below this element
+        """
+        try:
+            return self.get_parent_map(relative_root)[element]
+        except KeyError:
+            return None
+
+
+    def remove(self, element):
+        """
+        Removes a matching subelement.
+
+        @param: element: element to be removed.
+        """
+
+        self.get_parent(element).remove(element)
+
+
     def write(self, filename=None, encoding="UTF-8"):
         """
         Write current XML tree to filename, or self.name if None.
diff --git a/client/shared/xml_utils_unittest.py 
b/client/shared/xml_utils_unittest.py
index 58a03d5..ca21510 100755
--- a/client/shared/xml_utils_unittest.py
+++ b/client/shared/xml_utils_unittest.py
@@ -251,6 +251,15 @@ class test_XMLTreeFile(test_XMLBackup):
         self.assertTrue(self.is_same_contents(otherfile.name))
 
 
+    def test_remove(self):
+        xmlbackup = self.class_to_test(self.XMLFILE)
+        host_cpu_topology = xmlbackup.find('host/cpu/topology')
+        self.assertNotEqual(host_cpu_topology, None)
+        xmlbackup.remove(host_cpu_topology)
+        self.assertEqual(xmlbackup.find('host/cpu/topology'), None)
+        self.assertRaises(AttributeError, xmlbackup.remove, host_cpu_topology)
+
+
 class test_templatized_xml(xml_test_data):
 
     def setUp(self):
-- 
1.7.4.1

_______________________________________________
Autotest-kernel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/autotest-kernel

Reply via email to