xmlbuilder: Unify make_xpath callbacks

This commit is contained in:
Cole Robinson 2013-09-24 09:25:05 -04:00
parent 2148eaa2ae
commit 9ec845af03
6 changed files with 38 additions and 73 deletions

View File

@ -770,6 +770,7 @@ class XMLParseTest(unittest.TestCase):
dev1 = guest.get_devices("console")[0]
check = self._make_checker(dev1)
check("source_path", "/dev/pts/4")
check("_tty", "/dev/pts/4", "foo", "/dev/pts/4")
self._alter_compare(guest.get_xml_config(), outfile)

View File

@ -142,7 +142,7 @@ class _VirtualCharDevice(VirtualDevice):
_XML_PROP_ORDER = ["type", "_has_mode_bind", "_has_mode_connect",
"bind_host", "bind_port",
"source_mode", "source_path",
"source_mode", "_source_path",
"source_host", "source_port",
"target_type", "target_name"]
@ -150,17 +150,18 @@ class _VirtualCharDevice(VirtualDevice):
doc=_("Method used to expose character device in the host."),
xpath="./@type")
def _sourcepath_get_xpath(self):
ret = "./source/@path"
for xpath in [ret, "./@tty"]:
if self._xmlstate.xml_ctx.xpathEval(
self.fix_relative_xpath(xpath)):
ret = xpath
break
return ret
source_path = XMLProperty(make_getter_xpath_cb=_sourcepath_get_xpath,
doc=_("Host input path to attach to the guest."),
xpath="./source/@path")
_tty = XMLProperty("./@tty")
_source_path = XMLProperty(xpath="./source/@path",
doc=_("Host input path to attach to the guest."))
def _get_source_path(self):
source = self._source_path
if source is None and self._tty:
return self._tty
return source
def _set_source_path(self, val):
self._source_path = val
source_path = property(_get_source_path, _set_source_path)
def _get_default_source_mode(self):
if self.type == self.TYPE_UDP:
@ -168,14 +169,13 @@ class _VirtualCharDevice(VirtualDevice):
if not self.supports_property("source_mode"):
return None
return self.MODE_BIND
def _sourcemode_xpath(self):
def _make_sourcemode_xpath(self):
if self.type == self.TYPE_UDP:
return "./source[@mode='connect']/@mode"
return "./source/@mode"
source_mode = XMLProperty(name="char sourcemode",
doc=_("Target connect/listen mode."),
make_getter_xpath_cb=_sourcemode_xpath,
make_setter_xpath_cb=_sourcemode_xpath,
make_xpath_cb=_make_sourcemode_xpath,
default_cb=_get_default_source_mode)
def _get_default_sourcehost(self):
@ -188,24 +188,22 @@ class _VirtualCharDevice(VirtualDevice):
if not self._has_mode_connect:
self._has_mode_connect = self.MODE_CONNECT
return val
def _sourcehost_xpath(self):
def _make_sourcehost_xpath(self):
mode = self.source_mode
if self.type == self.TYPE_UDP:
mode = "connect"
return "./source[@mode='%s']/@host" % mode
source_host = XMLProperty(name="char sourcehost",
doc=_("Address to connect/listen to."),
make_getter_xpath_cb=_sourcehost_xpath,
make_setter_xpath_cb=_sourcehost_xpath,
make_xpath_cb=_make_sourcehost_xpath,
default_cb=_get_default_sourcehost,
set_converter=_set_source_validate)
def _sourceport_xpath(self):
def _make_sourceport_xpath(self):
return "./source[@mode='%s']/@service" % self.source_mode
source_port = XMLProperty(name="char sourceport",
doc=_("Port on target host to connect/listen to."),
make_getter_xpath_cb=_sourceport_xpath,
make_setter_xpath_cb=_sourceport_xpath,
make_xpath_cb=_make_sourceport_xpath,
set_converter=_set_source_validate, is_int=True)
_has_mode_connect = XMLProperty("./source[@mode='connect']/@mode")

View File

@ -482,21 +482,10 @@ class VirtualDisk(VirtualDevice):
# XML properties #
##################
def _make_getter_xpath_cb(self):
xpath = None
ret = "./source/@file"
for prop in _TARGET_PROPS:
xpath = "./source/@" + prop
if self._xmlstate.xml_ctx.xpathEval(
self.fix_relative_xpath(xpath)):
ret = xpath
break
return ret
def _make_setter_xpath_cb(self):
def _make_source_xpath(self):
return "./source/@" + self.disk_type_to_target_prop(self.type)
_xmlpath = XMLProperty(name="disk path",
make_getter_xpath_cb=_make_getter_xpath_cb,
make_setter_xpath_cb=_make_setter_xpath_cb,
make_xpath_cb=_make_source_xpath,
clear_first=["./source/@" + target for target in
_TARGET_PROPS])

View File

@ -86,21 +86,10 @@ class VirtualFilesystem(VirtualDevice):
readonly = XMLProperty("./readonly", is_bool=True)
def _xml_get_source_xpath(self):
xpath = None
ret = "./source/@dir"
for prop in self._target_props:
xpath = "./source/@" + prop
if self._xmlstate.xml_ctx.xpathEval(
self.fix_relative_xpath(xpath)):
ret = xpath
return ret
def _xml_set_source_xpath(self):
ret = "./source/@" + self.type_to_source_prop(self.type)
return ret
def _make_source_xpath(self):
return "./source/@" + self.type_to_source_prop(self.type)
source = XMLProperty(name="filesystem source",
make_getter_xpath_cb=_xml_get_source_xpath,
make_setter_xpath_cb=_xml_set_source_xpath)
make_xpath_cb=_make_source_xpath)
def _validate_set_target(self, val):
# In case of qemu for default fs type (mount) target is not

View File

@ -272,7 +272,7 @@ class StoragePool(_StorageObject):
self._random_uuid = util.generate_uuid(self.conn)
return self._random_uuid
def _source_path_xpath(self):
def _make_source_xpath(self):
if self.type == self.TYPE_NETFS:
return "./source/dir/@path"
if self.type == self.TYPE_SCSI:
@ -330,8 +330,7 @@ class StoragePool(_StorageObject):
iqn = XMLProperty("./source/initiator/iqn/@name",
doc=_("iSCSI initiator qualified name"))
source_path = XMLProperty(name="source path",
make_getter_xpath_cb=_source_path_xpath,
make_setter_xpath_cb=_source_path_xpath)
make_xpath_cb=_make_source_xpath)
source_name = XMLProperty("./source/name",
default_cb=_default_source_name,
doc=_("Name of the Volume Group"))
@ -379,7 +378,7 @@ class StoragePool(_StorageObject):
Return the /disk/@type value if the pool source is used as
VirtualDisk path
"""
xpath = self._source_path_xpath()
xpath = self._make_source_xpath()
if "/dir/" in xpath:
return "dir"
return "block"

View File

@ -330,8 +330,7 @@ class XMLChildProperty(property):
class XMLProperty(property):
def __init__(self, xpath=None, name=None, doc=None,
set_converter=None, validate_cb=None,
make_getter_xpath_cb=None, make_setter_xpath_cb=None,
set_converter=None, validate_cb=None, make_xpath_cb=None,
is_bool=False, is_int=False, is_yesno=False, is_onoff=False,
clear_first=None, default_cb=None, default_name=None,
track=True):
@ -358,10 +357,9 @@ class XMLProperty(property):
operation we convert the XML value with int(val) / 1024.
@param validate_cb: Called once when value is set, should
raise a RuntimeError if the value is not proper.
@param make_getter_xpath_cb:
@param make_setter_xpath_cb: Not all props map cleanly to a
@param make_xpath_cb: Not all props map cleanly to a
static xpath. This allows passing functions which generate
an xpath for getting or setting.
an xpath.
@param is_bool: Whether this is a boolean property in the XML
@param is_int: Whether this is an integer property in the XML
@param is_yesno: Whether this is a yes/no property in the XML
@ -390,9 +388,7 @@ class XMLProperty(property):
self._is_yesno = is_yesno
self._is_onoff = is_onoff
self._xpath_for_getter_cb = make_getter_xpath_cb
self._xpath_for_setter_cb = make_setter_xpath_cb
self._make_xpath_cb = make_xpath_cb
self._validate_cb = validate_cb
self._convert_value_for_setter_cb = set_converter
self._setter_clear_these_first = clear_first or []
@ -436,19 +432,12 @@ class XMLProperty(property):
raise RuntimeError("Didn't find expected property=%s" % self)
return self._propname
def _xpath_for_getter(self, xmlbuilder):
def _make_xpath(self, xmlbuilder):
ret = self._xpath
if self._xpath_for_getter_cb:
ret = self._xpath_for_getter_cb(xmlbuilder)
if self._make_xpath_cb:
ret = self._make_xpath_cb(xmlbuilder)
if ret is None:
raise RuntimeError("%s: didn't generate any setter xpath." % self)
return xmlbuilder.fix_relative_xpath(ret)
def _xpath_for_setter(self, xmlbuilder):
ret = self._xpath
if self._xpath_for_setter_cb:
ret = self._xpath_for_setter_cb(xmlbuilder)
if ret is None:
raise RuntimeError("%s: didn't generate any setter xpath." % self)
raise RuntimeError("%s: didn't generate any xpath." % self)
return xmlbuilder.fix_relative_xpath(ret)
@ -609,7 +598,7 @@ class XMLProperty(property):
"""
Actually fetch the associated value from the backing XML
"""
xpath = self._xpath_for_getter(xmlbuilder)
xpath = self._make_xpath(xmlbuilder)
node = _get_xpath_node(xmlbuilder._xmlstate.xml_ctx, xpath)
if not node:
return None
@ -636,7 +625,7 @@ class XMLProperty(property):
"""
if root_node is None:
root_node = xmlbuilder._xmlstate.xml_node
xpath = self._xpath_for_setter(xmlbuilder)
xpath = self._make_xpath(xmlbuilder)
node = _get_xpath_node(xmlbuilder._xmlstate.xml_ctx, xpath)
clearlist = self._build_clear_list(xmlbuilder, node)