mirror of
https://github.com/virt-manager/virt-manager.git
synced 2025-02-25 18:55:27 -06:00
xmlbuilder: More comments for _build_xpath_node
This commit is contained in:
parent
a931a1a6ad
commit
835ddc5f77
@ -158,44 +158,56 @@ def _add_pretty_child(parentnode, newnode):
|
|||||||
|
|
||||||
def _build_xpath_node(ctx, xpath):
|
def _build_xpath_node(ctx, xpath):
|
||||||
"""
|
"""
|
||||||
Build all nodes required to set an xpath. If we have XML <foo/>, and want
|
Build all nodes for the passed xpath. For example, if 'ctx' xml=<foo/>,
|
||||||
to set xpath /foo/bar/baz@booyeah, we create node 'bar' and 'baz'
|
and xpath=./bar/@baz, after this function the 'ctx' XML will be:
|
||||||
returning the last node created.
|
|
||||||
|
<foo>
|
||||||
|
<bar baz=''/>
|
||||||
|
</foo>
|
||||||
|
|
||||||
|
And the node pointing to @baz will be returned, for the caller to
|
||||||
|
do with as they please.
|
||||||
"""
|
"""
|
||||||
parentpath = ""
|
def _handle_node(nodename, parentnode, parentpath):
|
||||||
parentnode = None
|
# If the passed xpath snippet (nodename) exists, return the node
|
||||||
|
# If it doesn't exist, create it, and return the new node
|
||||||
|
|
||||||
nodelist = xpath.split("/")
|
# If nodename is a node property, we can handle it up front
|
||||||
for nodename in nodelist:
|
|
||||||
if not nodename:
|
|
||||||
continue
|
|
||||||
|
|
||||||
# If xpath is a node property, set it and move on
|
|
||||||
if nodename.startswith("@"):
|
if nodename.startswith("@"):
|
||||||
nodename = nodename.strip("@")
|
nodename = nodename.strip("@")
|
||||||
parentnode = parentnode.setProp(nodename, "")
|
return parentnode.setProp(nodename, ""), parentpath
|
||||||
continue
|
|
||||||
|
|
||||||
if not parentpath:
|
if not parentpath:
|
||||||
parentpath = nodename
|
parentpath = nodename
|
||||||
else:
|
else:
|
||||||
parentpath += "/%s" % nodename
|
parentpath += "/%s" % nodename
|
||||||
|
|
||||||
# Node found, nothing to create for now
|
# See if the xpath node already exists
|
||||||
node = _get_xpath_node(ctx, parentpath)
|
node = _get_xpath_node(ctx, parentpath)
|
||||||
if node:
|
if node:
|
||||||
parentnode = node
|
# xpath node already exists, so we don't need to create anything
|
||||||
continue
|
return node, parentpath
|
||||||
|
|
||||||
|
# If we don't have a parentnode by this point, the root of the
|
||||||
|
# xpath didn't find anything. Usually a coding error
|
||||||
if not parentnode:
|
if not parentnode:
|
||||||
raise RuntimeError("Could not find XML root node")
|
raise RuntimeError("Could not find XML root node")
|
||||||
|
|
||||||
# Remove conditional xpath elements for node creation
|
# Remove conditional xpath elements for node creation. We preserved
|
||||||
|
# them up until this point since it was needed for proper xpath
|
||||||
|
# lookup, but they aren't valid syntax when creating the node
|
||||||
if nodename.count("["):
|
if nodename.count("["):
|
||||||
nodename = nodename[:nodename.index("[")]
|
nodename = nodename[:nodename.index("[")]
|
||||||
|
|
||||||
newnode = libxml2.newNode(nodename)
|
newnode = libxml2.newNode(nodename)
|
||||||
parentnode = _add_pretty_child(parentnode, newnode)
|
return _add_pretty_child(parentnode, newnode), parentpath
|
||||||
|
|
||||||
|
|
||||||
|
# Split the xpath and lookup/create each individual piece
|
||||||
|
parentpath = None
|
||||||
|
parentnode = None
|
||||||
|
for nodename in xpath.split("/"):
|
||||||
|
parentnode, parentpath = _handle_node(nodename, parentnode, parentpath)
|
||||||
|
|
||||||
return parentnode
|
return parentnode
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user