XMLBuilder: Simplify is_multi handling

This commit is contained in:
Cole Robinson 2013-07-13 18:04:49 -04:00
parent d8c68e3495
commit 4d5ca0ae5a
2 changed files with 21 additions and 15 deletions

View File

@ -63,14 +63,8 @@ class Boot(XMLBuilderDomain.XMLBuilderDomain):
return self._bootorder
def _set_bootorder(self, val):
self._bootorder = val
def _bootorder_xpath_list(self):
l = []
for idx in range(len(self._get_bootorder())):
l.append("./os/boot[%d]/@dev" % (idx + 1))
return l
bootorder = _xml_property(_get_bootorder, _set_bootorder,
is_multi=True,
xml_set_list=_bootorder_xpath_list,
xpath="./os/boot/@dev")
def _get_kernel(self):

View File

@ -215,7 +215,7 @@ class _xml_property(property):
def __init__(self, fget=None, fset=None, doc=None,
xpath=None, get_converter=None, set_converter=None,
xml_get_xpath=None, xml_set_xpath=None,
xml_set_list=None, is_bool=False, is_multi=False,
is_bool=False, is_multi=False,
default_converter=None, clear_first=None):
"""
Set a XMLBuilder class property that represents a value in the
@ -243,8 +243,6 @@ class _xml_property(property):
@param xml_set_xpath: Not all props map cleanly to a static xpath.
This allows passing functions which generate an xpath for getting
or setting.
@param xml_set_list: Return a list of xpaths to set for each value
in the val list
@param is_bool: Whether this is a boolean property in the XML
@param is_multi: Whether data is coming multiple or a single node
@param default_converter: If the virtinst value is "default", use
@ -261,7 +259,6 @@ class _xml_property(property):
self._xpath_for_getter_cb = xml_get_xpath
self._xpath_for_setter_cb = xml_set_xpath
self._xml_set_list = xml_set_list
self._convert_value_for_getter_cb = get_converter
self._convert_value_for_setter_cb = set_converter
@ -344,6 +341,23 @@ class _xml_property(property):
return self._xpath_for_setter_cb(xmlbuilder)
return self._xpath
def _xpath_list_for_setter(self, xpath, setval, nodelist):
if not self._is_multi:
return [xpath]
ret = []
list_length = max(len(nodelist), len(setval), 1)
# This might not generally work, but as of this writing there's
# only one user of is_multi and it works for that. It's probably
# generalizable though.
for i in range(list_length):
idxstr = "[%d]/" % (i + 1)
splitpath = xpath.rsplit("/", 1)
ret.append("%s%s%s" % (splitpath[0], idxstr, splitpath[1]))
return ret
def _convert_value_for_setter(self, xmlbuilder):
# Convert from API value to XML value
val = self._orig_fget(xmlbuilder)
@ -435,14 +449,12 @@ class _xml_property(property):
nodelist = self._build_node_list(xmlbuilder, xpath)
clearlist = self._build_clear_list(xmlbuilder, nodelist)
xpath_list = xpath
if self._xml_set_list:
xpath_list = self._xml_set_list(xmlbuilder)
node_map = []
if clearlist:
node_map += _tuplify_lists(clearlist, None, "")
node_map += _tuplify_lists(nodelist, setval, xpath_list)
node_map += _tuplify_lists(nodelist, setval,
self._xpath_list_for_setter(xpath, setval, nodelist))
for node, val, use_xpath in node_map:
if node:
use_xpath = node.nodePath()