mirror of
https://github.com/virt-manager/virt-manager.git
synced 2024-10-27 10:25:25 +03:00
VirtualWatchdog: Convert to new style XML props
This commit is contained in:
parent
e5230e90e9
commit
6f61e30b18
@ -25,15 +25,17 @@ class VirtualWatchdog(VirtualDevice):
|
||||
|
||||
_virtual_device_type = VirtualDevice.VIRTUAL_DEV_WATCHDOG
|
||||
|
||||
MODEL_I6300 = "i6300esb"
|
||||
MODEL_IB700 = "ib700"
|
||||
MODEL_DEFAULT = "default"
|
||||
MODELS = ["i6300esb", "ib700", MODEL_DEFAULT]
|
||||
MODELS = [MODEL_I6300, MODEL_IB700, MODEL_DEFAULT]
|
||||
|
||||
ACTION_DEFAULT = "default"
|
||||
ACTION_SHUTDOWN = "shutdown"
|
||||
ACTION_RESET = "reset"
|
||||
ACTION_POWEROFF = "poweroff"
|
||||
ACTION_PAUSE = "pause"
|
||||
ACTION_NONE = "none"
|
||||
ACTION_DEFAULT = "default"
|
||||
ACTIONS = [ACTION_RESET, ACTION_SHUTDOWN,
|
||||
ACTION_POWEROFF, ACTION_PAUSE,
|
||||
ACTION_NONE, ACTION_DEFAULT]
|
||||
@ -52,54 +54,12 @@ class VirtualWatchdog(VirtualDevice):
|
||||
return _("No action")
|
||||
if action == VirtualWatchdog.ACTION_DEFAULT:
|
||||
return _("Hypervisor default")
|
||||
else:
|
||||
return action
|
||||
return action
|
||||
|
||||
def __init__(self, conn, parsexml=None, parsexmlnode=None):
|
||||
VirtualDevice.__init__(self, conn, parsexml, parsexmlnode)
|
||||
|
||||
self._model = None
|
||||
self._action = None
|
||||
|
||||
if self._is_parse():
|
||||
return
|
||||
|
||||
self.model = self.MODEL_DEFAULT
|
||||
self.action = self.ACTION_DEFAULT
|
||||
|
||||
def get_model(self):
|
||||
return self._model
|
||||
def set_model(self, new_model):
|
||||
if type(new_model) != str:
|
||||
raise ValueError(_("'model' must be a string, "
|
||||
" was '%s'." % type(new_model)))
|
||||
if not self.MODELS.count(new_model):
|
||||
raise ValueError(_("Unsupported watchdog model '%s'" % new_model))
|
||||
self._model = new_model
|
||||
model = XMLProperty(get_model, set_model,
|
||||
xpath="./@model")
|
||||
|
||||
def get_action(self):
|
||||
return self._action
|
||||
def set_action(self, val):
|
||||
if val not in self.ACTIONS:
|
||||
raise ValueError("Unknown watchdog action '%s'." % val)
|
||||
self._action = val
|
||||
action = XMLProperty(get_action, set_action,
|
||||
xpath="./@action")
|
||||
|
||||
def _get_xml_config(self):
|
||||
model = self.model
|
||||
if model == self.MODEL_DEFAULT:
|
||||
model = "i6300esb"
|
||||
|
||||
action = self.action
|
||||
if action == self.ACTION_DEFAULT:
|
||||
action = self.ACTION_RESET
|
||||
|
||||
xml = " <watchdog model='%s'" % model
|
||||
if action:
|
||||
xml += " action='%s'" % action
|
||||
xml += "/>"
|
||||
|
||||
return xml
|
||||
_XML_PROP_ORDER = ["model", "action"]
|
||||
model = XMLProperty(xpath="./@model",
|
||||
default_name=MODEL_DEFAULT,
|
||||
default_cb=lambda s: s.MODEL_I6300)
|
||||
action = XMLProperty(xpath="./@action",
|
||||
default_name=ACTION_DEFAULT,
|
||||
default_cb=lambda s: s.ACTION_RESET)
|
||||
|
@ -229,7 +229,7 @@ class XMLProperty(property):
|
||||
get_converter=None, set_converter=None,
|
||||
xml_get_xpath=None, xml_set_xpath=None,
|
||||
is_bool=False, is_tri=False, is_int=False, is_multi=False,
|
||||
clear_first=None, default_cb=None):
|
||||
clear_first=None, default_cb=None, default_name=None):
|
||||
"""
|
||||
Set a XMLBuilder class property that represents a value in the
|
||||
<domain> XML. For example
|
||||
@ -270,6 +270,8 @@ class XMLProperty(property):
|
||||
is never explicitly altered, this function is called for setting
|
||||
a default value in the XML, and for any 'get' call before the
|
||||
first explicit 'set'.
|
||||
@param default_name: If the user does a set and passes in this
|
||||
value, instead use the value of default_cb()
|
||||
"""
|
||||
|
||||
self._xpath = xpath
|
||||
@ -289,6 +291,7 @@ class XMLProperty(property):
|
||||
self._convert_value_for_setter_cb = set_converter
|
||||
self._setter_clear_these_first = clear_first or []
|
||||
self._default_cb = default_cb
|
||||
self._default_name = default_name
|
||||
|
||||
if sum([int(bool(i)) for i in [
|
||||
self._is_bool, self._is_int,
|
||||
@ -296,6 +299,9 @@ class XMLProperty(property):
|
||||
self._convert_value_for_setter_cb)]]) > 1:
|
||||
raise RuntimeError("Conflict property converter options.")
|
||||
|
||||
if self._default_name and not self._default_cb:
|
||||
raise RuntimeError("default_name requires default_cb.")
|
||||
|
||||
self._fget = fget
|
||||
self._fset = fset
|
||||
|
||||
@ -389,6 +395,8 @@ class XMLProperty(property):
|
||||
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:
|
||||
val = self._default_cb(xmlbuilder)
|
||||
return val
|
||||
|
||||
def _build_node_list(self, xmlbuilder, xpath):
|
||||
@ -453,6 +461,8 @@ class XMLProperty(property):
|
||||
propname = self._findpropname(xmlbuilder)
|
||||
unset = (propname not in propstore)
|
||||
if unset and self._default_cb:
|
||||
if self._default_name:
|
||||
return self._default_name
|
||||
return self._default_cb(xmlbuilder)
|
||||
return propstore.get(propname, None)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user