xmlbuilder: Allow is_int with custom converter options
This commit is contained in:
parent
012d1cdc52
commit
6127cac414
|
@ -195,18 +195,11 @@ class _VirtualCharDevice(VirtualDevice):
|
|||
|
||||
def _sourceport_xpath(self):
|
||||
return "./source[@mode='%s']/@service" % self.source_mode
|
||||
def _get_sourceport_convert(self, val):
|
||||
if val is None:
|
||||
return None
|
||||
return int(val)
|
||||
def _set_sourceport_convert(self, val):
|
||||
return self._get_sourceport_convert(self._set_source_validate(val))
|
||||
source_port = XMLProperty(name="char sourceport",
|
||||
doc=_("Port on target host to connect/listen to."),
|
||||
xml_get_xpath=_sourceport_xpath,
|
||||
xml_set_xpath=_sourceport_xpath,
|
||||
set_converter=_set_sourceport_convert,
|
||||
get_converter=_get_sourceport_convert)
|
||||
set_converter=_set_source_validate, is_int=True)
|
||||
|
||||
_has_mode_connect = XMLProperty(xpath="./source[@mode='connect']/@mode")
|
||||
_has_mode_bind = XMLProperty(xpath="./source[@mode='bind']/@mode")
|
||||
|
@ -220,16 +213,9 @@ class _VirtualCharDevice(VirtualDevice):
|
|||
bind_host = XMLProperty(xpath="./source[@mode='bind']/@host",
|
||||
doc=_("Host addresss to bind to."),
|
||||
set_converter=_set_bind_validate)
|
||||
def _get_bindport_convert(self, val):
|
||||
if val is None:
|
||||
return None
|
||||
return int(val)
|
||||
def _set_bindport_convert(self, val):
|
||||
return self._get_bindport_convert(self._set_bind_validate(val))
|
||||
bind_port = XMLProperty(xpath="./source[@mode='bind']/@service",
|
||||
doc=_("Host port to bind to."),
|
||||
get_converter=_get_bindport_convert,
|
||||
set_converter=_set_bindport_convert)
|
||||
set_converter=_set_bind_validate, is_int=True)
|
||||
|
||||
def _get_default_protocol(self):
|
||||
if not self.supports_property("protocol"):
|
||||
|
|
|
@ -39,12 +39,6 @@ def _validate_port(name, val):
|
|||
return val
|
||||
|
||||
|
||||
def _int_or_none(val):
|
||||
if val is None:
|
||||
return val
|
||||
return int(val)
|
||||
|
||||
|
||||
class VirtualGraphics(VirtualDevice):
|
||||
virtual_device_type = VirtualDevice.VIRTUAL_DEV_GRAPHICS
|
||||
|
||||
|
@ -148,12 +142,10 @@ class VirtualGraphics(VirtualDevice):
|
|||
if self.type == "spice":
|
||||
return (self.port == -1 or self.tlsPort == -1) and "yes" or "no"
|
||||
return None
|
||||
port = XMLProperty(xpath="./@port",
|
||||
get_converter=lambda s, v: _int_or_none(v),
|
||||
port = XMLProperty(xpath="./@port", is_int=True,
|
||||
set_converter=lambda s, v: _validate_port("Port", v),
|
||||
default_cb=_get_default_port)
|
||||
tlsPort = XMLProperty(xpath="./@tlsPort",
|
||||
get_converter=lambda s, v: _int_or_none(v),
|
||||
tlsPort = XMLProperty(xpath="./@tlsPort", is_int=True,
|
||||
set_converter=lambda s, v: _validate_port("TLS port", v),
|
||||
default_cb=_get_default_tlsport)
|
||||
autoport = XMLProperty(xpath="./@autoport", is_yesno=True,
|
||||
|
|
|
@ -303,10 +303,8 @@ class XMLProperty(property):
|
|||
self._default_cb = default_cb
|
||||
self._default_name = default_name
|
||||
|
||||
if sum([int(bool(i)) for i in [
|
||||
self._is_bool, self._is_int, self._is_yesno,
|
||||
(self._convert_value_for_getter_cb or
|
||||
self._convert_value_for_setter_cb)]]) > 1:
|
||||
if sum([int(bool(i)) for i in
|
||||
[self._is_bool, self._is_int, self._is_yesno]]) > 1:
|
||||
raise RuntimeError("Conflict property converter options.")
|
||||
|
||||
if self._default_name and not self._default_cb:
|
||||
|
@ -403,13 +401,14 @@ class XMLProperty(property):
|
|||
def _convert_value_for_setter(self, xmlbuilder):
|
||||
# Convert from API value to XML value
|
||||
val = self._orig_fget(xmlbuilder)
|
||||
if self._convert_value_for_setter_cb:
|
||||
val = self._convert_value_for_setter_cb(xmlbuilder, val)
|
||||
elif self._default_name and val == self._default_name:
|
||||
if self._default_name and val == self._default_name:
|
||||
val = self._default_cb(xmlbuilder)
|
||||
elif self._is_yesno:
|
||||
if val is not None:
|
||||
val = bool(val) and "yes" or "no"
|
||||
|
||||
if self._convert_value_for_setter_cb:
|
||||
val = self._convert_value_for_setter_cb(xmlbuilder, val)
|
||||
return val
|
||||
|
||||
def _build_node_list(self, xmlbuilder, xpath):
|
||||
|
@ -448,20 +447,24 @@ class XMLProperty(property):
|
|||
|
||||
if self._is_bool:
|
||||
if initial and self._is_tri and val is None:
|
||||
return None
|
||||
return bool(val)
|
||||
ret = None
|
||||
else:
|
||||
ret = bool(val)
|
||||
elif self._is_int and val is not None:
|
||||
intkwargs = {}
|
||||
if "0x" in str(val):
|
||||
intkwargs["base"] = 16
|
||||
return int(val, **intkwargs)
|
||||
ret = int(val, **intkwargs)
|
||||
elif self._is_yesno and val is not None:
|
||||
return bool(val == "yes")
|
||||
elif self._convert_value_for_getter_cb:
|
||||
return self._convert_value_for_getter_cb(xmlbuilder, val)
|
||||
ret = bool(val == "yes")
|
||||
elif self._is_multi and val is None:
|
||||
return []
|
||||
return val
|
||||
ret = []
|
||||
else:
|
||||
ret = val
|
||||
|
||||
if self._convert_value_for_getter_cb:
|
||||
ret = self._convert_value_for_getter_cb(xmlbuilder, ret)
|
||||
return ret
|
||||
|
||||
def _orig_fget(self, xmlbuilder):
|
||||
# Returns (is unset, fget value)
|
||||
|
|
Loading…
Reference in New Issue