From baaab74b6d44525682e6db3c62dd86771e13cc51 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Thu, 13 Sep 2018 10:33:08 -0400 Subject: [PATCH] xmlapi: Rework xmlns setting slightly So the xmlns is not always set on the top element. We need this for custom support --- .../compare/virt-install-many-devices.xml | 4 ++-- .../virt-xml-edit-simple-qemu-commandline.xml | 8 +------ virtinst/xmlapi.py | 23 +++++++++++-------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/tests/cli-test-xml/compare/virt-install-many-devices.xml b/tests/cli-test-xml/compare/virt-install-many-devices.xml index 4044f06a..a2f80c63 100644 --- a/tests/cli-test-xml/compare/virt-install-many-devices.xml +++ b/tests/cli-test-xml/compare/virt-install-many-devices.xml @@ -1,4 +1,4 @@ - + foobar 00000000-1111-2222-3333-444444444444 65536 @@ -400,7 +400,7 @@
- + diff --git a/tests/cli-test-xml/compare/virt-xml-edit-simple-qemu-commandline.xml b/tests/cli-test-xml/compare/virt-xml-edit-simple-qemu-commandline.xml index 896d3b1c..11ed2e3e 100644 --- a/tests/cli-test-xml/compare/virt-xml-edit-simple-qemu-commandline.xml +++ b/tests/cli-test-xml/compare/virt-xml-edit-simple-qemu-commandline.xml @@ -1,13 +1,7 @@ -- -+ - test-for-virtxml - 12345678-12f4-1234-1234-123456789012 - Test VM for virtxml cli tests -@@ -+ ++ + + + diff --git a/virtinst/xmlapi.py b/virtinst/xmlapi.py index 00e0bd8d..1b0452c3 100644 --- a/virtinst/xmlapi.py +++ b/virtinst/xmlapi.py @@ -97,7 +97,7 @@ class _XMLBase(object): raise NotImplementedError() def _node_set_property(self, node, propname, setval): raise NotImplementedError() - def _node_new(self, xpathseg): + def _node_new(self, xpathseg, parentnode): raise NotImplementedError() def _node_add_child(self, parentxpath, parentnode, newnode): raise NotImplementedError() @@ -212,7 +212,7 @@ class _XMLBase(object): parentnode = tmpnode continue - newnode = self._node_new(xpathseg) + newnode = self._node_new(xpathseg, parentnode) self._node_add_child(oldxpath, parentnode, newnode) parentnode = newnode @@ -307,17 +307,22 @@ class _Libxml2API(_XMLBase): else: node.setProp(propname, util.xml_escape(setval)) - def _node_new(self, xpathseg): + def _node_new(self, xpathseg, parentnode): newnode = libxml2.newNode(xpathseg.nodename) if not xpathseg.nsname: return newnode - ctxnode = self._ctx.contextNode() - for ns in util.listify(ctxnode.nsDefs()): - if ns.name == xpathseg.nsname: - break - else: - ns = ctxnode.newNs( + def _find_parent_ns(): + parent = parentnode + while parent: + for ns in util.listify(parent.nsDefs()): + if ns.name == xpathseg.nsname: + return ns + parent = parent.get_parent() + + ns = _find_parent_ns() + if not ns: + ns = newnode.newNs( self.NAMESPACES[xpathseg.nsname], xpathseg.nsname) newnode.setNs(ns) return newnode