VirtualDisk: Move xml cleanup to a global xmlbuilder hook

This commit is contained in:
Cole Robinson 2013-07-14 18:31:33 -04:00
parent c7ca9c88e3
commit 7cfe4ddb4d
2 changed files with 73 additions and 47 deletions

View File

@ -589,9 +589,9 @@ class VirtualDisk(VirtualDevice):
self._storage_backend = backend self._storage_backend = backend
def _refresh_backend_settings(self): def _refresh_backend_settings(self):
self.refresh_xml_prop("type") self._refresh_xml_prop("type")
self.refresh_xml_prop("driver_name") self._refresh_xml_prop("driver_name")
self.refresh_xml_prop("driver_type") self._refresh_xml_prop("driver_type")
self._xmlpath = self.path self._xmlpath = self.path
def __managed_storage(self): def __managed_storage(self):
@ -707,6 +707,13 @@ class VirtualDisk(VirtualDevice):
self.type == self.TYPE_BLOCK): self.type == self.TYPE_BLOCK):
self.driver_io = self.IO_MODE_NATIVE self.driver_io = self.IO_MODE_NATIVE
def _cleanup_xml(self, xml):
# Remove <driver> block if path is None. Might not be strictly
# requires but it's what we've always done
if not self.path and "<driver" in xml:
xml = "\n".join([l for l in xml.splitlines()
if "<driver" not in l])
return xml
def _get_xml_config(self): def _get_xml_config(self):
ret = " <disk>\n" ret = " <disk>\n"
@ -714,14 +721,6 @@ class VirtualDisk(VirtualDevice):
if addr: if addr:
ret += addr ret += addr
ret += " </disk>" ret += " </disk>"
ret = self._add_parse_bits(ret)
# Remove <driver> block if path is None. Might not be strictly
# requires but it's what we've always done
if not self.path and "<driver" in ret:
ret = "\n".join([l for l in ret.splitlines()
if "<driver" not in l])
return ret return ret
def is_size_conflict(self): def is_size_conflict(self):

View File

@ -596,6 +596,10 @@ class XMLBuilder(object):
self._parsexml(parsexml, parsexmlnode) self._parsexml(parsexml, parsexmlnode)
##############
# Public API #
##############
def copy(self): def copy(self):
# pylint: disable=W0212 # pylint: disable=W0212
# Access to protected member, needed to unittest stuff # Access to protected member, needed to unittest stuff
@ -608,9 +612,6 @@ class XMLBuilder(object):
return self._conn return self._conn
conn = property(_get_conn) conn = property(_get_conn)
def _is_parse(self):
return bool(self._xml_node or self._xml_ctx)
def set_xml_node(self, node): def set_xml_node(self, node):
self._parsexml(None, node) self._parsexml(None, node)
@ -619,6 +620,65 @@ class XMLBuilder(object):
return self._xml_node.nodePath() return self._xml_node.nodePath()
return None return None
def get_xml_config(self, *args, **kwargs):
"""
Construct and return object xml
@return: object xml representation as a string
@rtype: str
"""
if self._xml_ctx:
node = _get_xpath_node(self._xml_ctx, self._dumpxml_xpath)
if not node:
ret = ""
else:
ret = _sanitize_libxml_xml(node.serialize())
else:
ret = self._add_parse_bits(self._get_xml_config(*args, **kwargs))
ret = self._order_xml_elements(ret)
return self._cleanup_xml(ret)
#######################
# Internal helper API #
#######################
def _is_parse(self):
return bool(self._xml_node or self._xml_ctx)
def _refresh_xml_prop(self, propname):
"""
Refresh the XML for the passed class propname. Used to adjust
the XML when an interdependent property changes.
"""
getattr(self.__class__, propname).refresh_xml(self)
###################
# Child overrides #
###################
def set_defaults(self):
pass
def _get_xml_config(self):
"""
Internal XML building function. Must be overwritten by subclass
"""
raise NotImplementedError()
def _cleanup_xml(self, xml):
"""
Hook for classes to touch up the XML after generation.
"""
return xml
########################
# Internal XML parsers #
########################
def _add_child_node(self, parent_xpath, newnode): def _add_child_node(self, parent_xpath, newnode):
ret = _build_xpath_node(self._xml_ctx, parent_xpath, newnode) ret = _build_xpath_node(self._xml_ctx, parent_xpath, newnode)
return ret return ret
@ -699,21 +759,6 @@ class XMLBuilder(object):
self._proporder = origproporder self._proporder = origproporder
self._propstore = origpropstore self._propstore = origpropstore
def set_defaults(self):
pass
def refresh_xml_prop(self, propname):
"""
Refresh the XML for the passed class propname. Used to adjust
the XML when an interdependent property changes.
"""
getattr(self.__class__, propname).refresh_xml(self)
def _get_xml_config(self):
"""
Internal XML building function. Must be overwritten by subclass
"""
raise NotImplementedError()
def _order_xml_elements(self, xml): def _order_xml_elements(self, xml):
# This whole thing is reeeally hacky but it saves us some # This whole thing is reeeally hacky but it saves us some
@ -745,21 +790,3 @@ class XMLBuilder(object):
neworder += split neworder += split
return "\n".join([head] + neworder + [end]) return "\n".join([head] + neworder + [end])
def get_xml_config(self, *args, **kwargs):
"""
Construct and return object xml
@return: object xml representation as a string
@rtype: str
"""
if self._xml_ctx:
node = _get_xpath_node(self._xml_ctx, self._dumpxml_xpath)
if not node:
ret = ""
else:
ret = _sanitize_libxml_xml(node.serialize())
else:
ret = self._get_xml_config(*args, **kwargs)
return self._order_xml_elements(ret)