From b08647c2f277e463971ae1e4430aaed28a4619f3 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Mon, 18 Jul 2016 14:40:58 -0400 Subject: [PATCH] xmlbuilder: Handle setting conditional xpaths correctly So if xml= and xpath=./bar[@baz='foo'] and val=XXX, xmlbuilder previously would generate XML XXX But now generates the expected XXX No users yet, but they are incoming --- virtinst/xmlbuilder.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/virtinst/xmlbuilder.py b/virtinst/xmlbuilder.py index 228d41bbb..039662e63 100644 --- a/virtinst/xmlbuilder.py +++ b/virtinst/xmlbuilder.py @@ -167,6 +167,16 @@ def _build_xpath_node(ctx, xpath): And the node pointing to @baz will be returned, for the caller to do with as they please. + + There's also special handling to ensure that setting + xpath=./bar[@baz='foo']/frob will create + + + + + + Even if didn't exist before. So we fill in the dependent property + expression values """ def _handle_node(nodename, parentnode, parentpath): # If the passed xpath snippet (nodename) exists, return the node @@ -209,6 +219,19 @@ def _build_xpath_node(ctx, xpath): for nodename in xpath.split("/"): parentnode, parentpath = _handle_node(nodename, parentnode, parentpath) + # Check if the xpath snippet had an '=' expression in it, example: + # + # ./foo[@bar='baz'] + # + # If so, we also want to set , so that setting + # this XML element works as expected in this case. + if "[" not in nodename or "=" not in nodename: + continue + + propname, val = nodename.split("[")[1].strip("]").split("=") + propobj, ignore = _handle_node(propname, parentnode, parentpath) + propobj.setContent(val.strip("'")) + return parentnode