XMLProperty: Add convenience param is_int

This commit is contained in:
Cole Robinson 2013-07-13 21:49:32 -04:00
parent 5f58d05051
commit 7d75a43e33
5 changed files with 25 additions and 28 deletions

View File

@ -283,8 +283,8 @@ class XMLParseTest(unittest.TestCase):
check("iotune_rbs", 2, 0) check("iotune_rbs", 2, 0)
check("iotune_wis", 3, 0) check("iotune_wis", 3, 0)
check("iotune_wbs", 4, 0) check("iotune_wbs", 4, 0)
check("iotune_tis", 0, 5) check("iotune_tis", None, 5)
check("iotune_tbs", 0, 6) check("iotune_tbs", None, 6)
self._alter_compare(guest.get_xml_config(), outfile) self._alter_compare(guest.get_xml_config(), outfile)

View File

@ -365,8 +365,7 @@ class Guest(XMLBuilder):
val = self.__validate_cpus(val) val = self.__validate_cpus(val)
self._maxvcpus = val self._maxvcpus = val
maxvcpus = XMLProperty(_get_maxvcpus, _set_maxvcpus, maxvcpus = XMLProperty(_get_maxvcpus, _set_maxvcpus,
xpath="./vcpu", xpath="./vcpu", is_int=True)
get_converter=lambda s, x: int(x))
# set phy-cpus for the guest # set phy-cpus for the guest
def get_cpuset(self): def get_cpuset(self):

View File

@ -496,24 +496,12 @@ class VirtualDisk(VirtualDevice):
error_policy = XMLProperty(xpath="./driver/@error_policy") error_policy = XMLProperty(xpath="./driver/@error_policy")
serial = XMLProperty(xpath="./serial") serial = XMLProperty(xpath="./serial")
iotune_rbs = XMLProperty(xpath="./iotune/read_bytes_sec", iotune_rbs = XMLProperty(xpath="./iotune/read_bytes_sec", is_int=True)
get_converter=lambda s, x: int(x or 0), iotune_ris = XMLProperty(xpath="./iotune/read_iops_sec", is_int=True)
set_converter=lambda s, x: int(x)) iotune_tbs = XMLProperty(xpath="./iotune/total_bytes_sec", is_int=True)
iotune_ris = XMLProperty(xpath="./iotune/read_iops_sec", iotune_tis = XMLProperty(xpath="./iotune/total_iops_sec", is_int=True)
get_converter=lambda s, x: int(x or 0), iotune_wbs = XMLProperty(xpath="./iotune/write_bytes_sec", is_int=True)
set_converter=lambda s, x: int(x)) iotune_wis = XMLProperty(xpath="./iotune/write_iops_sec", is_int=True)
iotune_tbs = XMLProperty(xpath="./iotune/total_bytes_sec",
get_converter=lambda s, x: int(x or 0),
set_converter=lambda s, x: int(x))
iotune_tis = XMLProperty(xpath="./iotune/total_iops_sec",
get_converter=lambda s, x: int(x or 0),
set_converter=lambda s, x: int(x))
iotune_wbs = XMLProperty(xpath="./iotune/write_bytes_sec",
get_converter=lambda s, x: int(x or 0),
set_converter=lambda s, x: int(x))
iotune_wis = XMLProperty(xpath="./iotune/write_iops_sec",
get_converter=lambda s, x: int(x or 0),
set_converter=lambda s, x: int(x))
############################# #############################

View File

@ -206,9 +206,7 @@ class VirtualGraphics(VirtualDevice):
raise ValueError(_("VNC port must be a number between " raise ValueError(_("VNC port must be a number between "
"5900 and 65535, or -1 for auto allocation")) "5900 and 65535, or -1 for auto allocation"))
self._port = val self._port = val
port = XMLProperty(get_port, set_port, port = XMLProperty(get_port, set_port, is_int=True, xpath="./@port")
get_converter=lambda s, x: int(x or -1),
xpath="./@port")
def get_listen(self): def get_listen(self):
return self._listen return self._listen
@ -312,13 +310,19 @@ class VirtualGraphics(VirtualDevice):
return self._build_xml(display=disp, xauth=xauth) return self._build_xml(display=disp, xauth=xauth)
def _spice_config(self): def _spice_config(self):
return self._build_xml(port=self.port, keymap=self.keymap, port = self.port
if port is None:
port = -1
return self._build_xml(port=port, keymap=self.keymap,
passwd=self.passwd, listen=self.listen, passwd=self.passwd, listen=self.listen,
tlsPort=self.tlsPort, canautoport=True, tlsPort=self.tlsPort, canautoport=True,
passwdValidTo=self.passwdValidTo) passwdValidTo=self.passwdValidTo)
def _vnc_config(self): def _vnc_config(self):
return self._build_xml(port=self.port, keymap=self.keymap, port = self.port
if port is None:
port = -1
return self._build_xml(port=port, keymap=self.keymap,
passwd=self.passwd, listen=self.listen, passwd=self.passwd, listen=self.listen,
# VNC supports autoport, but use legacy # VNC supports autoport, but use legacy
# syntax to not break XML tests # syntax to not break XML tests

View File

@ -225,7 +225,7 @@ class XMLProperty(property):
def __init__(self, fget=None, fset=None, doc=None, def __init__(self, fget=None, fset=None, doc=None,
xpath=None, get_converter=None, set_converter=None, xpath=None, get_converter=None, set_converter=None,
xml_get_xpath=None, xml_set_xpath=None, xml_get_xpath=None, xml_set_xpath=None,
is_bool=False, is_tri=False, is_multi=False, is_bool=False, is_tri=False, is_int=False, is_multi=False,
default_converter=None, clear_first=None): default_converter=None, clear_first=None):
""" """
Set a XMLBuilder class property that represents a value in the Set a XMLBuilder class property that represents a value in the
@ -257,6 +257,7 @@ class XMLProperty(property):
@param is_tri: Boolean XML property, but return None if there's @param is_tri: Boolean XML property, but return None if there's
no value set. no value set.
@param is_multi: Whether data is coming multiple or a single node @param is_multi: Whether data is coming multiple or a single node
@param is_int: Whethere this is an integer property in the XML
@param default_converter: If the virtinst value is "default", use @param default_converter: If the virtinst value is "default", use
this function to get the actual XML value this function to get the actual XML value
@param clear_first: List of xpaths to unset before any 'set' operation. @param clear_first: List of xpaths to unset before any 'set' operation.
@ -268,6 +269,7 @@ class XMLProperty(property):
self._is_tri = is_tri self._is_tri = is_tri
self._is_bool = is_bool or is_tri self._is_bool = is_bool or is_tri
self._is_int = is_int
self._is_multi = is_multi self._is_multi = is_multi
self._xpath_for_getter_cb = xml_get_xpath self._xpath_for_getter_cb = xml_get_xpath
@ -439,6 +441,8 @@ class XMLProperty(property):
val = self._convert_value_for_getter_cb(xmlbuilder, val) val = self._convert_value_for_getter_cb(xmlbuilder, val)
elif self._is_bool: elif self._is_bool:
val = True val = True
elif self._is_int:
val = int(val)
if not self._is_multi: if not self._is_multi:
return val return val
@ -530,6 +534,8 @@ class XMLBuilder(object):
def copy(self): def copy(self):
# pylint: disable=W0212
# Access to protected member, needed to unittest stuff
ret = copy.copy(self) ret = copy.copy(self)
ret._propstore = ret._propstore.copy() ret._propstore = ret._propstore.copy()
ret._proporder = ret._proporder[:] ret._proporder = ret._proporder[:]